December 7, 2012 at 12:23 pm
I have 2 dates format like 200903021124 and 200903030254.how to get hours,minutes Between those dates?
December 7, 2012 at 1:37 pm
--If the final total will be <= 24 hrs, you can do this:
SELECT CONVERT(char(5), DATEADD(MINUTE, DATEDIFF(MINUTE, STUFF(STUFF(date_column1, 9, 0, ' '), 12, 0, ':'), STUFF(STUFF(date_column2, 9, 0, ' '), 12, 0, ':')), 0), 8)
FROM (
SELECT '200903021124' AS date_column1, '200903030254' AS date_column2
) AS test_data
SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
December 7, 2012 at 1:43 pm
you will hear this about a million times, but the number one rule is to store datetimes as datatimes; not as strings or bigints or any other datatype;
when you don't follow that simple rule, you lose the ability to compare dates, add them up, and do so much more without jumping through hoops to turn the strings back into date times again.
I know sometimes it's not your fault and you inherit bad data structures, but you need to make the effort to convert teh data to the proper data types.
here's one example, assuming your data is strings.
With mySampleData
AS
(
SELECT '200903021124' AsAlmostButNotQuiteADate UNION ALL
SELECT '200903030254'
)
SELECT CONVERT(datetime, STUFF(STUFF(AsAlmostButNotQuiteADate,11,0,':'),9,0,' ') + ':00.000')
FROM mySampleData
Lowell
December 7, 2012 at 2:48 pm
rajkiran.panchagiri (12/7/2012)
I have 2 dates format like 200903021124 and 200903030254.how to get hours,minutes Between those dates?
Are those stored as BIGINT or some character based datatype? Can't tell from here. And, yes, it does make a difference.
--Jeff Moden
Change is inevitable... Change for the better is not.
December 7, 2012 at 2:53 pm
Lowell (12/7/2012)
you will hear this about a million times, but the number one rule is to store datetimes as datatimes; not as strings or bigints or any other datatype;when you don't follow that simple rule, you lose the ability to compare dates, add them up, and do so much more without jumping through hoops to turn the strings back into date times again.
I know sometimes it's not your fault and you inherit bad data structures, but you need to make the effort to convert teh data to the proper data types.
here's one example, assuming your data is strings.
With mySampleData
AS
(
SELECT '200903021124' AsAlmostButNotQuiteADate UNION ALL
SELECT '200903030254'
)
SELECT CONVERT(datetime, STUFF(STUFF(AsAlmostButNotQuiteADate,11,0,':'),9,0,' ') + ':00.000')
FROM mySampleData
If they're strings, that's the way I do it except I don't add the + ':00.000'. It works just fine without that and it's one less character based operation (Concatenation, in this case).
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply