Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insert a delimited string into a table in one statement

Status
Not open for further replies.

sodakotahusker

Programmer
Mar 15, 2001
601

This is probably kind of an off the wall question. I know I can do this by looping through and parsing indivual values but I thought I'd take a stab at an easier solution. If this just won't fly I'll try to write a generic function that I can call whenever I want to parse the contents of a string into a table.

Is it possible to take a delimted string - let us say comma delimited and do a replacement on the commas to substitute whatever delimimter SQL Server uses between rows so that an insert statement will insert each item as a new row? I tried carriage return/ line feed here but that does not work.


declare @string varchar(500)
declare @crlf varchar(10)
set @string = '155,233,543,765'
set @crlf = char(13) + char(10)
set @string = replace(@string,',',@crlf)

create table #temp (id int)
insert into #temp
(id)
select @string
select * from #temp
drop table #temp
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top