Solved

Convert VARCHAR value to DATETIME

Posted on 2008-10-14
7
898 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

895 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

14 Experts available now in Live!

Get 1:1 Help Now