How to create a function in SQL Server

前端 未结 5 1214
遇见更好的自我
遇见更好的自我 2020-12-07 18:09

Please help me, how to filter words in SQL using a function?

I\'m having a hard time if I explain it so I\'m giving example:

ID       |       Websit         


        
5条回答
  •  没有蜡笔的小新
    2020-12-07 18:47

    This one get everything between the "." characters. Please note this won't work for more complex URLs like "www.somesite.co.uk" Ideally the function would check for how many instances of the "." character and choose the substring accordingly.

    CREATE FUNCTION dbo.GetURL (@URL VARCHAR(250))
    RETURNS VARCHAR(250)
    AS BEGIN
        DECLARE @Work VARCHAR(250)
    
        SET @Work = @URL
    
        SET @Work = SUBSTRING(@work, CHARINDEX('.', @work) + 1, LEN(@work))   
        SET @Work = SUBSTRING(@work, 0, CHARINDEX('.', @work))
    
        --Alternate:
        --SET @Work = SUBSTRING(@work, CHARINDEX('.', @work) + 1, CHARINDEX('.', @work) + 1)   
    
        RETURN @work
    END
    

提交回复
热议问题