December 10, 2015 at 5:00 am
Hi,
How do I get the date to change if the time someone works goes past midnight.
i.e if someone works 21:00 - 06:00.
I have a roster date column, start shift column and end shift column.:crazy:
Thank you
December 10, 2015 at 6:05 am
hazeleyre_23 (12/10/2015)
Hi,How do I get the date to change if the time someone works goes past midnight.
i.e if someone works 21:00 - 06:00.
I have a roster date column, start shift column and end shift column.:crazy:
Thank you
Which date are you referring to?
Can you simply add 1 to this 'date' if the end shift is earlier than the start shift?
December 10, 2015 at 6:21 am
if StartShift and EndShift are DateTime data types, you can just use Datediff to get the period of time.
SELECT DateDiff(minute,StartShift,EndShift) from yourtable.
if it's a TIME data type, you have to convert to date time, and if EndShift is less than StartShift, add one day.
if you are storing strings or inteigers representing time(minutes?), you have to jump through even more hoops to convert to datetime.
what datatypes are you actually using? Best practice is always use datetime variables to represent datetimes. otherwise you have a mountain of conversions and extra jumps to go through(leap year, daylight savings, month end dates, etc)
Lowell
December 10, 2015 at 6:49 am
I am currently using Time datatype which i think is causing the issue.
If i was to change my database to datatime datatype would could i get it to just show time ?
thank you for your help 🙂
December 10, 2015 at 7:17 am
hazeleyre_23 (12/10/2015)
I am currently using Time datatype which i think is causing the issue.If i was to change my database to datatime datatype would could i get it to just show time ?
thank you for your help 🙂
Well how you have it stored and how you "show" it is two different things. However, once you have a datetime field working with the values become much simpler.
Simple Example:
SELECT GETDATE() AS myDateTime, CONVERT(VARCHAR(8),GETDATE(),114) AS myTime
December 10, 2015 at 7:29 am
i adapted and created some sample data.
Declare @MyDay datetime = '2015-01-01'
;With MYFirstNames(FName) AS
(
Select 'Leonardo' UNION ALL
Select 'Brad' UNION ALL
Select 'Arnold' UNION ALL
Select 'Mark' UNION ALL
Select 'Matt' UNION ALL
Select 'Bruce'
),
MyLastNames(LName) AS
(
Select 'DeCaprio' UNION ALL
Select 'Pitt' UNION ALL
Select 'Schwarzenegger' UNION ALL
Select 'Wahlberg' UNION ALL
Select 'Damon' UNION ALL
Select 'Willis'
), SomeDataWithTime
AS
(
SELECT TOP 20
A.FName,B.LName ,
CONVERT(time,DATEADD(hh,ABS(CHECKSUM(NEWID())) %24,0)) As StartShift,
CONVERT(time,DATEADD(hh,ABS(CHECKSUM(NEWID())) %24,0)) As EndShift
FROM MYFirstNames A
CROSS JOIN MyLastNames B
)
SELECT StartShift,
EndShift,
CONVERT(datetime,StartShift) StartShiftDT,
CONVERT(datetime,EndShift) StartShiftDT,
CASE
WHEN EndShift <= StartShift --= means they worked 24 hours straight? valid assumption? or did they punch in and out in the same minute?
THEN DATEDIFF(minute, CONVERT(datetime,StartShift),DATEADD(dd,1,CONVERT(datetime,EndShift)))
ELSE DATEDIFF(minute, CONVERT(datetime,StartShift),CONVERT(datetime,EndShift))
END / 60.0 AS ElapsedHours,
* FROM SomeDataWithTime
Lowell
December 10, 2015 at 7:34 am
I did something slightly different from Lowell's.
CREATE TABLE #Shifts(
StartShift time,
EndShift time
)
INSERT INTO #Shifts
VALUES( '21:00', '06:00'),
( '06:00', '14:00'),
( '14:00', '21:00');
SELECT *,
(DATEDIFF( MI, CAST( StartShift AS datetime), EndShift) / 60.)
+ CASE WHEN StartShift > EndShift THEN 24 ELSE 0 END AS ElapsedHours
FROM #Shifts
GO
DROP TABLE #Shifts
December 10, 2015 at 8:05 am
If you aren't storing dates, be careful here. I would bet you get a bug at some point when a shift or a schedule goes over 24 hours. I've worked 36 hours before as an hourly person, so be aware of this. Store the date.
Viewing 8 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply