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

x
?
Solved

Convert VARCHAR value to DATETIME

Posted on 2008-10-14
7
Medium Priority
?
930 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
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 1400 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 70

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 70

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 600 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
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.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

916 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