SQL Query to retrieve 2 digit numbers from the below string - sql

SQL Query to retrieve 2 digit numbers from the below string
String --> 'Partial:[64][95]'
The output should be in the below format.
64,95

You can use the replace() function several times to replace parts of the string.
In the example below I've set a declared variable to your example string:
DECLARE #mystring varchar(100) = 'Partial:[64][95]'
SELECT REPLACE(REPLACE(REPLACE(#mystring, 'Partial:[', '')
, '][', ',')
, ']', '') AS [answer]
Produces output:
answer
------
64,95

Use replace() :
select *, replace(replace(replace(Partial, '][', ','), '[', ''), ']', '')
from table t;
However, this would reduce your nested replace via TRANSLATE() but available from SQL Server 2017.

I was able to get the result with the below query.
DECLARE #string VARCHAR(100);
SET #string = 'Partial:[64][95]';
WHILE PATINDEX('%[^0-9]%', #string) <> 0
SET #string = STUFF(#string, PATINDEX('%[^0-9]%', #string), 1, '');
DECLARE #splitstring NVARCHAR(20) = #string;
DECLARE #i INT = 3;
WHILE #i < LEN(#splitstring)
BEGIN
SELECT #splitstring = STUFF(#splitstring, #i, 0, ',');
SET #i = #i +3;
END;
SELECT #splitstring;
Thanks everyone for the help.

Related

SQL Server capitalize every first letter of every word with loop [duplicate]

What’s the best way to capitalize the first letter of each word in a string in SQL Server.
From http://www.sql-server-helper.com/functions/initcap.aspx
CREATE FUNCTION [dbo].[InitCap] ( #InputString varchar(4000) )
RETURNS VARCHAR(4000)
AS
BEGIN
DECLARE #Index INT
DECLARE #Char CHAR(1)
DECLARE #PrevChar CHAR(1)
DECLARE #OutputString VARCHAR(255)
SET #OutputString = LOWER(#InputString)
SET #Index = 1
WHILE #Index <= LEN(#InputString)
BEGIN
SET #Char = SUBSTRING(#InputString, #Index, 1)
SET #PrevChar = CASE WHEN #Index = 1 THEN ' '
ELSE SUBSTRING(#InputString, #Index - 1, 1)
END
IF #PrevChar IN (' ', ';', ':', '!', '?', ',', '.', '_', '-', '/', '&', '''', '(')
BEGIN
IF #PrevChar != '''' OR UPPER(#Char) != 'S'
SET #OutputString = STUFF(#OutputString, #Index, 1, UPPER(#Char))
END
SET #Index = #Index + 1
END
RETURN #OutputString
END
GO
There is a simpler/smaller one here (but doesn't work if any row doesn't have spaces, "Invalid length parameter passed to the RIGHT function."):
http://www.devx.com/tips/Tip/17608
As a table-valued function:
CREATE FUNCTION dbo.InitCap(#v AS VARCHAR(MAX))
RETURNS TABLE
AS
RETURN
WITH a AS (
SELECT (
SELECT UPPER(LEFT(value, 1)) + LOWER(SUBSTRING(value, 2, LEN(value))) AS 'data()'
FROM string_split(#v, ' ')
ORDER BY CHARINDEX(value,#v)
FOR XML PATH (''), TYPE) ret)
SELECT CAST(a.ret AS varchar(MAX)) ret from a
GO
Note that string_split requires COMPATIBILITY_LEVEL 130.
A variation of the one I've been using for quite some time is:
CREATE FUNCTION [widget].[properCase](#string varchar(8000)) RETURNS varchar(8000) AS
BEGIN
SET #string = LOWER(#string)
DECLARE #i INT
SET #i = ASCII('a')
WHILE #i <= ASCII('z')
BEGIN
SET #string = REPLACE( #string, ' ' + CHAR(#i), ' ' + CHAR(#i-32))
SET #i = #i + 1
END
SET #string = CHAR(ASCII(LEFT(#string, 1))-32) + RIGHT(#string, LEN(#string)-1)
RETURN #string
END
You can easily modify to handle characters after items other than spaces if you wanted to.
Another solution without using the loop - pure set-based approach with recursive CTE
create function [dbo].InitCap (#value varchar(max))
returns varchar(max) as
begin
declare
#separator char(1) = ' ',
#result varchar(max) = '';
with r as (
select value, cast(null as varchar(max)) [x], cast('' as varchar(max)) [char], 0 [no] from (select rtrim(cast(#value as varchar(max))) [value]) as j
union all
select right(value, len(value)-case charindex(#separator, value) when 0 then len(value) else charindex(#separator, value) end) [value]
, left(r.[value], case charindex(#separator, r.value) when 0 then len(r.value) else abs(charindex(#separator, r.[value])-1) end ) [x]
, left(r.[value], 1)
, [no] + 1 [no]
from r where value > '')
select #result = #result +
case
when ascii([char]) between 97 and 122
then stuff(x, 1, 1, char(ascii([char])-32))
else x
end + #separator
from r where x is not null;
set #result = rtrim(#result);
return #result;
end
If you are looking for the answer to the same question in Oracle/PLSQL then you may use the function INITCAP. Below is an example for the attribute dname from a table department which has the values ('sales', 'management', 'production', 'development').
SQL> select INITCAP(dname) from department;
INITCAP(DNAME)
--------------------------------------------------
Sales
Management
Production
Development
;WITH StudentList(Name) AS (
SELECT CONVERT(varchar(50), 'Carl-VAN')
UNION SELECT 'Dean o''brian'
UNION SELECT 'Andrew-le-Smith'
UNION SELECT 'Eddy thompson'
UNION SELECT 'BOBs-your-Uncle'
), Student AS (
SELECT CONVERT(varchar(50), UPPER(LEFT(Name, 1)) + LOWER(SUBSTRING(Name, 2, LEN(Name)))) Name,
pos = PATINDEX('%[-'' ]%', Name)
FROM StudentList
UNION ALL
SELECT CONVERT(varchar(50), LEFT(Name, pos) + UPPER(SUBSTRING(Name, pos + 1, 1)) + SUBSTRING(Name, pos + 2, LEN(Name))) Name,
pos = CASE WHEN PATINDEX('%[-'' ]%', RIGHT(Name, LEN(Name) - pos)) = 0 THEN 0 ELSE pos + PATINDEX('%[-'' ]%', RIGHT(Name, LEN(Name) - pos)) END
FROM Student
WHERE pos > 0
)
SELECT Name
FROM Student
WHERE pos = 0
ORDER BY Name
This will result in:
Andrew-Le-Smith
Bobs-Your-Uncle
Carl-Van
Dean O'Brian
Eddy Thompson
Using a recursive CTE set based query should out perform a procedural while loop query.
Here I also have made my separate to be 3 different characters [-' ] instead of 1 for a more advanced example. Using PATINDEX as I have done allows me to look for many characters. You could also use CHARINDEX on a single character and this function excepts a third parameter StartFromPosition so I could further simply my 2nd part of the recursion of the pos formula to (assuming a space): pos = CHARINDEX(' ', Name, pos + 1).
The suggested function works fine, however, if you do not want to create any function this is how I do it:
select ID,Name
,string_agg(concat(upper(substring(value,1,1)),lower(substring(value,2,len(value)-1))),' ') as ModifiedName
from Table_Customer
cross apply String_Split(replace(trim(Name),' ',' '),' ')
where Name is not null
group by ID,Name;
The above query split the words by space (' ') and create different rows of each having one substring, then convert the first letter of each substring to upper and keep remaining as lower. The final step is to string aggregate based on the key.
BEGIN
DECLARE #string varchar(100) = 'asdsadsd asdad asd'
DECLARE #ResultString varchar(200) = ''
DECLARE #index int = 1
DECLARE #flag bit = 0
DECLARE #temp varchar(2) = ''
WHILE (#Index <LEN(#string)+1)
BEGIN
SET #temp = SUBSTRING(#string, #Index-1, 1)
--select #temp
IF #temp = ' ' OR #index = 1
BEGIN
SET #ResultString = #ResultString + UPPER(SUBSTRING(#string, #Index, 1))
END
ELSE
BEGIN
SET #ResultString = #ResultString + LOWER(SUBSTRING(#string, #Index, 1))
END
SET #Index = #Index+ 1--increase the index
END
SELECT #ResultString
END
It can be as simple as this:
DECLARE #Name VARCHAR(500) = 'Roger';
SELECT #Name AS Name, UPPER(LEFT(#Name, 1)) + SUBSTRING(#Name, 2, LEN(#Name)) AS CapitalizedName;
fname is column name if fname value is akhil then UPPER(left(fname,1)) provide capital First letter(A) and substring function SUBSTRING(fname,2,LEN(fname)) provide(khil) concate both using + then result is (Akhil)
select UPPER(left(fname,1))+SUBSTRING(fname,2,LEN(fname)) as fname
FROM [dbo].[akhil]
On SQL Server 2016+ using JSON which gives guaranteed order of the words:
CREATE FUNCTION [dbo].[InitCap](#Text NVARCHAR(MAX))
RETURNS NVARCHAR(MAX)
AS
BEGIN
RETURN STUFF((
SELECT ' ' + UPPER(LEFT(s.value,1)) + LOWER(SUBSTRING(s.value,2,LEN(s.value)))
FROM OPENJSON('["' + REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(#Text,'\','\\'),'"','\"'),CHAR(9),'\t'),CHAR(10),'\n'),' ','","') + '"]') s
ORDER BY s.[key]
FOR XML PATH(''),TYPE).value('(./text())[1]','NVARCHAR(MAX)'),1,1,'');
END
GO
CREATE FUNCTION [dbo].[Capitalize](#text NVARCHAR(MAX)) RETURNS NVARCHAR(MAX) AS
BEGIN
DECLARE #result NVARCHAR(MAX) = '';
DECLARE #c NVARCHAR(1);
DECLARE #i INT = 1;
DECLARE #isPrevSpace BIT = 1;
WHILE #i <= LEN(#text)
BEGIN
SET #c = SUBSTRING(#text, #i, 1);
SET #result += IIF(#isPrevSpace = 1, UPPER(#c), LOWER(#c));
SET #isPrevSpace = IIF(#c LIKE '[ -]', 1, 0);
SET #i += 1;
END
RETURN #result;
END
GO
DECLARE #sentence NVARCHAR(100) = N'i-thINK-this soLUTION-works-LiKe-a charm';
PRINT dbo.Capitalize(#sentence);
-- I-Think-This Solution-Works-Like-A Charm
Here is the simplest one-liner to do this:
SELECT LEFT(column, 1)+ lower(RIGHT(column, len(column)-1) ) FROM [tablename]
I was looking for the best way to capitalize and i recreate simple sql script
how to use SELECT dbo.Capitalyze('this is a test with multiple spaces')
result "This Is A Test With Multiple Spaces"
CREATE FUNCTION Capitalyze(#input varchar(100) )
returns varchar(100)
as
begin
declare #index int=0
declare #char as varchar(1)=' '
declare #prevCharIsSpace as bit=1
declare #Result as varchar(100)=''
set #input=UPPER(LEFT(#input,1))+LOWER(SUBSTRING(#input, 2, LEN(#input)))
set #index=PATINDEX('% _%',#input)
if #index=0
set #index=len(#input)
set #Result=substring(#input,0,#index+1)
WHILE (#index < len(#input))
BEGIN
SET #index = #index + 1
SET #char=substring(#input,#index,1)
if (#prevCharIsSpace=1)
begin
set #char=UPPER(#char)
if (#char=' ')
set #char=''
end
if (#char=' ')
set #prevCharIsSpace=1
else
set #prevCharIsSpace=0
set #Result=#Result+#char
--print #Result
END
--print #Result
return #Result
end
IF OBJECT_ID ('dbo.fnCapitalizeFirstLetterAndChangeDelimiter') IS NOT NULL
DROP FUNCTION dbo.fnCapitalizeFirstLetterAndChangeDelimiter
GO
CREATE FUNCTION [dbo].[fnCapitalizeFirstLetterAndChangeDelimiter] (#string NVARCHAR(MAX), #delimiter NCHAR(1), #new_delimeter NCHAR(1))
RETURNS NVARCHAR(MAX)
AS
BEGIN
DECLARE #result NVARCHAR(MAX)
SELECT #result = '';
IF (LEN(#string) > 0)
DECLARE #curr INT
DECLARE #next INT
BEGIN
SELECT #curr = 1
SELECT #next = CHARINDEX(#delimiter, #string)
WHILE (LEN(#string) > 0)
BEGIN
SELECT #result =
#result +
CASE WHEN LEN(#result) > 0 THEN #new_delimeter ELSE '' END +
UPPER(SUBSTRING(#string, #curr, 1)) +
CASE
WHEN #next <> 0
THEN LOWER(SUBSTRING(#string, #curr+1, #next-2))
ELSE LOWER(SUBSTRING(#string, #curr+1, LEN(#string)-#curr))
END
IF (#next > 0)
BEGIN
SELECT #string = SUBSTRING(#string, #next+1, LEN(#string)-#next)
SELECT #next = CHARINDEX(#delimiter, #string)
END
ELSE
SELECT #string = ''
END
END
RETURN #result
END
GO

Append a specific character after each character of a string in sql server

Thanks to advise me for the below issue:
I am using below query to fetch the value of a column:
Select OptionList = case when isnull(AS_CIS_Code,'') <> '' then AS_CIS_Code
else ''
end
from added_services
AS_CIS_Code column is of varchar(10) in added_services table. It contains values like 'AB', 'ABC', 'GHKIK', 'UYTIOPJ' and so on which represents different codes.
Now I have to select these codes after modifying the above query so that '_' is appended after each character.
Like it should be fetched as 'A_B_', 'A_B_C_', 'G_H_K_I_K_', 'U_Y_T_I_O_P_J_'.
How should I implement it? Using a temp table will down the performance for one column only, so should I use while loop or please suggest me better alternatives.
Try this:
DECLARE #Input VARCHAR(100) = 'TESTING'
DECLARE #Pos INT = LEN(#Input)
WHILE #Pos > 1
BEGIN
SET #Input = STUFF(#Input,#Pos,0,'_')
SET #Pos = #Pos - 1
END
SELECT #Input
Output
T_E_S_T_I_N_G
UDF
CREATE FUNCTION PadStr(#Data VARCHAR(100)) RETURNS VARCHAR(200)
AS
BEGIN
DECLARE #Input VARCHAR(200) = #Data
DECLARE #Pos INT = LEN(#Input)
WHILE #Pos > 1
BEGIN
SET #Input = STUFF(#Input,#Pos,0,'_')
SET #Pos = #Pos - 1
END
RETURN #Input + '_'
END
Output
SELECT dbo.PadStr('TESTING') -- T_E_S_T_I_N_G_
You can split the string using a numbers table and the rebuild it using for xml path().
select isnull(C.Value, '') as AS_CIS_Code
from added_services as A
cross apply (
select substring(A.AS_CIS_Code, T.N, 1)+'_'
from (values(1),(2),(3),(4),(5),(6),(7),(8),(9),(10)) as T(N)
where T.N <= len(A.AS_CIS_Code)
order by T.N
for xml path('')
) as C(Value)
SQL Fiddle
Try this
Create Function Code_Pad
(
#code varchar(max)
)
returns varchar(max)
as
begin
Declare #coding varchar(max) =''
Declare #i int = 1
WHILE(LEN(#Coding)<=2*len(#code)-1)
begin
Select #coding = #coding+SUBSTRING(#code,#i,1)+'_'
set #i=#i+1
end
return #coding
end
End of Function
Select OptionList = case when isnull(AS_CIS_Code,'') <> '' then dbo.Code_Pad(AS_CIS_Code) else ''
end
from added_services
May not be best solution, but works in one query using recursion
;WITH valCTE(Replaced,ToBeReplaced,Position)
AS
(
SELECT CAST(LEFT(AS_CIS_Code,1) + '_' AS VARCHAR(20))
,SUBSTRING(AS_CIS_Code,2,LEN(AS_CIS_Code)-1)
,1
FROM added_services
UNION ALL
SELECT CAST(Replaced + LEFT(ToBeReplaced,1) + '_' AS VARCHAR(20))
,SUBSTRING(ToBeReplaced,2,LEN(ToBeReplaced)-1)
,Position+1
FROM valCTE
WHERE LEN(ToBeReplaced)>0
)
SELECT TOP 1 LEFT(Replaced,LEN(Replaced)-1) -- remove last _
FROM valCTE
ORDER BY Position DESC

Replacing set of character in sql query

How do i replace set of character in a table?
ColumnA
%%1234UIT
!!1234TTT
678##PPP
Currently I am using
`replace(replace(replace([BLOCK_NAME],'%%',''),'!!',''),'##',''),1,LEN(BLOCK_NAME)-1`
OR
replace(replace(replace(substring([BLOCK_NAME],1,LEN(BLOCK_NAME)-1),'**',''),'##',''),'$$','')
Expected OUTPUT:
1234UIT
123TTT
678PPP
This should work for your problem, it removes all non numeric characters from the record. You can create a sql function to be called from your query.
WHILE PATINDEX('%[^0-9]%', #strText) > 0
BEGIN
SET #strText = STUFF(#strText, PATINDEX('%[^0-9]%', #strText), 1, '')
END
RETURN #strText
You can create a function as:
CREATE FUNCTION [dbo].[fn_RemoveCharacters]
(
#Str NVARCHAR(MAX),
#MatchExpression VARCHAR(255)
)
RETURNS NVARCHAR(MAX)
AS
BEGIN
SET #MatchExpression = '%['+#MatchExpression+']%'
WHILE PatIndex(#MatchExpression, #Str) > 0
SET #Str = Stuff(#Str, PatIndex(#MatchExpression, #Str), 1, '')
RETURN #Str
END
Go
and then call it as:
SELECT [dbo].[fn_RemoveCharacters](ColumnA, '%%!!##') as ColumnA
from table1
Hope this helps!!!
Please refer how to remove non-numeric/non-alphanumeric characters from string
CREATE FUNCTION [fnRemoveNonNumericCharacters](#strText VARCHAR(1000))
RETURNS VARCHAR(1000)
AS
BEGIN
WHILE PATINDEX('%[^a-z0-9]%', #strText) > 0
BEGIN
SET #strText = STUFF(#strText, PATINDEX('%[^a-z0-9]%', #strText), 1, '')
END
RETURN #strText
END
select data using the created function
SELECT
dbo.fnRemoveNonNumericCharacters(BLOCK_NAME)
FROM
YourTable
OR
;WITH T as(
SELECT STUFF(BLOCK_NAME, PATINDEX('%[^a-z0-9]%', BLOCK_NAME), 1, '') BLOCK_NAME from YourTable
UNION ALL
SELECT STUFF(BLOCK_NAME, PATINDEX('%[^a-z0-9]%', BLOCK_NAME), 1, '') BLOCK_NAME from T where PATINDEX('%[^a-z0-9]%', BLOCK_NAME) > 0
)
select
*
from
T
where PATINDEX('%[^a-z0-9]%', BLOCK_NAME) = 0
I prefer the second method using CTE without a user-defined function.

using a concatenated string variable in another query

I have a SPROC like this in SQL server which will split a concatenated string ([var1][var2]) and return 2 result set, how do I pass each individual item from the result sets into another #var in my SProc so that I can do this, thanks:
SET #var3 = (select [var1]+[var2]) --Join the result sets values and assign it to another variable
from ...where...
Result sets:
e.g
resultset
----
tbl1
----
[var1]
resultset
----
tbl1
----
[var2]
Query that splits the concatenated string into it's parts:
declare #Str as varchar(100)
set #Str = '[Var1][Var2]'
while (#Str <> '') begin
select LEFT(#Str, CHARINDEX(']', #Str)) as resulttbl
set #Str = SUBSTRING(#Str, CHARINDEX(']', #Str) + 1, LEN(#Str))
end
You could use OUTPUT parameters...
CREATE PROCEUDRE yourSP (#str AS VARCHAR(max), #output1 AS VARCHAR(max) OUTPUT, #output2 AS VARCHAR(max) OUTPUT)
AS
BEGIN
while (#Str <> '') begin
set #output1 = LEFT(#Str, CHARINDEX(']', #Str))
set #Str = SUBSTRING(#Str, CHARINDEX(']', #Str) + 1, LEN(#Str))
end
set #puput2 = #str
END
Then call that SP with bout input and output variables.
DECLARE
#str VARCHAR(max),
#result1 VARCHAR(max),
#result2 VARCHAR(max)
SELECT
#str = '[Var1][Var2]'
EXEC yourSP #str, #result1 OUTPUT, #result2 OUTPUT
SELECT #str, #output1, #output2
Or, you could package it in a table valued function instead of a stored procedure...
SELECT
#output1 = split.value1,
#output2 = split.value2
FROM
dbo.yourFn(#str) AS split
And if you have atable of data to process, this then applows you to use APPLY...
SELECT
source.str,
split.value1,
split.value2
FROM
source
CROSS APPLY
dbo.yourFn(source.str) AS split

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

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 the DB.
names can be 2 (fname, lname)or 3(...mname) words
i am using sql2005
This should work for both "Firstname Lastname" and "Firstname Middlename Lastname" combinations.
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
Of course, if users have a space in one of their names for some reason you will have an issue parsing this out, but I suspect that would be the case anyways when not storing your names in separate fields.
For SQL Server 2017 and newer:
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;
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
Here's my solution, and it has these features/peculiarities:
It can process however many names there are in the string. (That is, both less than two and more than three.)
All spaces between the names are preserved.
I know the OP has specified that there can only be 2 or 3 names in his case. I don't mind. I'm just sharing a solution that works, and if it's not best for the particular problem, it's fine.
So, here's the function:
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;
And here's a test call:
SELECT dbo.fnGetInitials(' John Ronald Reuel Tolkien ');
and the result:
----------------------------------------------------------------------------------------------------
J R R Tolkien
You can achieve it via xquery as well.
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,'')
Here is the complete solution.
http://raresql.com/2013/04/12/sql-server-get-the-first-letter-of-each-word-in-a-string-column/
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('<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
Here is how to call:
SELECT dbo.GetOnlyFirstLetters('Humayoun Kabir Sohel',' ');
Result will be:
HKS