Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Conversion Expand / Collapse
Author
Message
Posted Monday, January 28, 2013 10:43 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Saturday, April 06, 2013 9:18 AM
Points: 57, Visits: 76
How do i convert the below date from

Mon Jan 28 11:03:06 EST 2013

To

2013-01-28 11:03:06

Thanks for your help in advance
Post #1412555
Posted Monday, January 28, 2013 11:10 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Today @ 2:34 PM
Points: 8,592, Visits: 8,233
Mvs2k11 (1/28/2013)
How do i convert the below date from

Mon Jan 28 11:03:06 EST 2013

To

2013-01-28 11:03:06

Thanks for your help in advance


Are you trying to take a string and turn it into a datetime? The source you posted will not directly convert to a datetime. You are going to have to tweak this a bit make it work. What is the source of this date? Is it a datetime datatype or is it string data?


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Post #1412569
Posted Monday, January 28, 2013 11:15 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Today @ 2:34 PM
Points: 8,592, Visits: 8,233
This works on your example provided.

declare @String varchar(50) = 'Mon Jan 28 11:03:06 EST 2013'

select cast(left(right(@String, len(@String) - 4), 16) + right(@String, 4) as DateTime)

Depending on how things like single digit days are in the source this may not work. For example if the source contains 'Mon Jan 8 11:03:06 EST 2013' for January 8th this won't work.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Post #1412571
Posted Monday, January 28, 2013 1:08 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 4:39 PM
Points: 1,320, Visits: 1,772


select
convert(varchar(20), cast(stuff(substring(string, 5, 15), CHARINDEX(':', substring(string, 5, 15)) - 3, 0, RIGHT(string, 5)) as datetime), 120)
from (
select 'Mon Jan 28 11:03:06 EST 2013' as string union all
select 'Mon Jan 8 11:03:06 EST 2013'
) as test_data




SQL DBA,SQL Server MVP('07, '08, '09)
One man with courage makes a majority. Andrew Jackson
Post #1412621
Posted Monday, January 28, 2013 4:42 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Saturday, April 06, 2013 9:18 AM
Points: 57, Visits: 76
Thanks a lot.

How can i extend the query to pull the records fromdate and to date..
Post #1412698
Posted Monday, January 28, 2013 4:51 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 4:39 PM
Points: 1,320, Visits: 1,772
Not 100%, as I don't have any table definition to look at.

But if fromdate and todate are both in the text format above, then something like:


SELECT
...other_columns...
CONVERT(varchar(20), CAST(STUFF(SUBSTRING(fromdate, 5, 15),
CHARINDEX(':', substring(fromdate, 5, 15)) - 3, 0, RIGHT(fromdate, 5)) as datetime), 120) AS from_date,
CONVERT(varchar(20), CAST(STUFF(SUBSTRING(todate, 5, 15),
CHARINDEX(':', substring(todate, 5, 15)) - 3, 0, RIGHT(todate, 5)) as datetime), 120) AS to_date,
...other_columns...
FROM dbo.tablename
--INNER JOIN ...
WHERE ...



SQL DBA,SQL Server MVP('07, '08, '09)
One man with courage makes a majority. Andrew Jackson
Post #1412702
Posted Monday, January 28, 2013 5:27 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 6:28 PM
Points: 2,340, Visits: 3,168
Mvs2k11 (1/28/2013)
How do i convert the below date from

Mon Jan 28 11:03:06 EST 2013

To

2013-01-28 11:03:06

Thanks for your help in advance


If the string contains EST, does that mean other strings might contain different time zone offsets?

That would be a challenging opportunity to use the DATETIMEOFFSET data type.



No loops! No CURSORs! No RBAR! Hoo-uh!

INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?

Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Are you too recursively challenged?
Splitting strings based on patterns can be fast!
Post #1412710
Posted Tuesday, January 29, 2013 6:01 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Saturday, April 06, 2013 9:18 AM
Points: 57, Visits: 76
Yes you are correct the other strings contains different time zone.
Post #1413403
Posted Tuesday, January 29, 2013 6:19 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 6:28 PM
Points: 2,340, Visits: 3,168
Mvs2k11 (1/29/2013)
Yes you are correct the other strings contains different time zone.


If you'd like me to take a shot at implementing my suggestion, can you provide some additional sample data with the time zone information included as it would appear in your input strings? Like CST, MST, PST and/or EDST, CDST, etc.



No loops! No CURSORs! No RBAR! Hoo-uh!

INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?

Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Are you too recursively challenged?
Splitting strings based on patterns can be fast!
Post #1413407
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse