I am looking for help to import a .csv
file into SQL Server using BULK INSERT
and I have few basic questions.
Issues:
Here's how I would solve it:
Just Save your CSV File as a XLS Sheet in excel(By Doing so, you wouldn't have to worry about delimitiers. Excel's spreadsheet format will be read as a table and imported directly into a SQL Table)
Import the File Using SSIS
Write a Custom Script in the import manager to omit/modify the data you're looking for.(Or run a master script to scrutinize the data you're looking to remove)
Good Luck.
You first need to create a table in your database in which you will be importing the CSV file. After the table is created, follow the steps below.
• Log into your database using SQL Server Management Studio
• Right click on your database and select Tasks -> Import Data...
• Click the Next >
button
• For the Data Source, select Flat File Source
. Then use the Browse button to select the CSV file. Spend some time configuring how you want the data to be imported before clicking on the Next >
button.
• For the Destination, select the correct database provider (e.g. for SQL Server 2012, you can use SQL Server Native Client 11.0). Enter the Server name. Check the Use SQL Server Authentication
radio button. Enter the User name, Password, and Database before clicking on the Next >
button.
• On the Select Source Tables and Views window, you can Edit Mappings before clicking on the Next >
button.
• Check the Run immediately
check box and click on the Next >
button.
• Click on the Finish
button to run the package.
The above was found on this website (I have used it and tested):
The best, quickest and easiest way to resolve the comma in data issue is to use Excel to save a comma separated file after having set Windows' list separator setting to something other than a comma (such as a pipe). This will then generate a pipe (or whatever) separated file for you that you can then import. This is described here.
Because they do not use the SQL import wizard, the steps would be as follows:
Right click on the database in the option tasks to import data,
Once the wizard is open, we select the type of data to be implied. In this case it would be the
Flat file source
We select the CSV file, you can configure the data type of the tables in the CSV, but it is best to bring it from the CSV.
SQL client
Depending on our type of authentication we select it, once this is done, a very important option comes.
Enable id insert
(usually not starting from 1), instead if we have a column with the id in the CSV we select the enable id insert, the next step is to end the wizard, we can review the changes here.
On the other hand, in the following window may come alerts, or warnings the ideal is to ignore this, only if they leave error is necessary to pay attention.
This link has images.
I know that there are accepted answer but still, I want to share my scenario that maybe help someone to solve their problem TOOLS
SCENARIO
i was loading the dataset which's in CSV format which was later to be shown on the View
i tried to use the bulk load but I's unable to load as BULK LOAD
was using
FIELDTERMINATOR = ','
and Excel cell was also using ,
however, I also couldn't use Flat file source
directly because I was using Code-First Approach
and doing that only made model in SSMS DB, not in the model from which I had to use the properties later.
SOLUTION
Add-Migration
in NuGet package console Import the file into Excel by first opening excel, then going to DATA, import from TXT File, choose the csv extension which will preserve 0 prefixed values, and save that column as TEXT because excel will drop the leading 0 otherwise (DO NOT double click to open with Excel if you have numeric data in a field starting with a 0 [zero]). Then just save out as a Tab Delimited Text file. When you are importing into excel you get an option to save as GENERAL, TEXT, etc.. choose TEXT so that quotes in the middle of a string in a field like YourCompany,LLC are preserved also...
BULK INSERT dbo.YourTableName
FROM 'C:\Users\Steve\Downloads\yourfiletoIMPORT.txt'
WITH (
FirstRow = 2, (if skipping a header row)
FIELDTERMINATOR = '\t',
ROWTERMINATOR = '\n'
)
I wish I could use the FORMAT and Fieldquote functionality but that does not appear to be supported in my version of SSMS