sql to pick apart a string of a persons name and output the initials

后端 未结 5 1287
独厮守ぢ
独厮守ぢ 2020-12-21 15:28

how can i get SQL to take a sting and return the first letter of each word passed into it.

I want to use this UDF for generating initials for peoples names I have in

5条回答
  •  旧巷少年郎
    2020-12-21 15:48

    A Picture is 100 times better than description. Here is an example of UDF declaration:

    CREATE FUNCTION dbo.GetOnlyFirstLetters(@str NVARCHAR(4000),@sep NVARCHAR(10) )
    RETURNS NVARCHAR(100)
    AS
    BEGIN
       DECLARE @textXML XML
    
       SELECT   @textXML = CAST('' + replace(@str, @sep, '') + '' 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
    

    Here is how to call:

    SELECT  dbo.GetOnlyFirstLetters('Humayoun Kabir Sohel',' ');
    

    Result will be:

    HKS
    

提交回复
热议问题