Solved

Converting a text description of time to an ordinary Datetime value in SQL 2008

Posted on 2011-02-23
3
337 Views
Last Modified: 2012-05-11
I want to convert a varchar expression describing time into a date time value.  I will be given descriptions like this:  2010D320HR22MIN23
Which means the 320th day of Year 2010, Hour 22, Minute 23.
It may be equivalent to a string like this:
2010-10-19 22:23:00.0000000  
(Assuming 10-19 is the 320th day of 2010, I'm not sure if this is right)
So what I want is a function that takes 2010D320HR22MIN23 as input
and returns 2010-11-19 22:23:00.0000000  

I don't see any function that converts day of year to month and day. Is there one?  Or must one be built from scratch? Or is there another way of doing this?

Thanks,

C




0
Comment
Question by:cipriano555
  • 2
3 Comments
 
LVL 15

Expert Comment

by:derekkromm
ID: 34965472
declare @x varchar(100)
set @x = '2010D320HR22MIN23'

declare @y datetime
select @y = 
	dateadd(
		mi,
		convert(int,  right(@x, len(@x) - charindex('MIN', @x)-2)), 
		dateadd(
			hh, 
			convert(int, right(left(@x, charindex('MIN', @x)-1), len(left(@x, charindex('MIN', @x)-1)) - charindex('HR', @x)-1)), 
			dateadd(
				dd, 
				convert(int, right(left(@x, charindex('HR', @x)-1), len(left(@x, charindex('HR', @x)-1)) - 5)), 
				convert(datetime, '1/1/' + left(@x, 4)))))
select @y

Open in new window

0
 
LVL 15

Accepted Solution

by:
derekkromm earned 500 total points
ID: 34965497
Obviously replace all instances of @x with the name of your variable or description column.

And actually, a small error since it starts on 1/1/YY and adds days, it'll be 1 day too far ahead from that original query. Updated:

declare @x varchar(100)
set @x = '2010D1HR22MIN23'

declare @y datetime
select @y = 
	dateadd(
		mi,
		convert(int,  right(@x, len(@x) - charindex('MIN', @x)-2)), 
		dateadd(
			hh, 
			convert(int, right(left(@x, charindex('MIN', @x)-1), len(left(@x, charindex('MIN', @x)-1)) - charindex('HR', @x)-1)), 
			dateadd(
				dd, 
				convert(int, right(left(@x, charindex('HR', @x)-1), len(left(@x, charindex('HR', @x)-1)) - 5)) - 1, 
				convert(datetime, '1/1/' + left(@x, 4)))))
select @y

Open in new window

0
 

Author Closing Comment

by:cipriano555
ID: 34965602
Thank you so much for your help
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…

863 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

27 Experts available now in Live!

Get 1:1 Help Now