Could use fresh POV help :)
Need to show trips between midnight and 4AM. StartTime
column is of type DATETIME
. I already tried
CAST(StartTime AS TIME) BETWEEN CAST('00:00:00' AS TIME) AND....
but it didn't show the correct results and so I've done this IIF
but still even though the StartTime
is NOT between midnight and 4AM, the results shows 1. Am I missing something? Thank for your help!
SELECT
id
, StartTime
, DATEADD(d, 0, DATEDIFF(d, 0, StartTime)) AS Midnight
, DATEADD(hh, +4 , DATEDIFF(d, 0, StartTime)) AS FourAM
, IIF((StartTime BETWEEN DATEADD(d, 0, DATEDIFF(d, 0, StartTime))
AND DATEADD(hh, +4 , DATEDIFF(d, 0, StartTime))), 1, 0) _Between_Check
FROM
FUN.Trip
Results:
ID | StartDate | Midnight | FourAM | Between_Check
------------+-----------------------+-------------------------+-------------------------+--------------
-2135024021 | 19-10-02 00:04:01.000 | 2019-10-02 00:00:00.000 | 2019-10-02 00:04:00.000 | 1
-2135024228 | 19-10-05 00:04:30.000 | 2019-10-05 00:00:00.000 | 2019-10-05 00:04:00.000 | 1
Above is the result I'm getting. The Between_Check column should be showing 0 as the StartDate is a minute past 4AM
This is what I should be getting
Results:
ID | StartDate | Midnight | FourAM | Between_Check
------------+-----------------------+-------------------------+-------------------------+--------------
-2135024021 | 19-10-02 00:04:01.000 | 2019-10-02 00:00:00.000 | 2019-10-02 00:04:00.000 | 0
-2135024228 | 19-10-05 00:02:30.000 | 2019-10-05 00:00:00.000 | 2019-10-05 00:04:00.000 | 0