Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««123»»

Converting Hour and Minute to Decimal Expand / Collapse
Author
Message
Posted Thursday, July 31, 2014 10:00 AM This worked for the OP Answer marked as solution


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:08 PM
Points: 13,206, Visits: 12,687
ba.saidou (7/31/2014)
The goal here is to calculate a UPH of an activity, the activity is considered picking materials.
The issues I am running into is that as long the users are continuously performing the picking without interruption I can calculate their UPH based on the total units picked divided by the hour but when I have a user who only performed a single activity for the hour it throws my numbers off. I believe by converting the time to decimal this will eliminate the issue.

Based on that I am expecting a (8.266666667) Result number.

Thanks,


So what you really want is to look at the time portion. Keep the hours as is and make the minutes a decimal? Seems pretty strange but whatever.

This should do it.

declare @UPH datetime = '2014-07-29 08:16:31.000'

select cast(DATEPART(hour, @UPH) as varchar(2)) + '.' + stuff(cast(DATEPART(minute, @UPH) / 60.0 as varchar(10)), 1, 2, '')



_______________________________________________________________

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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1598355
Posted Thursday, July 31, 2014 10:34 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, September 10, 2014 12:20 PM
Points: 6, Visits: 16
Thanks Sean it worked!!!
Post #1598372
Posted Thursday, July 31, 2014 10:35 AM
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Yesterday @ 7:03 PM
Points: 4,451, Visits: 3,899
It sounds to me like you're after the number of units picked per hour for a specific period of time. It reminds me of a shop floor productivity report. Is this correct? If so, simply subtract the times and divide the quantity by the difference. This example uses the difference in minutes for precision.

with times as (
select dateadd(minute, -90, getdate()) starting_time,
GETDATE() ending_time,
14 quantity
union all
select dateadd(hour, -2, getdate()) starting_time,
GETDATE() ending_time,
160 quantity)
select starting_time, ending_time, quantity,
convert(numeric(12, 6), DATEDIFF(minute, starting_time, ending_time)) / 60 hours,
round(quantity / convert(numeric(12, 6), DATEDIFF(minute, starting_time, ending_time)) * 60, 3) uph
from times;

Am I over-simplifying this?



Tally Tables - Performance Personified
String Splitting with True Performance
Best practices on how to ask questions
Post #1598373
Posted Thursday, July 31, 2014 11:29 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, September 10, 2014 12:20 PM
Points: 6, Visits: 16
Hello Ed,

You nailed and allowed me to go beyound expectation.

Thank you!!!
Post #1598405
Posted Thursday, July 31, 2014 11:32 AM
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Yesterday @ 7:03 PM
Points: 4,451, Visits: 3,899
No problem. Glad I was able to help. Thanks for the feedback.


Tally Tables - Performance Personified
String Splitting with True Performance
Best practices on how to ask questions
Post #1598406
Posted Saturday, August 2, 2014 4:03 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 2:48 AM
Points: 2,395, Visits: 6,616
Quick solution, returns the hour + time fraction (minutes and seconds / 3600)


USE tempdb;
GO

DECLARE @DATE_VAL DATETIME = '2014-07-29 08:16:31.000'

SELECT @DATE_VAL
SELECT DATEPART(HOUR,@DATE_VAL) + (DATEDIFF(SECOND,'00:00:00.000',DATEADD(HOUR, - DATEPART(HOUR, CONVERT(time(3),@DATE_VAL)),CONVERT(time(3),@DATE_VAL))) / 3600.0)

Result
8.275277


Edit: Oops, feel a little silly here, didn't see the answers
Post #1598893
Posted Saturday, August 2, 2014 12:18 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 1:53 AM
Points: 35,540, Visits: 32,123
Sean Lange (7/31/2014)
Sql server stores datetime as decimal. You can easily cast a datetime to decimal. But what is the point? What are you trying to do with it?


I use to think that until I saw a proof and tried it all myself.

If you look at the DATETIME data type in 2005 BOL, it states...

Values with the datetime data type are stored internally by the Microsoft SQL Server 2005 Database Engine as two 4-byte integers. The first 4 bytes store the number of days before or after the base date: January 1, 1900. The base date is the system reference date. The other 4 bytes store the time of day represented as the number of milliseconds after midnight.

The smalldatetime data type stores dates and times of day with less precision than datetime. The Database Engine stores smalldatetime values as two 2-byte integers. The first 2 bytes store the number of days after January 1, 1900. The other 2 bytes store the number of minutes since midnight.


Here's a good link that demonstrates... http://blogs.lessthandot.com/index.php/datamgmt/datadesign/how-are-dates-stored-in-sql-server/

The bad part is that, as of 2008, they no longer tell you how the date/time is stored.

