1
ID          Dates                                          Qty      Secs    DayPart

CO138491-02 06/10/2013,06/11/2013,06/12/2013,06/13/2013    4        6.00    Morning

How can I change those comma separated values and have result like this without using CTE coz i am on SQL SERVER 2000, and its better if we don't use a loop coz latter the data is going to be huge

CO138491-02 06/10/2013  4   6.00    Morning
CO138491-02 06/11/2013  4   6.00    Morning
CO138491-02 06/12/2013  4   6.00    Morning
CO138491-02 06/13/2013  4   6.00    Morning

Any hint would be appreciated or any link to limit my search

2 Answers 2

4

Try this

SELECT          a.ID,
                SUBSTRING(',' + a.Dates + ',', n.Number + 1, CHARINDEX(',', ',' + a.Dates + ',', n.Number + 1) - n.Number - 1) AS [Value]
                , [Qty], [Secs], [DayPart]
FROM            Table1 AS a
INNER JOIN      master..spt_values AS n ON SUBSTRING(',' + a.Dates + ',', n.Number, 1) = ','
WHERE           n.Type = 'p'
                AND n.Number > 0 
                AND n.Number < LEN(',' + a.Dates + ',')

Check the below link for reference

http://www.codeproject.com/Questions/526739/ConvertplusColumnplusdataplusintoplusRowsplusthrou

Sign up to request clarification or add additional context in comments.

Comments

1

If you don't want use CTE or loops, try use approach with additional table of numbers:

See http://www.sommarskog.se/arrays-in-sql-2000.html#tblnum-core

Something like:

CREATE FUNCTION inline_split_me (@param varchar(7998)) RETURNS TABLE AS
   RETURN(SELECT substring(',' + @param + ',', Number + 1,
                    charindex(',', ',' + @param + ',', Number + 1) - Number - 1)
                 AS Value
          FROM   Numbers
          WHERE  Number <= len(',' + @param + ',') - 1
            AND  substring(',' + @param + ',', Number, 1) = ',')

2 Comments

Actually I don't want a splitter rather each value with splitted strings, in addition since its a huge data is it wise to use a function??
I do not know another way, except function in this case. The most effective way is use of CLR function, but it dosn't work in MSSQL2000

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.