Solved

Convert VARCHAR value to DATETIME

Posted on 2008-10-14
7
917 Views
Last Modified: 2008-10-15
OK in SQL in a column called "STARTDATE" I a list of values that don't really show DATETIME. I need to convert...

20081018090000 which is 2008= year, 10 = month, 18 - day of month. 090000 = 9am

Is there a way I can convert that in SQL to 10/18/2008 9:00am ?

0
Comment
Question by:jsuttor
[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
7 Comments
 
LVL 3

Author Comment

by:jsuttor
ID: 22717796
all of the values in Column Startdate are in this same format. Sorry if that was not clear in the original post.
0
 
LVL 39

Accepted Solution

by:
BrandonGalderisi earned 350 total points
ID: 22717821
Here you go.

The line that leads with "set @dt = " is what you want, just replace your column name.
declare @dts varchar(20)
     ,@dt datetime
set @dts='20081018090000'
set @dt = convert(datetime, left(@dts,8), 112) + convert(datetime,substring(@dts,9,2)+':'+substring(@dts,11,2)+':'+right(@dts,2),108)
select @dt

Open in new window

0
 
LVL 3

Author Comment

by:jsuttor
ID: 22717872
Ok well maybe I was not clear enough. I need this done in a query. I know the rest of the query. I just need to convert the values that come up in the above format to a datetime.

My query is as follows... just need the STARTDATE converted to datetime

SELECT     Name, Location, Description, StartDate
FROM         dbo.calendarEvents
ORDER BY StartDate DESC
0
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 
LVL 60

Expert Comment

by:chapmandew
ID: 22717901
Brandon was quite clear in his explanation...just as a refresher (NO POINTS please)


select convert(datetime, left(YOURFIELDNAME,8), 112) + convert(datetime,substring(YOURFIELDNAME,9,2)+':'+substring(YOURFIELDNAME,11,2)+':'+right(YOURFIELDNAME,2),108)
FROM YOURTABLENAME
0
 
LVL 2

Expert Comment

by:Deepika_Rastogi
ID: 22719264

Brandon is right

you just have to do this

SELECT     Name, Location, Description, (convert(datetime, left(StartDate ,8), 112) + convert(datetime,substring(StartDate ,9,2)+':'+substring(StartDate ,11,2)+':'+right(StartDate ,2),108)) as Start_Date
FROM  calendarEvents
ORDER BY Start_Date DESC

Hope this helps
Deepika
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 22723475
SELECT CONVERT(CHAR(10), CAST(LEFT(startdate, 8) AS DATETIME), 101) +
    RIGHT(CONVERT(VARCHAR(30), CAST(STUFF(SUBSTRING(startdate, 9, 4), 3, 0, ':') AS DATETIME), 0) , 8)


For example:

SELECT CONVERT(CHAR(10), CAST(LEFT(startdate, 8) AS DATETIME), 101) +
    RIGHT(CONVERT(VARCHAR(30), CAST(STUFF(SUBSTRING(startdate, 9, 4), 3, 0, ':') AS DATETIME), 0) , 8)
FROM (
    SELECT '20081018090000' AS startdate
) AS sampleData
0
 
LVL 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 150 total points
ID: 22723508
[Note that format 108 will give you 24-hr time, not am/pm.]

Also, you may want to add a computed column to the table that does the conversion.  Then you won't have to repeat the code in several different SELECTs, just use a different column name when you want the formatted version.

For example:

ALTER TABLE tablename
ADD startdate_formatted AS CONVERT(CHAR(10), CAST(LEFT(startdate, 8) AS DATETIME), 101) +
    RIGHT(CONVERT(VARCHAR(30), CAST(STUFF(SUBSTRING(startdate, 9, 4), 3, 0, ':') AS DATETIME), 0) , 8)

And, from then on:

SELECT ..., startdate_formatted, ...
FROM tablename
WHERE startdate_formatted >= ...
ORDER BY startdate_formatted
0

Featured Post

Independent Software Vendors: 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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Viewers will learn how the fundamental information of how to create a table.
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.

729 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