Shifting gears a bit, I love DATETIME instead of DATETIME2. You can convert DATETIME into a DECIMAL or FLOAT number, etc, as expected and, as you stated, the number to the left of the decimal place is the number of whole days since the first of January 1900 and every thing to the right of the decimal is fractional days which can easily be manipulated as time. You cannot do such conversions with the "new" DATETIME2 data type, which is really disappointing.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1598963
Posted Saturday, August 2, 2014 12:38 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 2:48 AM
Points: 2,395, Visits: 6,616
Jeff Moden (8/2/2014)
Sean Lange (7/31/2014)
Sql server stores datetime as decimal. You can easily cast a datetime to decimal. But what is the point? What are you trying to do with it?


I use to think that until I saw a proof and tried it all myself.

If you look at the DATETIME data type in 2005 BOL, it states...

Values with the datetime data type are stored internally by the Microsoft SQL Server 2005 Database Engine as two 4-byte integers. The first 4 bytes store the number of days before or after the base date: January 1, 1900. The base date is the system reference date. The other 4 bytes store the time of day represented as the number of milliseconds after midnight.

The smalldatetime data type stores dates and times of day with less precision than datetime. The Database Engine stores smalldatetime values as two 2-byte integers. The first 2 bytes store the number of days after January 1, 1900. The other 2 bytes store the number of minutes since midnight.


Here's a good link that demonstrates... http://blogs.lessthandot.com/index.php/datamgmt/datadesign/how-are-dates-stored-in-sql-server/

The bad part is that, as of 2008, they no longer tell you how the date/time is stored.

Shifting gears a bit, I love DATETIME instead of DATETIME2. You can convert DATETIME into a DECIMAL or FLOAT number, etc, as expected and, as you stated, the number to the left of the decimal place is the number of whole days since the first of January 1900 and every thing to the right of the decimal is fractional days which can easily be manipulated as time. You cannot do such conversions with the "new" DATETIME2 data type, which is really disappointing.


Doesn't look like the format has changed, look at this query
DECLARE @DT2_0 DATETIME2(0) = '1900-01-01 00:00:00'
DECLARE @DT2_1 DATETIME2(1) = '1900-01-01 00:00:00.1'
DECLARE @DT2_7 DATETIME2(7) = '1900-01-01 00:00:00.0000001'
DECLARE @DT DATETIME = '1900-01-01 00:00:00.003'
SELECT CONVERT(VARBINARY(12),@DT2_0,0) UNION ALL
SELECT CONVERT(VARBINARY(12),@DT2_1,0) UNION ALL
SELECT CONVERT(VARBINARY(12),@DT2_7,0) UNION ALL
SELECT CONVERT(VARBINARY(12),@DT,0)

Results
0x000000005B950A
0x010100005B950A
0x0701000000005B950A
0x0000000000000001


The datetime2 format is first byte is precision, last two are the number of days since year 1 and the middle holds the number of time units since midnight depending on the precision. Mind you, one has to reverse the bytes as it is small endian

The datetime format is still the same upto and including 2014.
Post #1598969
Posted Saturday, August 2, 2014 12:40 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 1:53 AM
Points: 35,540, Visits: 32,123
Here's what I'd use to solve this problem on my systems because it's short and accurate with the accuracy limits of DATETIME. No... it's not ANSI/ISO compliant and no... it won't work with DATETIME2() data types. Of course, if you know me, you'll know that I don't believe in either when it comes to T-SQL, especially when it comes to performance.

DECLARE @UPH datetime = '2014-07-29 08:16:31.000';
SELECT CAST(@UPH AS DECIMAL(17,12))%1*24;


1. The DATETIME is converted to decimal.
2. That is played against Modulus(1) (%1) to return only the fractional part (the REMAINDER from the MODULUS) of the decimal value, which is TIME represented as a fractional day.
3. That is multiplied by 24 hours because there are 24 hours in 1 day, and we're done.

It's a real shame that you can't convert the DATETIME2() directly to decimal to make such problems so easy.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1598970
Posted Saturday, August 2, 2014 12:45 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 2:48 AM
Points: 2,395, Visits: 6,616
Jeff Moden (8/2/2014)
Here's what I'd use to solve this problem on my systems because it's short and accurate with the accuracy limits of DATETIME. No... it's not ANSI/ISO compliant and no... it won't work with DATETIME2() data types. Of course, if you know me, you'll know that I don't believe in either when it comes to T-SQL, especially when it comes to performance.

DECLARE @UPH datetime = '2014-07-29 08:16:31.000';
SELECT CAST(@UPH AS DECIMAL(17,12))%1*24;


The DATETIME is converted to decimal.
That is play against Modulus(1) (%1) to return only the fractional part of the decimal value, which is TIME represented as a fractional day.
That is multiplied by 24 hours because there are 24 hours in 1 day, and we're done.

It's a real shame that you can't convert the DATETIME2() directly to decimal to make such problems so easy.


Short, sweet (and ansi sour)
Post #1598972
« Prev Topic | Next Topic »

Add to briefcase ««123»»

Permissions Expand / Collapse