I am having trouble importing date data into SQL Server from a csv file using bulk insert.
I'm using the following stored procedure:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[usp_ImportTestData]
@Filepath varchar(500),
@Pattern varchar(100),
@TableName varchar(128),
@ViewName varchar(128),
@ResetTable bit = 0
AS
SET QUOTED_IDENTIFIER OFF
DECLARE @query varchar(1000)
DECLARE @numfiles int
DECLARE @filename varchar(100)
DECLARE @files TABLE (Name varchar(200) NULL)
--Delete the contents of the rawData table and let the user know
IF @ResetTable = 1
BEGIN
PRINT 'Emptying table [' + @TableName + ']...'
EXEC ('DELETE ' + @TableName)
END
--Pull a list of the CSV file names from the folder that they're stored in
SET @query ='master.dbo.xp_cmdshell "dir '+@filepath+@pattern +' /b"'
INSERT @files(Name)
EXEC (@query)
DECLARE curs_files CURSOR FOR
SELECT Name
FROM @files
WHERE Name IS NOT NULL
--For each CSV file, execute a query
SET @numfiles = 0
OPEN curs_files
FETCH NEXT FROM curs_files INTO @filename
WHILE (@@FETCH_STATUS = 0)
BEGIN
SET @numfiles += 1
-- BULK INSERT each CSV file into the rawData view and update the rawData
-- table with the file name and the upload datetime
SET @query = ('BULK INSERT ' + @ViewName + ' FROM ''' + @Filepath+@filename + ''' WITH(FIRSTROW = 2, FIELDTERMINATOR='','', ROWTERMINATOR=''\n'');'
+ ' UPDATE ' + @TableName
+ ' SET [FileName] = ' + '''' + @filename + ''''
+ ' WHERE [FileName] Is Null;'
+ ' UPDATE ' + @TableName
+ ' SET [UploadDatetime] = ' + '''' + CAST(GETDATE() as nvarchar(1000)) + ''''
+ ' WHERE [UploadDatetime] Is Null;'
)
PRINT 'Importing [' + @filename + '] into [' + @TableName + ']...'
EXEC (@query)
FETCH NEXT FROM curs_files INTO @filename
END
CLOSE curs_files
DEALLOCATE curs_files
Please note I got this code from http://www.decisivedata.net/blog/import-data-from-multiple-csv-files-using-bulk-insert.
This is my table:
[dbo].[Test]
(
[Money] [float] NULL,
[Amount] [varchar](50) NULL,
[Date] [datetime] NULL,
[DataID] [int] IDENTITY(1,1) NOT NULL,
[FileName] [varchar](50) NULL,
[UploadDatetime] [datetime] NULL
)
This is my view
SELECT Money, Amount, Date
FROM dbo.Test
Here is a sample of one of my csv files:
Money, Amount, Date,
1043333333.5,4, 01/29/2018
57,4, 01/29/2018
604,3,01/29/2018
Here is my query:
EXEC usp_ImportTestData
@filepath = 'C:\TestDataCSV\',
@pattern = '*.csv',
@TableName = 'Test',
@ViewName = 'V_Test',
@ResetTable = 1
When I run the query I get this error message
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 3, column 3 (Date)
I have no idea what is wrong! If I try to do a straight bulk insert for just one file it doesn't seem to have any trouble with the date field. If I get rid of the date column it seems to work fine too..