1

I am trying to upload dummy data which would help in uploading bigger files. The dummy data is as follows:

+---------+-------+----------+------------+------+---------------------------------------------------------------+
|  Name   | Class | Subject  |  ExamDate  | Mark |                           Description                         |
+---------+-------+----------+------------+------+---------------------------------------------------------------+
| Prabhat |     4 | "Math"   | 02/10/2013 |   25 |    "Test data for prabhat.'                                     |
| Murari  |     5 |"Science" | 02/11/2013 |   24 |    "Test data for his's test, where we can test 2nd ROW, Test." |
| sanjay  |     4 | "Science"|            |   25 | "Test Only."                                                    |
| xyz     |     5 |"Science,"|            |      |       ""                                                        |
+---------+-------+----------+------------+------+---------------------------------------------------------------+

The problem that forces me to upload data through BULK INSERT is that I do not want to change the data type of ExamDate column to date after uploading it.

Therefore, I created a table and uploaded data through bulk insert using following piece of code:

CREATE TABLE test_data (
[Name] NVARCHAR(MAX) NULL,
[Class] NVARCHAR(MAX) NULL,
[Subject] NVARCHAR(MAX) NULL,
[ExamDate] date NULL,
[Mark] INT NULL,
[Description] NVARCHAR(MAX) NULL 
)


BULK INSERT test_data
FROM 'D:\TP - Churn\text.csv'
WITH
(
    FIRSTROW = 2,
    FORMAT = 'CSV',
    FIELDQUOTE = '"',
    FIELDTERMINATOR = '"\"',  --CSV field delimiter
    ROWTERMINATOR = '\n',   --Use to shift the control to next row
    TABLOCK

)

The above query gives the an error as follows Incorrect syntax near 'FORMAT'. Removing FORMAT = 'CSV' line and executing would give the same error but this time with FIELDQUOTE.

The point of introducing FIELDQUOTE = '"' is to deal with the last row, where in subject column there is a , after science. This entry in csv looks like "Science," .

As per Incorrect syntax near format in BULK INSERT? I think we can not use FIELDQUOTE while using SQL Server 2016.

Edit: So far I've managed to this with a format file as follows:

If that is true than how do we deal with the issue stated above?

11.0
6
1   SQLCHAR   0  8000  ","      1     Name          SQL_Latin1_General_CP1_CI_AS
2   SQLCHAR   0  40    ","      2     Class         SQL_Latin1_General_CP1_CI_AS
3   SQLCHAR   0  40    "\","    3     Subject       SQL_Latin1_General_CP1_CI_AS
4   SQLCHAR   0  40    ","      4     ExamDate      SQL_Latin1_General_CP1_CI_AS
5   SQLCHAR   0  40    ","      5     Mark          SQL_Latin1_General_CP1_CI_AS
6   SQLCHAR   0  1000  "\"\r\n" 6     Description   SQL_Latin1_General_CP1_CI_AS

The results are as follow:

+---------+-------+-----------+------------+------+-------------------------------------------------------------+
|  Name   | Class |  Subject  |  ExamDate  | Mark |                         Description                         |
+---------+-------+-----------+------------+------+-------------------------------------------------------------+
| Prabhat |     4 | "Math     | 10/02/2013 | 25   | "Test data for prabhat.                                     |
| Murari  |     5 | "Science  | 11/02/2013 | 24   | "Test data for his's test, where we can test 2nd ROW, Test. |
| sanjay  |     4 | "Science  | NULL       | 25   | "Test Only.                                                 |
| xyz     |     5 | "Science, | NULL       | NULL | "                                                           |
+---------+-------+-----------+------------+------+-------------------------------------------------------------+

The quotes at the beginning are not being removed in both Subject & Description column. Furthermore, last row and last column was empty but in the results its not NULL.

Result of file opened in notepad:

Prabhat,4,"Math",02/10/2013,25,"Test data for prabhat."
Murari,5,"Science",02/11/2013,24,"Test data for his's test, where we can test 2nd ROW, Test."
sanjay,4,"Science",,25,"Test Only."
xyz,5,"Science,",,,""
Furqan Hashim
  • 1,304
  • 2
  • 15
  • 35
  • 1
    `FIELDQUOTE` was added in SQL Server 2017, so no, it's not available in SQL Server 2016. Have you considered changing your file to something else (such as Pipe Delimited), or using an ETL tool (like SSIS)? – Thom A Oct 02 '18 at 11:02
  • This is an example file which can easily be converted to Pipe Delimited but I want to do this task on 3-4 bigger files which would be costly. Haven't used an ETL tool. – Furqan Hashim Oct 02 '18 at 11:20
  • Does your file really look exactly like that? Does it really have all those `+`, `-`, `|` in it? If your file really looks like that you can treat it as pipe delimited or fixed width. If your file doesn't really look like that, you need to post what we are actually working with. Can you clarify: `ExamDate` will eventually reside in a field of type `date` right? Never store dates as strings – Nick.Mc Oct 02 '18 at 13:44
  • ExamDate will reside in a field of type date. There might be NULLS in ExamDate. Moreover, there won't be any +, -, or | but you can expect , as demonstrated in last row 3rd column. – Furqan Hashim Oct 02 '18 at 15:08
  • There is no point pursuing this question unless you can describe the file format. Is it delimited? Is it fixed width? Is it a text file? Excel? Please edit your question and describe the actual format, for example is it CSV? CSV (comma seperated) is both the most widely used and yet the most error prone. Tab or pipe delimited is far more reliable due to the comma issue you describe – Nick.Mc Oct 03 '18 at 04:03
  • In a nutshell the problem is to define text qualifier as " while using using bulk insert query. – Furqan Hashim Oct 03 '18 at 04:57
  • But your posted sample data has no `"` in it anywhere. – Nick.Mc Oct 04 '18 at 01:55
  • Sorry my bad, I've made some edits. – Furqan Hashim Oct 04 '18 at 06:12
  • Your posted sample is still pipe delimited - is that what you're loading? Open your source file in notepad and paste whats in there. Don't try formatting it. – Nick.Mc Oct 04 '18 at 06:41

0 Answers0