Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

SQL Server convert varchar to date - various formats

Posted on 2014-02-19
2
Medium Priority
?
682 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 23

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 70

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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

647 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