?
Solved

How do I convert a string of comma separated values into a database table?

Posted on 2011-09-08
7
Medium Priority
?
220 Views
Last Modified: 2012-05-12
I have a user table that looks like this, where AuthorizedDBList is a string that I use in an IN clause of an Access Database.
UserId, AuthorizedDBList
1, 050','051','052',053
What I want is to convert to a SQL Server, Many-to-Many, AuthorizedDatabase table that looks like this.
UserId, DatabaseId
1,050
1,051
1,052
1,053

My question is... how do I get from one to the other?
0
Comment
Question by:jamestieman
7 Comments
 
LVL 5

Expert Comment

by:DavidMorrison
ID: 36505807
if this is a one off I'd suggest exporting to a text editor and removing the quotes round the string.

Then pull it into sql which should give each value its own column as you've removed the quotes. from there do a PIVOT statement (http://msdn.microsoft.com/en-us/library/ms177410.aspx) and your done


Thanks

Dave
0
 
LVL 9

Expert Comment

by:borki
ID: 36505814
Is this a one-off conversion? Or, are you doing this on a regular basis?
Where are you executing the code, Access (VBA) or SQL server (TSQL)?
0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 36507068

You can create a split function, then use a loop control to populate your result table
ALTER FUNCTION [dbo].[Split] 
(
@String VARCHAR(8000), 
@Delimiter VARCHAR(1)
) 
RETURNS @Tokens table (Token VARCHAR(255)) 
AS 
BEGIN 
WHILE (CHARINDEX(@Delimiter,@String)>0) 
   BEGIN 
       INSERT INTO @Tokens (Token) VALUES
       (LTRIM(RTRIM(SUBSTRING(@String,1,CHARINDEX(@Delimiter,@String)-1)))) 
       SET @String = SUBSTRING(@String, CHARINDEX(@Delimiter,@String)+LEN(@Delimiter),LEN(@String)) 
   END 
   INSERT INTO @Tokens (Token) VALUES (LTRIM(RTRIM(@String)))
RETURN 
END

Open in new window

0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
LVL 32

Accepted Solution

by:
Ephraim Wangoya earned 2000 total points
ID: 36507151
you can use it in this manner
declare @test table(id int, value varchar(512))
declare @result table(id int, value varchar(255))

insert into @test 
select 1, '12,34,45,56,76' 
union all
select 2, '98,87,5'

declare @id integer, @values varchar(10)

select top 1 @id = ID from @test order by id

while exists(select 1 from @test where id >= @id)
begin
  select top 1 @id = Id, @values = value
  from @test
  where id >= @id
  order by id asc
  
  insert into @result
  select @id, Token
  from dbo.Split(@values, ',') A
  
  set @id = @id + 1
end

select * from @result

Open in new window

0
 
LVL 12

Expert Comment

by:Saurabh Bhadauria
ID: 36507880
At below you can find excellent way to generate a table from comma separated string..

http://www.sqlservercentral.com/articles/String+Manipulation/72540/

Let me know if  you have any doubts..
0
 
LVL 3

Expert Comment

by:hspoulsen
ID: 36509112
I think you should have a look at http://sommarskog.se/arrays-in-sql-2005.html

Erland is a MVP, and his entire homepage is worth spending quite a few hours on reading and understanding.

The link is pointing at a split function, which is implemented in many different ways, and compared to each other for speed, features and robustness.

HIH
Best regards,
Henrik Staun Poulsen, Stovi Software

0
 

Author Closing Comment

by:jamestieman
ID: 36510668
I appreciate everyone's advice, but ewangoya gave me a cut and paste solution that worked the first time.  
Thanks to all
Jamie
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
If you’re using QODBC to update QuickBooks data from Microsoft® Access but Access is not showing the updated data, you could have set up QODBC incorrectly.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question