Solved

Converting Date/Time into something more useful

Posted on 2008-06-16
12
341 Views
Last Modified: 2008-06-16
Experts,

I have a NVARCHAR field in my table called CreatedDate. Here is a sample of the data that is in this field: 2008-05-15T16:42:50. I need to be able to convert this data into: 05-15-2008 16:42:50. I have tried a few combinations of CONVERT statements, but nothing seems to be working. All help is appreciated.

BTW

I am running SQL Server 2000
0
Comment
Question by:MarkRod
12 Comments
 
LVL 43

Expert Comment

by:TimCottee
ID: 21793142
Hello MarkRod,

You just need to change the "T" to " " e.g.,

Declare @Something varchar(50)
Set @Something = '2008-05-15T16:42:50'
Select cast(replace(@Something,'T',' ') as datetime)

Regards,

TimCottee
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21793149
>I have a NVARCHAR field in my table called CreatedDate.
why is that not datetime data type, actually?
I guess because it's loaded from external data source, seeing the data?

anyhow, here we go:

CONVERT(VARCHAR(11), CONVERT(datetime, REPLACE(yourfield, 'T', ' '), 105) + CONVERT(VARCHAR(8), CONVERT(datetime, REPLACE(yourfield, 'T', ' '), 8)

Open in new window

0
 
LVL 7

Expert Comment

by:garethh86
ID: 21793160
0
 

Author Comment

by:MarkRod
ID: 21794078
Tim,

I did that but I still was not able to then convert the date format from yyyy-mm-dd
0
 

Author Comment

by:MarkRod
ID: 21794604
I use this in my select statement: CONVERT(NVARCHAR(40), REPLACE([CreatedDate],'T',' '), 101). My output is 2008-05-15 16:42:50. Shouldn't it be 05/15/2008 16:42:20?
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21794809
>Shouldn't it be 05/15/2008 16:42:20?
no, because the convert, when getting a varchar input, will not assume datetime...

didn't you try my suggestion?
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

Author Comment

by:MarkRod
ID: 21794910
I can't get it to work right
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21796340
Indeed, I omitted a part in the expression...

declare @t varchar(30)

set @t = '2008-05-15T16:42:50'

select @t original

  , CONVERT(CHAR(11), CONVERT(datetime, REPLACE(@t, 'T', ' '), 120) , 105) 

  + CONVERT(VARCHAR(8), CONVERT(datetime, REPLACE(@t, 'T', ' '), 120), 8) 

  result

Open in new window

0
 

Author Comment

by:MarkRod
ID: 21796388
Almost there, with what you just posted it puts the date in dd-mm-yyyy format. I need it in mm-dd-yyyy format. I appreciate your help.
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
ID: 21796557
change the 105 to 110:
http://msdn.microsoft.com/en-us/library/ms187928.aspx
declare @t varchar(30)

set @t = '2008-05-15T16:42:50'

select @t original

  , CONVERT(CHAR(11), CONVERT(datetime, REPLACE(@t, 'T', ' '), 120) , 110) 

  + CONVERT(VARCHAR(8), CONVERT(datetime, REPLACE(@t, 'T', ' '), 120), 8) 

  result

Open in new window

0
 

Author Comment

by:MarkRod
ID: 21796613
That does it! Thanks for everything!
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21796654
this time, it's me to thank you for closing the question,
as this one pushed me over the 10M barrier (savant level) in the MS SQL Server zone!
CHeers
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
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.

929 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

19 Experts available now in Live!

Get 1:1 Help Now