?
Solved

Need SQL help to convert nvarchar(255) to datetime.

Posted on 2013-05-15
3
Medium Priority
?
634 Views
Last Modified: 2013-05-16
I hope this posting finds everyone doing well.

I received a SQL table with a nvarchar(255) column with dates as strings, below are a few examples.

Sun Aug 19 19:01:17 +0000 2012
Thu Oct 13 21:35:22 +0000 2011
Fri Apr 13 03:24:38 +0000 2012
Mon Nov 28 03:05:30 +0000 2011
Wed May 27 01:30:07 +0000 2009
Fri Jul 22 00:27:20 +0000 2011

I wanted to see if anyone knew how to convert these to datetime.  I have been trying the convert method with no luck.  I figure I would ask on here before I started writing my own SQL function to do this.

Many thanks!
0
Comment
Question by:rye004
[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
3 Comments
 
LVL 75

Assisted Solution

by:Aneesh Retnakaran
Aneesh Retnakaran earned 500 total points
ID: 39169671
declare @dt varchar(255) = 'Sun Aug 19 19:01:17 +0000 2012'
select cast (RIGHT(@dt,4)+ ' ' +substring(@dt,5, len(@dt)-14) as datetime )
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 39172083
If you don't need the offset -- for example, it's always +0000 anyway, you can do this:

    CAST(  RIGHT(string, 4) + SUBSTRING(string, 4, 17)  ) AS datetime
0
 

Author Closing Comment

by:rye004
ID: 39173031
Thank you so much for your fast response.  These both worked perfectly.
0

Featured Post

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

762 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