Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Conversion of (yyyyddmmhhmmss) to datetime

Posted on 2004-08-19
9
Medium Priority
?
451 Views
Last Modified: 2010-05-19
Hi,

we are storing datetime(yyyymmddhhmmss) in the varchar datatype as string. I want to manupulate the time with that datestring.  I have used CONVERT(datetime,'20040816000000'), but failed. Please help me

Thank you
0
Comment
Question by:chemanth
[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
9 Comments
 
LVL 11

Accepted Solution

by:
ram2098 earned 672 total points
ID: 11839248
You need to split and do it

select convert(datetime,convert(varchar(10),CONVERT(datetime,left('20040816000000',8),101),120) + ' ' + substring('20040816000000',9,2) + ':' + substring('20040816000000',11,2)+ ':' + substring('20040816000000',13,2))
0
 
LVL 11

Expert Comment

by:ram2098
ID: 11839257
The above displays you it in the valid datetime format. Is this want? Let me know if I am wrong.
0
 
LVL 3

Expert Comment

by:alexpreston
ID: 11839264
I think that's right. I was trying to think of a neater way, but I think this is the best way. It seems a lot of effort but since the date string is not in a recognised format, the fiddling around is necessary.
0
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 664 total points
ID: 11839330
CONVERT arguments:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_ca-co_2f3o.asp

As you can see, only ISO has a non-separator format, but without time (s%$@  M$ :-)

Thus, indeed you need to fiddle around with substrings etc (as shown above)

CHeers

0
 
LVL 10

Expert Comment

by:AustinSeven
ID: 11839481
I saw ram2098's post after I had already done something similar.   Here it is anyway.   I don't know any other 'neater' way that this general method.  

ram2098's example gives this output: 2004-08-16 00:00:00.000

My example below gives this output:  2004-08-16 01:08:12.000 -- because I entered a time value in the string.  However, ram2098's solution produces the correct result on the time side too.  

select convert(datetime,left('20040816000000',8) +' '+ substring('20040816010812',9,2) + ':' + substring( '20040816010812',11,2) + ':' + right('20040816010812',2),108)

As for doing time calculations, here's a bit of an example:-

declare @time1 datetime
declare @time2 datetime

select @time1 = convert(datetime,left('20040816000000',8) +' '+ substring('20040816000000',9,2) + ':' + substring( '20040816000000',11,2) + ':' + right('20040816000000',2),108)

select @time2 = convert(datetime,left('20040816010812',8) +' '+ substring('20040816010812',9,2) + ':' + substring( '20040816010812',11,2) + ':' + right('20040816010812',2),108)

select datediff(minute,@time1,@time2)


AustinSeven
0
 
LVL 70

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 664 total points
ID: 11841940
I agree, you have to do some manipulation.  The shortest method I can figure out is:

DECLARE @v1 VARCHAR(20)
SET @v1 = '20040816000000'

SELECT CAST(LEFT(@v1, 8) + ' ' + STUFF(STUFF(SUBSTRING(@v1, 9, 6), 3, 0, ':'), 6, 0, ':') AS DATETIME)
0
 
LVL 3

Expert Comment

by:Ayesha_K
ID: 11842759
you can also select minutes and seconds from the database like

select minute(Mydate),second(MyDate) from ...

Regards
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

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…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

715 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