Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Converting to datetime a char field with several possible date formats Expand / Collapse
Author
Message
Posted Tuesday, February 12, 2013 11:52 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, February 07, 2014 6:42 AM
Points: 31, Visits: 277
Comments posted to this topic are about the item Converting to datetime a char field with several possible date formats
Post #1419310
Posted Sunday, April 14, 2013 4:49 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 5:08 PM
Points: 35,971, Visits: 30,263
Thanks for sharing your code but it just doesn't need to be so complex a thing. SQL Server will handle all sorts of date formats without any help. The only time it really runs into a problem is where you have DMY and MDY formats in the same column so it wouldn't be able to figure out if 3/7/2005 is in March or July in a mixed column... just like your code won't be able to.

For code that follows one standard (eithe DMY or MDY) in a given column. No special processing for format is needed. For example:

SET DATEFORMAT DMY

SELECT SomeStringDate
, ConvertedToDateTime = CAST(d.SomeStringDate AS DATETIME)
FROM (
SELECT '15-03-05' UNION ALL
SELECT '15/03/08' UNION ALL
SELECT '15/03/2005' UNION ALL
SELECT '15.03.2005' UNION ALL
SELECT 'March 15 2005' UNION ALL
SELECT 'Mar 15 2005' UNION ALL
SELECT '15 March 2005' UNION ALL
SELECT '15 Mar 2005' UNION ALL
SELECT '20050315'
) d (SomeStringDate)
;



--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

"Change is inevitable. Change for the better is not." -- 04 August 2013
(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1442146
Posted Monday, April 15, 2013 1:34 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, February 07, 2014 6:42 AM
Points: 31, Visits: 277
Thanks a lot, I did not know about SET DATEFORMAT
Post #1442188
Posted Monday, April 15, 2013 4:18 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 5:08 PM
Points: 35,971, Visits: 30,263
Glad to help. Thank you for the feedback.

--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

"Change is inevitable. Change for the better is not." -- 04 August 2013
(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1442529
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse