0

I´ve created a report in SSRS in which I display all Machines volume from last hour:

where  IntervalStartDateTime >= dateadd(HOUR, -2, GETDATE())
    And IntervalStartDateTime <= dateadd(HOUR, -1, GETDATE())

from last 4 hours:

where  IntervalStartDateTime >= dateadd(HOUR, -4, GETDATE())
    And IntervalStartDateTime <= dateadd(HOUR, -3, GETDATE())

and from last 8 hours:

where  IntervalStartDateTime >= dateadd(HOUR, -8, GETDATE())
    And IntervalStartDateTime <= dateadd(HOUR, -7, GETDATE())

And additionaly I would like to display a row for the actual shift --> here I have some problems. There a three shifts. Night Shift: from: 05:45 - 13:45 early shift: from: 13:45 - 21:45 late shift: from : 21:45 - 05:45

Table:

IntervalStartDateTime
2017-03-28 15:30:00.000
2017-03-28 15:30:00.000
2017-03-28 15:30:00.000
2017-03-28 15:30:00.000
2017-03-28 15:30:00.000

How can I get the actual shift value? I am using Microsoft SQL Server Management Studio

jarlh
  • 42,561
  • 8
  • 45
  • 63
M.JAY
  • 183
  • 2
  • 11

2 Answers2

0

For the current shift...

with WorkByShift as
(
select OtherFields,
       case
         when cast(IntervalStartDateTime as time) < '05:45:00' then 1
         when cast(IntervalStartDateTime as time) < '13:45:00' then 2
         when cast(IntervalStartDateTime as time) < '21:45:00' then 3
         else 1
       end as ShiftNo
from MyTable
)
select WBS.*
from WorkByShift  WBS
where ShiftNo = case
                 when cast(GETDATE() as time) < '05:45:00' then 1
                 when cast(GETDATE() as time) < '13:45:00' then 2
                 when cast(GETDATE() as time) < '21:45:00' then 3
                 else 1
                end
and IntervalStartDateTime > dateadd(HOUR, -16, getdate())
JohnHC
  • 10,935
  • 1
  • 24
  • 40
0

Please try -

WHERE ( IntervalStartTime >= CASE
                                 WHEN CONVERT( TIME, GETDATE() ) < '05:45:00' THEN CONVERT( DATE, DATEADD( DAY, -1, GETDATE() ) ) + CAST( '21:45:00' AS DATETIME )
                                 WHEN CONVERT( TIME, GETDATE() ) < '13:45:00' THEN CONVERT( DATE, GETDATE() ) + CAST( '05:45:00' AS DATETIME )
                                 WHEN CONVERT( TIME, GETDATE() ) < '21:45:00' THEN CONVERT( DATE, GETDATE() ) + CAST( '13:45:00' AS DATETIME )
                                 ELSE                                              CONVERT( DATE, GETDATE() ) + CAST( '21:45:00' AS DATETIME )
                             END )

Note : This is my first time trying SQL-Server, but I'm going to have start sometime I guess. :)

If you have any questions or comments, then please feel free to post a Comment accordingly.

Further Reading...

combining Date and Time fields to DateTime, SQL Server 2008

How to return the date part only from a SQL Server datetime datatype

Community
  • 1
  • 1
toonice
  • 2,211
  • 1
  • 13
  • 20