?
Solved

SQL Server convert varchar to date - various formats

Posted on 2014-02-19
2
Medium Priority
?
677 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 22

Assisted Solution

by:Steve Wales
Steve Wales earned 200 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 1800 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

752 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