casperkc
casperkc

Reputation: 81

Splitting a string and insert into a database

I've setup a SqlServer Express and created the tables i need. Here's however where I run into a problem.

I have this textfile containing lot's of different info, here's and example:

Line1: 00001, Some name, Address, Email
Line2: 00002.. 

How do I split lines, which are all divided with commas between the relevant information and move the information into the correct columns in my database?

ID      | Name        | Address      | Email    |   <-- Columns

000001  | Some name   | Some street  | @mail    |   <-- Data inserted

000002 etc.

I hope you understand my question, I did my best to visualize it.

Thanks in advance.

Upvotes: 1

Views: 648

Answers (1)

anthonyvscode
anthonyvscode

Reputation: 995

http://midnightprogrammer.net/post/Import-CSV-File-Into-SQL-Server-Using-Bulk-Insert.aspx

BULK INSERT Contact
FROM 'c:\TestData.csv'  -- Full path of the Delimited file
WITH
(
FIELDTERMINATOR = ',', --CSV field delimiter
ROWTERMINATOR = '\n'   --Use to shift the control to next row
)

or if you're just after a once off import right click your db in management studio -> tasks -> import data, choose flat file source and follow the wizard

Upvotes: 4

Related Questions