dbaspot
Tags Register FAQ Calendar Search Today's Posts Mark Forums Read

Import CSV file to SQL table - sqlserver-faq

This is a discussion on Import CSV file to SQL table - sqlserver-faq ; Greetings, How do I import data from CSV file (source.csv) to my MSSQL table (dbo.target)? Kindly advise. Thanks ascll...


Home > Database Forum > Microsoft SQL Server > sqlserver-faq > Import CSV file to SQL table

Reply

 

LinkBack Thread Tools Display Modes
  #1  
Old 06-28-2007, 04:31 AM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Import CSV file to SQL table

Greetings,

How do I import data from CSV file (source.csv) to my MSSQL table
(dbo.target)?

Kindly advise.

Thanks
ascll



Reply With Quote
  #2  
Old 06-28-2007, 07:43 AM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: Import CSV file to SQL table

Check out the following topics in the BOL:

BULK INSERT
bcp.exe

--
Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
SQL Server MVP
Toronto, ON Canada
https://mvp.support.microsoft.com/profile/Tom.Moreau


"nelson" wrote in message
news:%23GYOu6VuHHA.2272@TK2MSFTNGP04.phx.gbl...
Greetings,

How do I import data from CSV file (source.csv) to my MSSQL table
(dbo.target)?

Kindly advise.

Thanks
ascll



Reply With Quote
  #3  
Old 06-28-2007, 09:48 AM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: Import CSV file to SQL table

I have two csv files that I need import into MSSQL every 30 minutes. One csv
I am able to perform an INSERT INTO table (column1, column2, etc) SELECT
column1, column2, etc. FROM OPENROWSET('MSDASQL', 'Driver={Microsoft Text
Driver (*.txt; *.csv)};DBQ=C:\DataFiles;', 'SELECT * from Materials.csv'),
however the other file has two columns with many invalid dates such as
00/00/0000 or 04/11/0704 (the first is output by SAP and the other is
fat-fingered). I know I need to read the file, but I am stuck as to how to
parse through it and remove any bad dates. BULK COPY won't help me on this.

Mike

"Tom Moreau" wrote:

> Check out the following topics in the BOL:
>
> BULK INSERT
> bcp.exe
>
> --
> Tom
>
> ----------------------------------------------------
> Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
> SQL Server MVP
> Toronto, ON Canada
> https://mvp.support.microsoft.com/profile/Tom.Moreau
>
>
> "nelson" wrote in message
> news:%23GYOu6VuHHA.2272@TK2MSFTNGP04.phx.gbl...
> Greetings,
>
> How do I import data from CSV file (source.csv) to my MSSQL table
> (dbo.target)?
>
> Kindly advise.
>
> Thanks
> ascll
>
>
>
>

Reply With Quote
  #4  
Old 06-28-2007, 03:40 PM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: Import CSV file to SQL table

In that case, you'll need to import the file into a staging table, with a
varchar column instead of datetime. Then, you can do an INSERT SELECT into
the good table, filtering out the bad stuff in the WHERE clause.

--
Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
SQL Server MVP
Toronto, ON Canada
https://mvp.support.microsoft.com/profile/Tom.Moreau


"Mike D" wrote in message
news:53F2CB48-AC63-4E4A-9C08-B202080E6D43@microsoft.com...
I have two csv files that I need import into MSSQL every 30 minutes. One
csv
I am able to perform an INSERT INTO table (column1, column2, etc) SELECT
column1, column2, etc. FROM OPENROWSET('MSDASQL', 'Driver={Microsoft Text
Driver (*.txt; *.csv)};DBQ=C:\DataFiles;', 'SELECT * from Materials.csv'),
however the other file has two columns with many invalid dates such as
00/00/0000 or 04/11/0704 (the first is output by SAP and the other is
fat-fingered). I know I need to read the file, but I am stuck as to how to
parse through it and remove any bad dates. BULK COPY won't help me on this.

Mike

"Tom Moreau" wrote:

> Check out the following topics in the BOL:
>
> BULK INSERT
> bcp.exe
>
> --
> Tom
>
> ----------------------------------------------------
> Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
> SQL Server MVP
> Toronto, ON Canada
> https://mvp.support.microsoft.com/profile/Tom.Moreau
>
>
> "nelson" wrote in message
> news:%23GYOu6VuHHA.2272@TK2MSFTNGP04.phx.gbl...
> Greetings,
>
> How do I import data from CSV file (source.csv) to my MSSQL table
> (dbo.target)?
>
> Kindly advise.
>
> Thanks
> ascll
>
>
>
>


Reply With Quote
  #5  
Old 06-29-2007, 11:46 AM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: Import CSV file to SQL table

Thanks Tom for the reply. Would I use the same SqlBulkCopy but to a
non-esisting table or do I have to create it first?
--
Regards,

Mike D


"Tom Moreau" wrote:

