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
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