Solved

Convert VARCHAR value to DATETIME

Posted on 2008-10-14
7
894 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 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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:ScottPletcher
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:ScottPletcher
ScottPletcher 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
MS SQL 2014 get SPIDs of users 6 25
C# SQL BULK INSERT CLASS 5 34
Complex SQL 10 33
SQL Server issue connecting to named instance 6 41
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 …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
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.

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now