> In that case, you'll need to import the file into a staging table, with a
> varchar column instead of datetime. Then, you can do an INSERT SELECT into
> the good table, filtering out the bad stuff in the WHERE clause.
>
> --
> Tom
>
> ----------------------------------------------------
> Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
> SQL Server MVP
> Toronto, ON Canada
> https://mvp.support.microsoft.com/profile/Tom.Moreau
>
>
> "Mike D" wrote in message
> news:53F2CB48-AC63-4E4A-9C08-B202080E6D43@microsoft.com...
> I have two csv files that I need import into MSSQL every 30 minutes. One
> csv
> I am able to perform an INSERT INTO table (column1, column2, etc) SELECT
> column1, column2, etc. FROM OPENROWSET('MSDASQL', 'Driver={Microsoft Text
> Driver (*.txt; *.csv)};DBQ=C:\DataFiles;', 'SELECT * from Materials.csv'),
> however the other file has two columns with many invalid dates such as
> 00/00/0000 or 04/11/0704 (the first is output by SAP and the other is
> fat-fingered). I know I need to read the file, but I am stuck as to how to
> parse through it and remove any bad dates. BULK COPY won't help me on this.
>
> Mike
>
> "Tom Moreau" wrote:
>
> > Check out the following topics in the BOL:
> >
> > BULK INSERT
> > bcp.exe
> >
> > --
> > Tom
> >
> > ----------------------------------------------------
> > Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
> > SQL Server MVP
> > Toronto, ON Canada
> > https://mvp.support.microsoft.com/profile/Tom.Moreau
> >
> >
> > "nelson" wrote in message
> > news:%23GYOu6VuHHA.2272@TK2MSFTNGP04.phx.gbl...
> > Greetings,
> >
> > How do I import data from CSV file (source.csv) to my MSSQL table
> > (dbo.target)?
> >
> > Kindly advise.
> >
> > Thanks
> > ascll
> >
> >
> >
> >

>
>

Reply With Quote
  #6  
Old 06-29-2007, 05:14 PM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: Import CSV file to SQL table

The table must exist first.

--
Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
SQL Server MVP
Toronto, ON Canada
https://mvp.support.microsoft.com/profile/Tom.Moreau


"Mike D" wrote in message
news:CF9CDAE3-D384-4864-B894-F24CDA8EAD9C@microsoft.com...
Thanks Tom for the reply. Would I use the same SqlBulkCopy but to a
non-esisting table or do I have to create it first?
--
Regards,

Mike D


"Tom Moreau" wrote:

> In that case, you'll need to import the file into a staging table, with a
> varchar column instead of datetime. Then, you can do an INSERT SELECT
> into
> the good table, filtering out the bad stuff in the WHERE clause.
>
> --
> Tom
>
> ----------------------------------------------------
> Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
> SQL Server MVP
> Toronto, ON Canada
> https://mvp.support.microsoft.com/profile/Tom.Moreau
>
>
> "Mike D" wrote in message
> news:53F2CB48-AC63-4E4A-9C08-B202080E6D43@microsoft.com...
> I have two csv files that I need import into MSSQL every 30 minutes. One
> csv
> I am able to perform an INSERT INTO table (column1, column2, etc) SELECT
> column1, column2, etc. FROM OPENROWSET('MSDASQL', 'Driver={Microsoft Text
> Driver (*.txt; *.csv)};DBQ=C:\DataFiles;', 'SELECT * from Materials.csv'),
> however the other file has two columns with many invalid dates such as
> 00/00/0000 or 04/11/0704 (the first is output by SAP and the other is
> fat-fingered). I know I need to read the file, but I am stuck as to how
> to
> parse through it and remove any bad dates. BULK COPY won't help me on
> this.
>
> Mike
>
> "Tom Moreau" wrote:
>
> > Check out the following topics in the BOL:
> >
> > BULK INSERT
> > bcp.exe
> >
> > --
> > Tom
> >
> > ----------------------------------------------------
> > Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
> > SQL Server MVP
> > Toronto, ON Canada
> > https://mvp.support.microsoft.com/profile/Tom.Moreau
> >
> >
> > "nelson" wrote in message
> > news:%23GYOu6VuHHA.2272@TK2MSFTNGP04.phx.gbl...
> > Greetings,
> >
> > How do I import data from CSV file (source.csv) to my MSSQL table
> > (dbo.target)?
> >
> > Kindly advise.
> >
> > Thanks
> > ascll
> >
> >
> >
> >

>
>


Reply With Quote
  #7  
Old 07-22-2009, 03:33 PM
Database Newbie
 
Join Date: Jul 2009
Posts: 1
tommonline is on a distinguished road
Default Re: Import CSV file to SQL table

Try http://www.sqlscripter.com to import text/csv data. There's a Csv to SQL converter and the data will be validated as well.

Cheers
Reply With Quote
Reply

Thread Tools
Display Modes



All times are GMT -4. The time now is 05:55 AM.