How use Bulk insert csv to sql server with datetime format correct?

ぐ巨炮叔叔 提交于 2019-12-11 04:46:26

问题


i want use bulk insert file csv insert to SQL Server 2012. same column have datetime but use bulk insert datetime format not work and i not use SSIS.

Example Create Table

CREATE TABLE [dbo].[scanindex_test](
    [request_no] [varchar](13) NOT NULL,
    [request_date] [datetime] NULL,
    [id_card] [varchar](20) NULL,
    [firstname] [varchar](100) NULL,
    [surname] [varchar](100) NULL
)

Query Sql Server 2012:

declare 
    @path      varchar(255),
    @sql       varchar(5000)           

SET @path = 'C:\Test\TESTFILE.csv'    

set @sql = 'BULK INSERT [dbo].[scanindex_test] FROM ''' + @path + ''' 
      ' + '     WITH (      
                CODEPAGE=''RAW'',           
                FIELDTERMINATOR = '','', 
                ROWTERMINATOR = ''\n''
                ) '
print @sql
exec (@sql)

when i run query it's error:

Msg 4864, Level 16, State 1, Line 1
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 11, column 2 (request_date).
Msg 4865, Level 16, State 1, Line 1
Cannot bulk load because the maximum number of errors (10) was exceeded.
Msg 7399, Level 16, State 1, Line 1
The OLE DB provider "BULK" for linked server "(null)" reported an error. The provider did not give any information about the error.
Msg 7330, Level 16, State 2, Line 1
Cannot fetch a row from OLE DB provider "BULK" for linked server "(null)".

Example Data in CSV File

request_no | request_date  | id_card  | firstname    | surname
1          | 12/7/2017     | 1122     | AA           | BB
2          | 12/7/2017     | 4399     | SS           | EE
3          | 13/7/2017     | 5599     | QQ           | KK     

Result when run query:

request_no | request_date            | id_card  | firstname | surname
1          | 2017-12-07 00:00:00.000 | 1122     | AA        | BB
2          | 2017-12-07 00:00:00.000 | 4399     | SS        | EE
3  >> Error, because it's look datetime format: 2017-13-07 (yyyy-mm-dd)

but I want result datetime format (YYYY-MM-DD) correct:

request_no | request_date            | id_card | firstname | surname
1          | 2017-07-12 00:00:00.000 | 1122    | AA        | BB
2          | 2017-07-12 00:00:00.000 | 4399    | SS        | EE
3          | 2017-07-13 00:00:00.000 | 5599    | QQ        | KK

Please Help me. Thanks advance ;)


回答1:


You need to change the DATEFORMAT to DMY. Adding the following to the top of your script should work:

SET DATEFORMAT DMY;

So your full script should be:

SET DATEFORMAT DMY;

declare 
    @path      varchar(255),
    @sql       varchar(5000)           

SET @path = 'C:\Test\TESTFILE.csv'    

set @sql = 'BULK INSERT [dbo].[scanindex_test] FROM ''' + @path + ''' 
      ' + '     WITH (      
                CODEPAGE=''RAW'',           
                FIELDTERMINATOR = '','', 
                ROWTERMINATOR = ''\n''
                ) '
print @sql
exec (@sql)


来源:https://stackoverflow.com/questions/45217560/how-use-bulk-insert-csv-to-sql-server-with-datetime-format-correct

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!