Solved

Convert VARCHAR value to DATETIME

Posted on 2008-10-14
7
921 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
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!

 
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

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.
I have a large data set and a SSIS package. How can I load this file in multi threading?
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.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

623 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