Solved

SQL Server convert varchar to date - various formats

Posted on 2014-02-19
2
669 Views
Last Modified: 2014-02-19
Hi.. I have a field with various dates like this.
How can I convert them ALL to / into Date field.

Aug  1 2013 12:00AM
12/29/2013
Jan 1 2011
null




thx
0
Comment
Question by:JElster
2 Comments
 
LVL 22

Assisted Solution

by:Steve Wales
Steve Wales earned 50 total points
ID: 39871356
You're going to want to have a look at the CONVERT function.  It will cover most of them but you may have to tinker a little with the one with the date in the format where the month name is used.

See docs: http://msdn.microsoft.com/en-us/library/ms187928.aspx
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 450 total points
ID: 39871710
Just CAST it to a date, unless you have a really odd format that SQL can't handle.

If you need to, you can use ISDATE() to test the column first, to see if it will convert OK or not.

SELECT
    date_string,
    CAST(date_string AS date) AS date
FROM (
    SELECT 'Aug  1 2013 12:00AM' AS date_string UNION ALL
    SELECT '12/29/2013' UNION ALL
    SELECT 'Jan 1 2011' UNION ALL
    SELECT null
) AS test_data
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question