July 29, 2010 at 9:55 am
Hi ,
I have two string columns that i have just imported from a flat file:
Columns:
LineDate varchar(40),
Date varchar(40)
Data:
Linedate Date
4/15/2005 4/29/2005 12:00:00 PM
I want to convert these two columns into datetime using this syntax convert(datetime,'linedate',101)
but i am getting this error:
Conversion failed when converting datetime from character string.
Any help?
July 29, 2010 at 10:00 am
DECLARE @TABLE AS TABLE(
Linedate VARCHAR(40),
Date VARCHAR(40))
INSERT INTO @TABLE
SELECT '4/15/2005', '4/29/2005 12:00:00 PM'
SELECT CONVERT(DATETIME, linedate, 101),
CONVERT(DATETIME, DATE, 101)
FROM @TABLE
July 29, 2010 at 10:12 am
the only difference i can see in the syntax is ' '
but i have tried and got this:
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
July 29, 2010 at 12:32 pm
looks like i don't have to convert a string into datetime format while inserting the rows to another table,sql server will automatically do it.
but i have some empty string fields which sql can't convert while inserting the column to another table.......
can anyone please tell me how should i convert empty string into empty datetime or null.
August 12, 2010 at 6:46 am
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value
Most common reason is the default language setting on your logins as this defines date format of either dd/mm/yyyy or mm/dd/yyyy
You can override this by using
SET DATEFORMAT mdy
can anyone please tell me how should i convert empty string into empty datetime or null.
CAST(NULLIF(@date,'') as datetime)
Far away is close at hand in the images of elsewhere.
Anon.
August 17, 2010 at 6:48 am
Thank you very much for the replies,and i thought i gave reply for this ,the real problem is with my data ..
Most of the date fields have the dates like: 07/29/0200,06/14/0210 etc.........
my destination column date will accept nulls ........so i have changed the data manually for dates less than 1900-01-01 and imported successfully...
August 17, 2010 at 7:04 am
This was removed by the editor as SPAM
August 17, 2010 at 7:10 am
you can use cast function to covnert it.
DECLARE @LineDate varchar(40), @Date varchar(40)
SET @LineDate = '4/15/2005'
SET @Date = '4/29/2005 12:00:00 PM'
SELECT CAST(@LineDate AS DATETIME), CAST(@Date AS DATETIME)
Abhijit - http://abhijitmore.wordpress.com
August 17, 2010 at 9:58 pm
This should work
SELECT CAST(@LineDate AS DATETIME), convert(datetime, convert(varchar,@date,101),101 )
(tweak the 101 to the kind of format you want).
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply