August 9, 2018 at 6:52 am
Hi, I have read a lot of post about this subject and as yet can't find a solution that works for me.
I have a decimal number
3.25
which is 3.25 minutes
translated to HH:MM:SS that equals 00:03:15
My question is how in SQL server 2008 R2 can I replicate this?
Most of the posts I have read, disregard the seconds and they are vital for this calculation.
example is
CONVERT(char(8),DATEADD(minute,3.25,0),114) = 00:03:00
and I require 00:03:15
Any help gratefully received.
Dave
August 9, 2018 at 7:16 am
DECLARE @t decimal(10,2) = 3.25;
SELECT CONVERT(char(8), DATEADD(second, @t%1 * 60, DATEADD(minute, @t, 0)), 114);
August 9, 2018 at 7:29 am
Ken McKelvey - Thursday, August 9, 2018 7:16 AM
DECLARE @t decimal(10,2) = 3.25;
SELECT CONVERT(char(8), DATEADD(second, @t%1 * 60, DATEADD(minute, @t, 0)), 114);
There is absolutely no reason to do this as two separate operations. Instead of just converting the decimal portion to seconds, convert everything to seconds.
Also, you should never store date/time data as characters.
SELECT CAST(DATEADD(SECOND, 3.25 * 60, 0) AS time(0))
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
August 9, 2018 at 7:50 am
drew.allen - Thursday, August 9, 2018 7:29 AMKen McKelvey - Thursday, August 9, 2018 7:16 AM
DECLARE @t decimal(10,2) = 3.25;
SELECT CONVERT(char(8), DATEADD(second, @t%1 * 60, DATEADD(minute, @t, 0)), 114);There is absolutely no reason to do this as two separate operations. Instead of just converting the decimal portion to seconds, convert everything to seconds.
Also, you should never store date/time data as characters.
SELECT CAST(DATEADD(SECOND, 3.25 * 60, 0) AS time(0))
Drew
Worked like a charm.
Many thanks Drew.
Dave
August 9, 2018 at 8:47 am
david_h_edmonds - Thursday, August 9, 2018 7:50 AMdrew.allen - Thursday, August 9, 2018 7:29 AMKen McKelvey - Thursday, August 9, 2018 7:16 AM
DECLARE @t decimal(10,2) = 3.25;
SELECT CONVERT(char(8), DATEADD(second, @t%1 * 60, DATEADD(minute, @t, 0)), 114);There is absolutely no reason to do this as two separate operations. Instead of just converting the decimal portion to seconds, convert everything to seconds.
Also, you should never store date/time data as characters.
SELECT CAST(DATEADD(SECOND, 3.25 * 60, 0) AS time(0))
Drew
Worked like a charm.
Many thanks Drew.
Dave
Note that this option will only work if the value is lower than 1440 minutes. Once you get there, it will reset to zero. If that's fine, then go ahead.
August 9, 2018 at 9:01 am
Luis Cazares - Thursday, August 9, 2018 8:47 AMdavid_h_edmonds - Thursday, August 9, 2018 7:50 AMdrew.allen - Thursday, August 9, 2018 7:29 AMKen McKelvey - Thursday, August 9, 2018 7:16 AM
DECLARE @t decimal(10,2) = 3.25;
SELECT CONVERT(char(8), DATEADD(second, @t%1 * 60, DATEADD(minute, @t, 0)), 114);There is absolutely no reason to do this as two separate operations. Instead of just converting the decimal portion to seconds, convert everything to seconds.
Also, you should never store date/time data as characters.
SELECT CAST(DATEADD(SECOND, 3.25 * 60, 0) AS time(0))
Drew
Worked like a charm.
Many thanks Drew.
Dave
Note that this option will only work if the value is lower than 1440 minutes. Once you get there, it will reset to zero. If that's fine, then go ahead.
It's to do with call answering times, if a call is ringing longer than 1440 minutes, we have bigger problems!!! π
Thanks for the heads up though.
Dave
August 11, 2018 at 6:00 pm
david_h_edmonds - Thursday, August 9, 2018 9:01 AMLuis Cazares - Thursday, August 9, 2018 8:47 AMdavid_h_edmonds - Thursday, August 9, 2018 7:50 AMdrew.allen - Thursday, August 9, 2018 7:29 AMKen McKelvey - Thursday, August 9, 2018 7:16 AM
DECLARE @t decimal(10,2) = 3.25;
SELECT CONVERT(char(8), DATEADD(second, @t%1 * 60, DATEADD(minute, @t, 0)), 114);There is absolutely no reason to do this as two separate operations. Instead of just converting the decimal portion to seconds, convert everything to seconds.
Also, you should never store date/time data as characters.
SELECT CAST(DATEADD(SECOND, 3.25 * 60, 0) AS time(0))
Drew
Worked like a charm.
Many thanks Drew.
Dave
Note that this option will only work if the value is lower than 1440 minutes. Once you get there, it will reset to zero. If that's fine, then go ahead.
It's to do with call answering times, if a call is ringing longer than 1440 minutes, we have bigger problems!!! π
Thanks for the heads up though.Dave
If that's the case, then why isn't 3.25 minutes acceptable? I'd also think that you'd want to add up the number of minutes per hour or per day, as well, and already having minutes in decimal form is a blessing in disguise.
--Jeff Moden
Change is inevitable... Change for the better is not.
August 12, 2018 at 12:12 am
Hi, thanks for the reply, I have them in both formats, decimal and time. I am trying to cater for end users who arenβt really sure what they want.
Kind Regards
Dave
August 12, 2018 at 12:46 am
Quick question, as there is no duration data type in SQL Server, why not simply record the start and end date times and when needed, simply calculate the difference to the granularity needed?
π
There are many pitfalls when emulating duration with the date and time data types and although this may seem trivial at the first glance, it is an accident waiting to happen.
August 12, 2018 at 6:06 am
If you can collect duration in minutes, storing it as a decimal count of minutes and only ever converting it as a presentation layer thing is probably the best approach. If you do store a start and end time, consider DateTimeOffset for both ends less you end up with negative durations when leap years occur.
August 12, 2018 at 9:47 am
All valid points for consideration however in this instance, the LOBS calculates the duration prior to the extract so I am absolved of responsibility for how it is calculated.
Cheers
Dave
August 12, 2018 at 9:55 am
andycadley - Sunday, August 12, 2018 6:06 AMIf you can collect duration in minutes, storing it as a decimal count of minutes and only ever converting it as a presentation layer thing is probably the best approach. If you do store a start and end time, consider DateTimeOffset for both ends less you end up with negative durations when leap years occur.
That is simply an assumption and as the requirements may change, what if the business decides on billing by the millisecond?
π
In this situation, the business has to decide on the highest/lowest granularity and that defines the data collection and processes.
August 12, 2018 at 3:02 pm
david_h_edmonds - Sunday, August 12, 2018 9:47 AMAll valid points for consideration however in this instance, the LOBS calculates the duration prior to the extract so I am absolved of responsibility for how it is calculated.CheersDave
Heh... True enough. I forgot that this is probably coming directly from a phone system. Avaya systems store call duration as whole seconds rather than decimal minutes. At least those conversions are fairly direct.
--Jeff Moden
Change is inevitable... Change for the better is not.
August 12, 2018 at 3:05 pm
Eirikur Eiriksson - Sunday, August 12, 2018 9:55 AMandycadley - Sunday, August 12, 2018 6:06 AMIf you can collect duration in minutes, storing it as a decimal count of minutes and only ever converting it as a presentation layer thing is probably the best approach. If you do store a start and end time, consider DateTimeOffset for both ends less you end up with negative durations when leap years occur.That is simply an assumption and as the requirements may change, what if the business decides on billing by the millisecond?
πIn this situation, the business has to decide on the highest/lowest granularity and that defines the data collection and processes.
For phone systems, I don't know of a one that stores duration of the call as anything less than whole seconds. The same goes with call Start and End times. Haven't seen one yet that stores such information with a resolution of less than a second.
That, not withstanding, I suppose it could happen someday.
--Jeff Moden
Change is inevitable... Change for the better is not.
August 12, 2018 at 3:20 pm
Eirikur Eiriksson - Sunday, August 12, 2018 12:46 AMQuick question, as there is no duration data type in SQL Server, why not simply record the start and end date times and when needed, simply calculate the difference to the granularity needed?
πThere are many pitfalls when emulating duration with the date and time data types and although this may seem trivial at the first glance, it is an accident waiting to happen.
I have to disagree but only on a purely semantic level. The DATETIME datatype calculates (Duration = EndDateTime-StartDateTime, directly) and stores durations as an offset from the "0" based date just fine. It even works with negative durations if you're trying to build a countdown. The only problem is that there isn't a convenient built-in display conversion other than format #114 (hh:mi:ss:mmm(24h)) but that's also not a huge issue.
Please see the following article for more information:
Calculating Duration Using DATETIME Start and End Dates (SQL Spackle)
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 15 posts - 1 through 15 (of 16 total)
You must be logged in to reply to this topic. Login to reply