Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Converting Date/Time into something more useful

Posted on 2008-06-16
12
Medium Priority
?
374 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
[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
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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

721 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