Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="http://www.sommarskog.se/arrays-in-sql-2005.html#tblnum" rel="nofollow noreferrer">I prefer the number table approach to split a string in TSQL</a></p> <p>For this method to work, you need to do this one time table setup:</p> <pre><code>SELECT TOP 10000 IDENTITY(int,1,1) AS Number INTO Numbers FROM sys.objects s1 CROSS JOIN sys.objects s2 ALTER TABLE Numbers ADD CONSTRAINT PK_Numbers PRIMARY KEY CLUSTERED (Number) </code></pre> <p>Once the Numbers table is set up, create this split function:</p> <pre><code>CREATE FUNCTION [dbo].[FN_ListToTable] ( @SplitOn char(1) --REQUIRED, the character to split the @List string on ,@List varchar(8000)--REQUIRED, the list to split apart ) RETURNS TABLE AS RETURN ( ---------------- --SINGLE QUERY-- --this will not return empty rows ---------------- SELECT ListValue FROM (SELECT LTRIM(RTRIM(SUBSTRING(List2, number+1, CHARINDEX(@SplitOn, List2, number+1)-number - 1))) AS ListValue FROM ( SELECT @SplitOn + @List + @SplitOn AS List2 ) AS dt INNER JOIN Numbers n ON n.Number &lt; LEN(dt.List2) WHERE SUBSTRING(List2, number, 1) = @SplitOn ) dt2 WHERE ListValue IS NOT NULL AND ListValue!='' ); GO </code></pre> <p>You can now easily split a CSV string into a table and join on it:</p> <pre><code>select * from dbo.FN_ListToTable(',','1,2,3,,,4,5,6777,,,') </code></pre> <p>OUTPUT:</p> <pre><code>ListValue ----------------------- 1 2 3 4 5 6777 (6 row(s) affected) </code></pre> <p>Your can now use a CROSS APPLY to split every row in your table like:</p> <pre><code>DECLARE @YourTable table (NameList varchar(5000), TimeOf int) INSERT INTO @YourTable VALUES ('John Smith, Jeremy Boyle, Robert Brits, George Aldrich', 5) INSERT INTO @YourTable VALUES ('John Smith, Peter Hanson', 15) INSERT INTO @YourTable VALUES ('Jeremy Boyle, Robert Brits', 10) SELECT st.ListValue AS NameOf, SUM(o.TimeOf) AS TimeOf FROM @YourTable o CROSS APPLY dbo.FN_ListToTable(',',o.NameList) AS st GROUP BY st.ListValue ORDER BY st.ListValue </code></pre> <p>OUTPUT:</p> <pre><code>NameOf TimeOf ----------------------- ----------- George Aldrich 5 Jeremy Boyle 15 John Smith 20 Peter Hanson 15 Robert Brits 15 (5 row(s) affected) </code></pre> <p>Using this, I would recommend that you alter your table design and use this output to INSERT into a new table. That would be a more normalized approach. Also Don't use reserved words for column names, it makes it a hassle. Notice how I use "NameOf" and "TimeOf", so I avoid using reserved words.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload