I have a csv file and i need to import it to a table in sql 2005 or 2008. The column names and count in the csv are different from the table column names and count. The csv is splitted by a ';' .
Example
CSV FILEcontents:
FirstName;LastName;Country;Age
Roger;Mouthout;Belgium;55
SQL Person Table
Columns: FName,LName,Country
You can use a format file when importing with bcp:
Create a format file for your table:
Edit the import file. The trick is to add a dummy row for the field you want to skip, and add a '0' as server column order.
Then import the data using this format file, specifying your inputfile, this format file and the seperator:
For info, with the same structure, you can use this kind of statement:
See example
I'd create a temporary table, bulk insert the lot, select into the new table what you need and drop the temporary table.
Something like
I now prefer to use XML format files like this with BULK INSERT or OPENROWSET:
Then you can use the server-side BULK INSERT command as follows:
alternatively, if you want to modify the data 'in-flight', you can use the