Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Converting Date/Time into something more useful

Posted on 2008-06-16
12
Medium Priority
?
377 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 143

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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 

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 143

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
 

Author Comment

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

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 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1000 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 143

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

971 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