Solved

SQL Server convert varchar to date - various formats

Posted on 2014-02-19
2
667 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:
ScottPletcher 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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

     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 …
After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now