如何在 SQL Server 中创建函数 [英] How to create a function in SQL Server
问题描述
请帮助我,如何使用函数过滤 SQL 中的单词?
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 | WebsiteName |
-----------------------------------
1 | www.yahoo.com |
2 | www.google.com |
3 | www.youtube.com |
我想要的是,如何获取网站的名称.我想选择具有这样输出的记录.如何删除www."以及记录中的.com".
What I want is, how to get the name of the website. I want to select the record with an output like this. How to remove the 'www.' and '.com' in the record.
ID | WebsiteName
--------------------------
1 | yahoo
感谢您的帮助.:D
推荐答案
这个怎么样?
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
然后使用:
SELECT ID, dbo.StripWWWandCom (WebsiteName)
FROM dbo.YourTable .....
当然,这是非常有限的,因为它只会去掉开头的 www.
和结尾的 .com
- 什么都没有否则(因此它不适用于其他主机名称,例如 smtp.yahoo.com
和其他 Internet 域,例如 .org
、.edu
>、.de
等)
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.)
这篇关于如何在 SQL Server 中创建函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!