June 27, 2013 at 2:43 pm
Hi!
I have two fields that are integer type in this format
DATE: 1130627 = (exclude the first digit)(13-06-27)
TIME: 51458 (24hr) = 5:14:58
I am trying to get this into a single datetime field and I cant figure it out 🙁
Can anyone help please.
Thanks!
R.
June 27, 2013 at 3:09 pm
This is not beautiful but might do the trick while I try something different.
DECLARE @Date int = 1130627 ,--= (exclude the first digit)(13-06-27)
@Time int = 51458 --(24hr) = 5:14:58
SELECT DATEADD( ss, @Time % 100, DATEADD( minute, (@Time % 10000) / 100, DATEADD( hh, @Time / 10000, CONVERT( datetime, RIGHT( @Date, 6),12))))
June 27, 2013 at 3:15 pm
And here's a different approach.
SELECT CONVERT( datetime, RIGHT( @Date, 6) + ' ' + STUFF( STUFF( RIGHT( '0' + CAST( @Time as varchar(6)), 6), 5,0,':'), 3,0,':'), 12)
If you have any questions on what's happening on any of the solutions, feel free to ask.
June 27, 2013 at 3:41 pm
Thank you so much! 🙂
June 27, 2013 at 9:29 pm
Similar to the one Luis posted:
DECLARE @ThisDate INT,
@ThisTime INT;
SET @ThisDate = 1130627;
SET @ThisTime = 51458;
SELECT CAST(CAST(19000000 + @ThisDate AS VARCHAR) + ' ' + STUFF(STUFF(RIGHT('0' + CAST(@ThisTime AS VARCHAR), 6),5,0,':'),3,0,':') AS DATETIME);
June 28, 2013 at 7:24 am
Since nobody has said it yet....any chance you can change your datatype in the table to be a datetime? You really should be storing datetime information in a datetime column. It avoid all the hurdles you are going through now.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
June 28, 2013 at 8:06 am
I understood that he wanted the code to fill a datetime column instead of using the int columns. I might be wrong :hehe:
June 28, 2013 at 8:11 am
Luis Cazares (6/28/2013)
I understood that he wanted the code to fill a datetime column instead of using the int columns. I might be wrong :hehe:
It very well could be that the OP is already trying to make their life easier by following my suggestion long before I said it. 😎
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
June 30, 2013 at 8:35 pm
Another way:
DECLARE @Date int = 1130627 ,--= (exclude the first digit)(13-06-27)
@Time int = 51458 --(24hr) = 5:14:58
SELECT DATEADD(hour, @Time/10000
,DATEADD(minute, @Time/100%100
,DATEADD(second, @Time%100, STUFF(STUFF(@Date+19000000, 7, 0, '-'), 5, 0, '-'))))
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply