September 14, 2012 at 10:53 am
I am trying to convert varchar data '27/04/2012'( dd/mm/yyyy) to date column as like below conversion
cast (Processing_Date as date).
But it giving error like 'Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string'
Seems to be varchar data is changing into yyyy/mm/dd(2012/27/04) after conversion.thats is the problem. i.e month and day value changed.
Please help to resolve the issue by correct format before loading data?
Any help will be appreciated!!!!!!!!!
Regards,
Kumar
September 14, 2012 at 10:59 am
KumarSQLDBA (9/14/2012)
I am trying to convert varchar data '27/04/2012'( dd/mm/yyyy) to date column as like below conversioncast (Processing_Date as date).
But it giving error like 'Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string'
Seems to be varchar data is changing into yyyy/mm/dd(2012/27/04) after conversion.thats is the problem. i.e month and day value changed.
Please help to resolve the issue by correct format before loading data?
Any help will be appreciated!!!!!!!!!
The default string format for converting a character string to date is YYYY-MM-DD. try this:
select convert(date, Processing_Date, 103)
September 14, 2012 at 12:08 pm
Read this link for reasons why Lynn chose 103 and what all those numbers mean:
September 14, 2012 at 12:19 pm
SET DATEFORMAT DMY;
--SET DATEFORMAT MDY
DECLARE @processing_date AS varchar( 10 );
SET @processing_date = '27/04/2012';
SELECT CAST( @Processing_Date AS date );
this should work as well, but I think it is better to use previous solution and understand the options available
________________________________________________________________
you can lead a user to data....but you cannot make them think
and remember....every day is a school day
September 14, 2012 at 3:46 pm
Better still, just always use 'YYYYMMDD[ 24h:mm:ss.sss]', which always works, regardless of SQL settings.
'20120427'
SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
September 14, 2012 at 7:48 pm
ScottPletcher (9/14/2012)
Better still, just always use 'YYYYMMDD[ 24h:mm:ss.sss]', which always works, regardless of SQL settings.'20120427'
Problem is you don't always have that choice.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply