Error on BULK insert date [duplicate] - sql

This question already has an answer here:
Fun with BULK INSERT SQL SERVER - type mismatch or invalid character for the specified codepage
(1 answer)
Closed 9 years ago.
I am trying to BULK insert into sql server 2008 and i get an error on the Date column.
SET DATEFORMAT dmy
BULK
INSERT CustomSelection
FROM 'c:\test.csv'
WITH
(
FIRSTROW = 2,
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 2, column 1 (Date).
This is the Date in the .csv file: 18/08/2012.
Any ideas how to solve this ?

your date format is invalid thats way sql server giving error .
To solve these type of issue you have to follow following steps :
1. Import csv in database table , table column must be varchar it should not be datetime .
2. Then run following query to find invalid dates .
select inserteddate from table where isdate(inserteddate) = 0
3. then update those date manually or by update query something like :
UPDATE TABLE1
SET INSERTEDDATE = CONVERT(DATETIME , '18/08/2012' , 105)
WHERE INSERTEDDATE = '18/08/2012'

Related

Update of table not happening on datatype 'Text' [duplicate]

This question already has answers here:
WHERE clause on SQL Server "Text" data type
(7 answers)
Closed 5 years ago.
update SubCategoryMaster
set SubCategoryDescription='SHARPENERANDRULER'
where SubCategoryDescription='SharpenerAndRuler'
In this query, SubCategoryDescription's datatype is TEXT. When I execute this query, I get an error:
Msg 402, Level 16, State 1, Line 1
The data types text and varchar are incompatible in the equal to operator.
Please suggest
blog.sqlauthority.com has an answer for you. The following is the relevant part of the blog post, in case this link is dead:
Solution 1: Convert the data types to match with each other using CONVERT function.
Change the datatype of the MyText to nvarchar.
SELECT ID, MyText
FROM TestTable
WHERE CONVERT(NVARCHAR(MAX), MyText) = N'AnyText'
GO
Solution 2: Convert the data type of columns from NTEXT to NVARCHAR(MAX) (TEXT to VARCHAR(MAX)
ALTER TABLE TestTable
ALTER COLUMN MyText NVARCHAR(MAX)
GO
Now you can run the original query again and it will work fine.
Solution 3: Using LIKE command instead of Equal to command.
SELECT ID, MyText
FROM TestTable
WHERE MyText LIKE 'AnyText'
GO

Bulk insert error, when loading date format

I'm receiving a lot of .csv files that I need to bulk insert into a SQL table.
In the csv file the date format is in YYYY-MM-DD and a seperate column for time which is in format HH:MM:SS.
"2016-11-24";"01:00:16"
In my table I created the two columns as a date datatype and a time datatype and using this piece of code to insert:
BULK
INSERT [dbo].[table_name]
FROM 'filepath'
WITH
(
FIRSTROW = 2,
FIELDTERMINATOR = ';',
ROWTERMINATOR = '\n'
)
But I get this error:
Msg 4864, Level 16, State 1, Line 37
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 2, column 1 (date).
If I use a normal insert into statement it works...
First check your physical table date and time columns data type. It has datetime format, which means first you move all .csv file data to a temporary table with varchar datatype. After inserted in temporary table then move to physical table with use of datetime conversion.

Converting varchar datatype to datetime datatype using SQL 2012 management studio

I have a column which has ddmmmyyyy:hh:mm:ss.nnnnnn it is stored as varchar(25). I need to save it as datetime in the same column. I have tried using
update tablename
set columnname = (SUBSTRING(columnname,1,2) + '-' + SUBSTRING(columnname,3,3) + '-' +
SUBSTRING(columnname,6,4) + ' ' + SUBSTRING(columnname,11,8));
and then
alter table tablename
alter columnname datetime;
but later it shows up the error
Msg 242, Level 16, State 3, Line 1
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
How do I change it any other opinion or any modification for the above query. Please help. Thank you.
As per your given string format, you should use datetime2 data type
Your string format is almost correct, only 1 colon is extra after Year.
If you fix that thing, you can directly cast the varchar field into datetime2. For example first you can replace the extra colon with space by running following query,
UPDATE myTable
SET targetColumn = STUFF ( targetColumn , 10, 1, ' ')
-- ddmmmyyyy:hh:mm:ss.nnnnnn
-- \
-- this colon is extra which is at 10th position
After this you can directly ALTER your table and change the data type to datetime2.
Important: data in all the lines must contain valid date
Here is a test which shows how you can convert
CREATE TABLE testTable(testCol varchar(25));
INSERT INTO testTable(testCol)
VALUES('03Jan2014 18:33:39.999999');
ALTER TABLE testTable ALTER COLUMN testCol datetime2;
SELECT *
FROM testTable
DROP TABLE testTable;
It has already been answered here: Is there a way to convert a varchar to DATETIME in SQL SERVER 2008?
He uses: convert(datetime,'24/05/2012 09:56:06',103)
Although you might have to do some substrings to adapt to a format covered by convert: http://www.sql-server-helper.com/tips/date-formats.aspx
Add a new column
alter table t
add n datetime
Update the new column
update t
set n = datetimefromparts(
cast(substring(o,6,4) as int),
case substring(o,3,3)
when 'jan' then 1
...
when 'dec' then 12
end,
cast(substring(o,1,2) as int),
cast(substring(o,11,2) as int),
cast(substring(o,14,2) as int),
cast(substring(o,17,2) as int),
cast(substring(o,20,6) as int)
)
If you need to drop the old column
alter table t
drop column o

error Bulk load data conversion error (truncation) when inserting data from a text file

got about 20 to 50 rows in a text file to insert into my database 'bourse' using sql server management studio 2012 my database 'bourse contains certain number of tables for example i have the table IB_emetteur it has 3 attributes (c_emetteur , libelle , mnemes )
and i have the data in a text file structured like this :
1,UFI,U.F.I
2,TSI, T.S.I
3,ADAY,A.Day
5,CAPITAL,C.PITAL
7,COFCAP,COFjuil
8,SFI,SUIhyuo
9,AFC,A.KIYUI
13,CGI,chakoqguio
14,BNAC,banque hyuijsii
i have to insert this data in my table so i used this query :
bulk insert [dbo].[IB_Emetteur]
from 'C:\Users\Manu\Documents\GL5\Finance\liste_emetteur.txt'
with (fieldterminator = ',', rowterminator = '\n')
go
but i got this error
Bulk load data conversion error (truncation) for row 1, column 2 (mnemes)
Try
bulk insert [dbo].[IB_Emetteur]
from 'C:\Users\Manu\Documents\GL5\Finance\liste_emetteur.txt'
with (fieldterminator = ',', rowterminator = '\n',FIRSTROW = 2)
go

Inserting Dates with BULK INSERT

I have a CSV file, which contains three dates:
'2010-07-01','2010-08-05','2010-09-04'
When I try to bulk insert them...
BULK INSERT [dbo].[STUDY]
FROM 'StudyTable.csv'
WITH
(
MAXERRORS = 0,
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
I get an error:
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 1, column 1 (CREATED_ON).
So I'm assuming this is because I have an invalid date format. What is the correct format to use?
EDIT
CREATE TABLE [dbo].[STUDY]
(
[CREATED_ON] DATE,
[COMPLETED_ON] DATE,
[AUTHORIZED_ON] DATE,
}
You've got quotes (') around your dates. Remove those and it should work.
Does your data file have a header record? If it does, obviously your table names will not be the correct data type, and will fail when SQL Server tries to INSERT them into your table. Try this:
BULK INSERT [dbo].[STUDY]
FROM 'StudyTable.csv'
WITH
(
MAXERRORS = 0,
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n',
FIRSTROW = 2
)
According to MSDN the BULK INSERT operation technically doesn't support skipping header records in the CSV file. You can either remove the header record or try the above. I don't have SQL Server in front of me at the moment, so I have not confirmed this works. YMMV.