-2

I have a dataset more than 1000 when import into SSMS. Thus I use the following T-SQL to create table and import data:

CREATE TABLE sales(
  SPID text,
  GeoID text,
  PID text,
  SaleDate smalldatetime,
  Amount int,
  Customers int,
  Boxes int);

BULK INSERT sales
FROM 'C:\Users\alvin_zoj6s4b\Downloads\sales.txt'
WITH (FIELDQUOTE = '\' 
      , FIELDTERMINATOR = ','
      , ROWTERMINATOR = '\n');

I got the following error: erroe

The problem is on the SalesDate column so I change from smalldatetime to Text on create table query:

CREATE TABLE sales(
  SPID text,
  GeoID text,
  PID text,
  SaleDate TEXT,
  Amount int,
  Customers int,
  Boxes int);

The result of the table showing ' ' on the text data type (SPID, GeoID, PID & SaleDate): error2

Here is the structure of my text file stored in my laptop: textfile

The 2 problems I would like to clarify:

  1. Text data type should put ' ' in the raw data when import the data to SSMS but why my output will showing ' '?
  2. The SaleDate only work when I change it to text, anything I have done wrongly?
Thom A
  • 88,727
  • 11
  • 45
  • 75
Azul
  • 25
  • 8
  • What version of SQL Server are you using? – Thom A Aug 25 '22 at 12:24
  • @Larnu I am using SSMS 2018 – Azul Aug 25 '22 at 12:27
  • What version of SQL Server? There's no such thing as SSMS 2018, and SSMS isn't SQL Server; it's a an IDE like application for SQL Server. What does `PRINT @@VERSION;` return? – Thom A Aug 25 '22 at 12:28
  • Also, why are you using `text`? That data type has been deprecated since **2005**; *17 years* ago. – Thom A Aug 25 '22 at 12:36
  • @Larnu oh, I am using Microsoft SQL Server 2019 (RTM) - 15.0.2000.5 (X64) – Azul Aug 25 '22 at 12:42
  • @Larnu do you mean why I am using text file to import? I saw online suggest it is work and my raw data is having many rows and columns, too manual to arrange the data into proper column in excel. Do you have any suggestion for me? – Azul Aug 25 '22 at 12:46
  • You should really get that updated; you've failed to apply almost 3 years of security and bug fixes. As for the problem, you need to let SQL Server know it's on CSV format with the `FORMAT` parameter, and let it know that the quote identifier is a single quote (`'`) rather than the default double quotes (`"`). – Thom A Aug 25 '22 at 12:46
  • *"do you mean why I am using text file to import?"* No I am asking why you are using the *deprecated* `text` data type. – Thom A Aug 25 '22 at 12:47
  • @Larnu Text data type is still exist according to official documentation https://learn.microsoft.com/en-us/sql/t-sql/data-types/data-types-transact-sql?view=sql-server-ver16 I have converted the txt file to csv. When I open the csv, the text data type is having ' ', so should I replace all to no ' ' in the Excel file? How about the SaleDate issue? – Azul Aug 25 '22 at 12:54
  • I didn't say it didn't exist, I said it it **deprecated** and has been for *17 years*; you should **not** be using it anymore; especially on new development work. Because it's still in SQL Server isn't a reason to use it, much like using a hammer to put a screw into wood isn't an excuse because it was the first tool in arm reach. – Thom A Aug 25 '22 at 12:57
  • 1
    Note from that [documentation](https://learn.microsoft.com/en-us/sql/t-sql/data-types/ntext-text-and-image-transact-sql?view=sql-server-ver16) it states: *"**Important:** The ntext, text, and image data types will be removed in a future version of SQL Server. Avoid using these data types in new development work, and plan to modify applications that currently use them."* – Thom A Aug 25 '22 at 12:58
  • @Larnu I have changed the text date type to nvarchar(255). SaleDate column still need to use nvarchar(255) to have no error and all the nvarchar will have ' '. What I should do now? – Azul Aug 25 '22 at 13:11

1 Answers1

0

As I mentioned in the comments, you need to tell SQL Server that the file is a CSV file, with FORMAT and tell it the quote identifier is a single quote (') with FIELDQUOTE, as the default is a double quote ("). You had, for some reason, defined the FIELDQUOTE as '\', yet there isn't a single \ in your images.

You also need to fix those data types; text has been deprecated for 17 years and so you shouldn't be using it. I have used lengths based on your image of data (Please do not upload images of code/data/errors when asking a question.). I also use a date for your column SaleDate as all the times are at midnight; if they do have a time portion, then you might want a datetime2 (as there's no milliseconds in the data a datetime2(0) would make sense).

This gives the following:

CREATE TABLE dbo.sales (SPID varchar(4), --guessed length, but I doubt you need MAX for values that will have 8,000+ characters
                        GeoID varchar(2), --guessed length, but I doubt you need MAX for values that will have 8,000+ characters
                        PID varchar(3), --guessed length, but I doubt you need MAX for values that will have 8,000+ characters
                        SaleDate date, --Considering all your columns are mid night, this seems a better choice. Otherwise use datetime2(0)
                        Amount int,
                        Customers int,
                        Boxes int);

GO
BULK INSERT dbo.sales
FROM 'C:\Users\alvin_zoj6s4b\Downloads\sales.txt'
WITH (FIELDTERMINATOR = ',',
      ROWTERMINATOR = '\n',
      FORMAT = 'CSV',
      FIELDQUOTE = '''');
Thom A
  • 88,727
  • 11
  • 45
  • 75
  • thank you for the hints. Just to update the code a bit BULK INSERT sales FROM 'C:\Users\alvin_zoj6s4b\Downloads\sales.txt' WITH ( FIELDTERMINATOR = ',', ROWTERMINATOR = '\n', FORMAT = 'CSV', FIELDQUOTE = ''''); – Azul Aug 25 '22 at 13:55