我怎样才能让SQL采取刺痛并返回传入其中的每个单词的第一个字母。
我想使用此UDF为数据库中的人名创建首字母。
名称可以是2(fname,lname)或3(... mname)字
我正在使用sql2005
答案 0 :(得分:2)
这适用于“Firstname Lastname”和“Firstname Middlename Lastname”组合。
DECLARE @name AS NVARCHAR(50)
SET @name = 'Firstname Middle Lastname'
SELECT SUBSTRING(@name, 1, 1) + --First initial
SUBSTRING(@name, CHARINDEX(' ', @name) + 1, 1) + --Middle/Last initial
CASE WHEN 0 <> CHARINDEX(' ', @name, CHARINDEX(' ', @name) + 1) -- More than two words
THEN SUBSTRING(@name, CHARINDEX(' ', @name, CHARINDEX(' ', @name) + 1) + 1, 1) --Last initial
ELSE '' --Have to add empty string to avoid NULLing entire result
END
当然,如果用户出于某种原因在其中一个名称中有一个 空间,那么解析这个问题就会出现问题,但我怀疑在没有存储你的名字时会出现这种情况。单独的领域。
答案 1 :(得分:1)
CREATE FUNCTION dbo.GetFirstLetter ( @Array VARCHAR(1000), @separator VARCHAR(10))
RETURNS @resultTable TABLE
(parseValue VARCHAR(100))
AS
BEGIN
DECLARE @separator_position INT
DECLARE @array_value VARCHAR(1000)
SET @array = @array + @separator
WHILE patindex('%' + @separator + '%' , @array) <> 0
BEGIN
SELECT @separator_position = patindex('%' + @separator + '%', @array)
SELECT @array_value = left(@array, @separator_position - 1)
INSERT @resultTable
VALUES (SUBSTRING(Cast(@array_value AS varchar), 1, 1))
SELECT @array = stuff(@array, 1, @separator_position, '')
END
RETURN
END
答案 2 :(得分:0)
这是我的解决方案,它具有以下功能/特性:
我知道OP已经指定在他的情况下只能有2或3个名字。我不介意。我只是分享一个有效的解决方案,如果它不是特定问题的最佳选择,那就没关系。
所以,这是函数:
CREATE FUNCTION dbo.fnGetInitials (@name varchar(max))
RETURNS varchar(max)
AS BEGIN
DECLARE @cutpos int, @spacepos int, @result varchar(max);
DECLARE @cutlist TABLE (CutPos int, SpacePos int);
SET @result = LTRIM(RTRIM(@name));
SET @cutpos = 2;
SET @spacepos = CHARINDEX(' ', @result);
WHILE @spacepos > 0 BEGIN
INSERT INTO @cutlist VALUES (@cutpos, @spacepos);
SET @spacepos = @spacepos + 1;
SET @cutpos = @spacepos + 1;
SET @spacepos = CHARINDEX(' ', @result, @spacepos);
END;
DELETE FROM @cutlist WHERE CutPos >= SpacePos;
SELECT @result = STUFF(@result, CutPos, SpacePos - CutPos, '')
FROM @cutlist
ORDER BY CutPos DESC;
RETURN @result;
END;
这是一个测试电话:
SELECT dbo.fnGetInitials(' John Ronald Reuel Tolkien ');
结果:
----------------------------------------------------------------------------------------------------
J R R Tolkien
答案 3 :(得分:0)
您也可以通过xquery实现它。
Declare @Xml XML
Declare @String Varchar(Max)
Declare @firstletter Varchar(Max)
Declare @delimiter Varchar(5)
SET @delimiter=' '
SET @String= 'THIS IS SQL'
SET @Xml = cast(('<a>'+replace(@String,@delimiter,'</a><a>')+'</a>') AS XML)
;WITH CTE AS
(SELECT A.value('.', 'varchar(max)') as [Column]FROM @Xml.nodes('a') AS FN(a) )
SELECT Stuff((SELECT '' + LEFT([Column],1)from CTE
FOR XML PATH ('') ),1,0,'')
这是完整的解决方案。
http://raresql.com/2013/04/12/sql-server-get-the-first-letter-of-each-word-in-a-string-column/
答案 4 :(得分:0)
图片比描述的要好100倍。这是UDF声明的示例:
data = []
def StdDev( data ):
suma = 0
for x in data: suma += ((x - sum(data)/len(data))**2) # every iteration subtracks the mean value of a list [sum(data)/len(data)] from every [x] element of a list and then raises to the second power [**2]
print((suma/(len(data)-1))**0.5) # then divide by n-1 [len(data)-1] and extract the root of 2 [**0.5]
以下是呼叫方式:
CREATE FUNCTION dbo.GetOnlyFirstLetters(@str NVARCHAR(4000),@sep NVARCHAR(10) )
RETURNS NVARCHAR(100)
AS
BEGIN
DECLARE @textXML XML
SELECT @textXML = CAST('<d>' + replace(@str, @sep, '</d><d>') + '</d>' AS XML)
DECLARE @result VARCHAR(8000)
SET @result = ''
SELECT @result = @result + LEFT(T.split.value ('.', 'nvarchar(max)'), 1)
FROM @textXML.nodes ('/d') T (split)
RETURN @result
END
GO
结果将是:
SELECT dbo.GetOnlyFirstLetters('Humayoun Kabir Sohel',' ');
答案 5 :(得分:0)
对于 SQL Server 2017 及更新版本:
CREATE FUNCTION dbo.fnGetFirstChars (@string NVARCHAR(max), @seperator NVARCHAR(MAX))
RETURNS NVARCHAR(max)
AS BEGIN
DECLARE @result NVARCHAR(MAX)
SELECT @result = STRING_AGG(SUBSTRING(value, 1, 1), '')
FROM STRING_SPLIT(@string, @seperator)
RETURN @result
END;