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 2005 Forums
 Transact-SQL (2005)
 How to break string and insert each substring

Author  Topic 

imranabdulaziz
Yak Posting Veteran

83 Posts

Posted - 2010-08-12 : 05:24:53
Hi All,

I am using Sql server 2005.
My table is like

Empcode Date
em001 2,3,7
em002 12 , 5


I need to break it as

empcode date
em001 2
em001 3
em001 7
em002 12
em002 5



Is there any other way apart from cursor to do the job
Please help

Sachin.Nand

2937 Posts

Posted - 2010-08-12 : 05:29:05
Is your table & data is really that way or is it just some sample data???
You save dates in comma seperated format?


Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless.

PBUH
Go to Top of Page

imranabdulaziz
Yak Posting Veteran

83 Posts

Posted - 2010-08-12 : 05:37:02
No It is a temp table where data comes from excel
Go to Top of Page

Bustaz Kool
Master Smack Fu Yak Hacker

1834 Posts

Posted - 2010-08-12 : 13:07:47
In broad strokes:
1) Write a table valued function that accepts a CSV string and returns the individual strings between the commas. There are samples of this available via a search.
2) Perform a CROSS APPLY of your table data and the function you just developed

select Empcode, MyValue
from Employee
CROSS JOIN
MyCSVFunction(Date) -- Returns a column named "MyValue"

HTH

=======================================
A couple of months in the laboratory can save a couple of hours in the library. -Frank H. Westheimer, chemistry professor (1912-2007)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-08-12 : 13:12:06
see secnario 4 in

http://visakhm.blogspot.com/2010/01/multipurpose-apply-operator.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -