How to create a function in SQL Server

前端 未结 5 1211
遇见更好的自我
遇见更好的自我 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:54

    How about this?

    CREATE FUNCTION dbo.StripWWWandCom (@input VARCHAR(250))
    RETURNS VARCHAR(250)
    AS BEGIN
        DECLARE @Work VARCHAR(250)
    
        SET @Work = @Input
    
        SET @Work = REPLACE(@Work, 'www.', '')
        SET @Work = REPLACE(@Work, '.com', '')
    
        RETURN @work
    END
    

    and then use:

    SELECT ID, dbo.StripWWWandCom (WebsiteName)
    FROM dbo.YourTable .....
    

    Of course, this is severely limited in that it will only strip www. at the beginning and .com at the end - nothing else (so it won't work on other host machine names like smtp.yahoo.com and other internet domains such as .org, .edu, .de and etc.)

提交回复
热议问题