Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 CSV list and count the elements?

Author  Topic 

learntsql

524 Posts

Posted - 2012-02-08 : 11:05:47
Hi All,
Please tell me
From following query how to get no of elements?

SELECT SUBSTRING(
(SELECT ',' + s.Name
FROM HumanResources.Shift s
ORDER BY s.Name
FOR XML PATH('')),2,200000) AS CSV
GO

TIA

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-02-08 : 11:27:26
You can count the number of comma's like this:
SELECT csv, LEN(CSV)-LEN(REPLACE(CSV,',',''))+1 FROM (
--- YOUR CODE HERE
)S
If there are any commas in the data itself, this won't work.
Go to Top of Page

learntsql

524 Posts

Posted - 2012-02-08 : 11:51:44
Nice...
ThanQ.
Go to Top of Page
   

- Advertisement -