October 8, 2022 at 3:38 am
October 8, 2022 at 8:53 am
If you have Jeff Moden's infamous dbo.DelimitedSplit8K splitter function installed (link), this becomes fairly straightforward:
DROP TABLE IF EXISTS #SomeText;
CREATE TABLE #SomeText
(
Name VARCHAR(500) NOT NULL
);
INSERT #SomeText
(
Name
)
VALUES
('Sathish Kumar')
,('A B C')
,('Shijay')
,('Some other random stuff');
SELECT st.Name
,InitialLetters = STRING_AGG(spl.InitialLetter, ' ') WITHIN GROUP(ORDER BY spl.ItemNumber)
FROM #SomeText st
CROSS APPLY
(
SELECT ItemNumber
,InitialLetter = LEFT(Item, 1)
FROM dbo.DelimitedSplit8K(st.Name, ' ')
) spl
GROUP BY st.Name;
The absence of evidence is not evidence of absence.
Martin Rees
You can lead a horse to water, but a pencil must be lead.
Stan Laurel
October 8, 2022 at 6:45 pm
Hi All,
How could I can extract all the first characters in the string using SQL Server query
Just a posting tip... if you post "Readily Consumable Data" in the future instead of a picture, people will respond to your post a lot quicker. You were lucky that Phil saw this one (he's a kind soul 😀 ) and posted working code that also demonstrates how to post "Readily Consumable Data".
For a bit more on why you should take the time, please read the article at the first link in my signature line below. It also shows one of a few ways to generate such data from a table without much strain or pain on your part.
--Jeff Moden
Change is inevitable... Change for the better is not.
October 8, 2022 at 6:48 pm
As a bit of a sidebar, you may have some people come back with a suggestion of using the built-in STRING_SPLIT() function. That will seem to work but there is absolutely no guarantee that it will produce the output in the correct order until SQL Server 2022 comes out, where the finally realized that you need a position ordinal for each value returned in order to guarantee a correct sort.
--Jeff Moden
Change is inevitable... Change for the better is not.
October 8, 2022 at 11:08 pm
As a bit of a sidebar, you may have some people come back with a suggestion of using the built-in STRING_SPLIT() function. That will seem to work but there is absolutely no guarantee that it will produce the output in the correct order until SQL Server 2022 comes out, where the finally realized that you need a position ordinal for each value returned in order to guarantee a correct sort.
You could use this one instead https://www.sqlservercentral.com/scripts/a-varcharmax-string_split-function-for-sql-2012-and-above
November 5, 2022 at 8:28 pm
Jeff Moden wrote:As a bit of a sidebar, you may have some people come back with a suggestion of using the built-in STRING_SPLIT() function. That will seem to work but there is absolutely no guarantee that it will produce the output in the correct order until SQL Server 2022 comes out, where the finally realized that you need a position ordinal for each value returned in order to guarantee a correct sort.
You could use this one instead https://www.sqlservercentral.com/scripts/a-varcharmax-string_split-function-for-sql-2012-and-above
One of these days, you'll need to post a performance test for that, Jonathan. 😀
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply