March 23, 2023 at 5:36 pm
March 23, 2023 at 6:28 pm
Per SQL 2022 you could try STRING_SPLIT with the optional ordinal parameter and then recombine using STRING_AGG
drop table if exists #temp
go
select *
into #temp
from (values ('First American Equipment Finance'),
('United Wholesale Monkies'),
('Texas Instruments')) T(Col1);
select string_agg(left(ss.[value], 1), '') within group (order by ss.ordinal) first1_in_order
from #temp t
cross apply string_split(t.Col1, ' ', 1) ss
group by Col1;
Aus dem Paradies, das Cantor uns geschaffen, soll uns niemand vertreiben können
March 23, 2023 at 6:40 pm
I get wrong result for the above code.
March 23, 2023 at 8:06 pm
Care to explain? What do you get when you run it? Works for me.
Be specific. Show what you get.
March 24, 2023 at 8:17 am
This was the error message
Msg 8144, Level 16, State 3, Line 12
Procedure or function string_split has too many arguments specified.
Completion time: 2023-03-24T13:47:00.0877172+05:30
March 24, 2023 at 2:59 pm
I have broken up the solution for explanation. I broke it into 3 logical parts.
1) Use 'cross apply' with string_split to list each word for each CompanyName
2) Use left to extract the initial of each word
3) Using 'string_agg' to concatenate the initials grouping on CompanyName
Here it is in detail:
/*
--- setup
*/
drop table if exists #temp
go
select *
into #temp
from (values ('First American Equipment Finance'),
('United Wholesale Monkies'),
('Texas Instruments')) T(CompanyName);
select CompanyName
, first1_in_order = string_agg(left(ss.[value], 1), '') within group (order by ss.ordinal)
from #temp t
cross apply string_split(t.CompanyName, ' ', 1) ss
group by CompanyName;
/*--- Breaking up the solution for explanation
1) Use 'cross apply' with string_split to list each word for each company name.
2) Use left to extract the initial of each word
*/
select CompanyName
, ss.value
, ss.ordinal
, Initial = left([value], 1)
from #temp t
cross apply string_split(t.CompanyName, ' ', 1) ss
CompanyName value ordinal
-------------------------------- -------------------------------- --------------------
First American Equipment Finance First 1
First American Equipment Finance American 2
First American Equipment Finance Equipment 3
First American Equipment Finance Finance 4
United Wholesale Monkies United 1
United Wholesale Monkies Wholesale 2
United Wholesale Monkies Monkies 3
Texas Instruments Texas 1
Texas Instruments Instruments 2
(9 rows affected)
Completion time: 2023-03-24T09:45:44.1896132-05:00
/*
--- 3) Using 'string_agg' to concatenate the initials grouping on CompanyName
*/
select
CompanyName
, Initials = string_agg(Initial, '') within group (order by ordinal)
from (
select CompanyName
, ss.value
, ss.ordinal
, Initial = left([value], 1)
from #temp t
cross apply string_split(t.CompanyName, ' ', 1) ss
) t
group by CompanyName
CompanyName Initials
-------------------------------- -------------------------------------------
First American Equipment Finance FAEF
Texas Instruments TI
United Wholesale Monkies UWM
(3 rows affected)
March 24, 2023 at 5:13 pm
This was the error message
Msg 8144, Level 16, State 3, Line 12 Procedure or function string_split has too many arguments specified.
Completion time: 2023-03-24T13:47:00.0877172+05:30
That probably means that you're not actually using SQL Server 2022 or the compatibility level isn't correct for SQL Server 2022. This particular forum is explicitly for 2022. Please identify what version of SQL Server you're actually using and please pay attention as to which forum you're posting to in the future.
Also, please "Read'n'Heed" the article at the first link in my signature line below for future posts. It'll help a whole lot.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply