Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 633973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:14:38+00:00 2026-05-13T20:14:38+00:00

I am using MS SQL 2005 I have a problem, which currently I am

  • 0

I am using MS SQL 2005
I have a problem, which currently I am battling to get a solution.

I have a Table, with a these columns : NameList;Time

The Namelist Column has comma delimited data in it. The table data is as such:

Namelist    Time
John Smith, Jeremy Boyle, Robert Brits, George Aldrich  5
John Smith, Peter Hanson    15
Jeremy Boyle, Robert Brits  10
....

I need some sort of SQL expression that will provide me with this end result:

Name    Total_Time
John Smith  20
Jeremy Boyle    15
Robert Brits    15

Etc……
Basically the expression must find All the names in the rows and math those names with the names in the other rows and add the times together for each user.

The Idea I have is to convert the comma delimited data into rows and count the distinct records of each then somehow know what the time for it is… then multiply….. but I have no idea on how to implement it

Any Help would be much appreciated

Thanks,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T20:14:38+00:00Added an answer on May 13, 2026 at 8:14 pm

    I prefer the number table approach to split a string in TSQL

    For this method to work, you need to do this one time table setup:

    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)
    

    Once the Numbers table is set up, create this split function:

    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 < LEN(dt.List2)
                      WHERE SUBSTRING(List2, number, 1) = @SplitOn
                 ) dt2
            WHERE ListValue IS NOT NULL AND ListValue!=''
    
    );
    GO 
    

    You can now easily split a CSV string into a table and join on it:

    select * from dbo.FN_ListToTable(',','1,2,3,,,4,5,6777,,,')
    

    OUTPUT:

    ListValue
    -----------------------
    1
    2
    3
    4
    5
    6777
    
    (6 row(s) affected)
    

    Your can now use a CROSS APPLY to split every row in your table like:

    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
    

    OUTPUT:

    NameOf                  TimeOf     
    ----------------------- -----------
    George Aldrich          5          
    Jeremy Boyle            15         
    John Smith              20         
    Peter Hanson            15         
    Robert Brits            15         
    
    (5 row(s) affected)
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using SQL Server 2005. I have a table with a text column
I am using MS SQL Server 2005, I have dates stored in epoch time
I'm using SQL Server 2005. I have a field that must either contain a
We're using SQL Server 2005 in a project. The users of the system have
I'm using SQL Server 2008 Management studio viewing a 2005 server and have just
We are using SQL 2005 and the try-catch functionality to handle all of our
Using SQL Server 2005 and Management Studio how do I insert a picture into
When using SQL Server Management Studio from SQL Server 2005, I can connect to
I'm using SQL Server 2005, and I would like to know how to access
We are using SQL Server 2005, but this question can be for any RDBMS

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.