Question

I have a datetime field. I want to get the records between 9AM and 5PM also I need the records between 5PM AND 9AM. If I am using between operator it gives me the same number of records.

SELECT count(*)
FROM DirectLineMainCallQuery AS CallTbl
Where  Format(CallTbl.CallDate,"dddd") <> 'Saturday' AND Format(CallTbl.CallDate,"dddd") <> 'Sunday'
AND (Format(CallTbl.StartTime,'hh:mm') Between '09:00' AND '17:00')
UNION ALL
SELECT count(*)
FROM DirectLineMainCallQuery AS CallTbl
Where  Format(CallTbl.CallDate,"dddd") <> 'Saturday' AND Format(CallTbl.CallDate,"dddd") <> 'Sunday'
AND  (Format(CallTbl.StartTime,'hh:mm') Between '17:00' AND '09:00') ;

Any help would be appreciated.

Thanks

Was it helpful?

Solution

AK47 has the correct comment, of needing two time ranges, the second one being either 1700 to midnight, or 0000 to 0900, as below-- Notice the extra pair of parens that enclose the last two betweens...

   SELECT Count(*)
    FROM   directlinemaincallquery AS CallTbl
    WHERE  Format(CallTbl.calldate, "dddd") <> 'Saturday'
       AND Format(CallTbl.calldate, "dddd") <> 'Sunday'
     AND ( Format(CallTbl.starttime, 'hh:mm') BETWEEN '09:00' AND '17:00' )

    UNION ALL

    SELECT Count(*)
    FROM   directlinemaincallquery AS CallTbl
    WHERE  Format(CallTbl.calldate, "dddd") <> 'Saturday'
       AND Format(CallTbl.calldate, "dddd") <> 'Sunday'
    AND (( Format(CallTbl.starttime, 'hh:mm') BETWEEN '17:00' AND '23:59' )  
      OR ( Format(CallTbl.starttime, 'hh:mm') BETWEEN '00:00' AND '09:00' )); 

Edited 4/6 nite

You said -- If I am using between operator it gives me the same number of records

And MSACCESS agrees with you... Between 9 and 17 is the same as Between 17 and 9

I created this test, and got this result----

SELECT table1.*
FROM table1
Where Frame between '8' and '3'

Frame
3
4
5
6
7
8

MSACCESS does not care that the "larger" is before the "smaller", but rather gives you BETWEEN the smaller and the larger of the two values. While you may think that it should "Do What I Mean", it cannot. The way to ask it is to make two Betweens for 17--thru--2359 and 0000 thru 0900 as shown in my example, or to use Greater/Lessor signs (>= <= ).

OTHER TIPS

Please try following code,

SELECT count(*)
FROM DirectLineMainCallQuery AS CallTbl
Where  Format(CallTbl.CallDate,"dddd") <> 'Saturday' AND Format(CallTbl.CallDate,"dddd") <> 'Sunday'
AND (Format(CallTbl.StartTime,'hh:mm') >= '09:00' AND (CallTbl.StartTime,'hh:mm') < '17:00')
Union All
SELECT count(*)
FROM DirectLineMainCallQuery AS CallTbl
Where  Format(CallTbl.CallDate,"dddd") <> 'Saturday' AND Format(CallTbl.CallDate,"dddd") <> 'Sunday'
AND (Format(CallTbl.StartTime,'hh:mm') >= '17:00' AND (CallTbl.StartTime,'hh:mm') < '9:00')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top