Lemi
Lemi

Reputation: 33

Retrieve records falling within a daily time range across a given month

My table contains fields that store

I need help on how to select only those tickets sold between 8:00 AM and 12:00 PM on a day-to-day basis for an entire month, without including any sales between 12:01 PM and 10:00 PM.

Upvotes: 1

Views: 37

Answers (2)

Nick Pfitzner
Nick Pfitzner

Reputation: 216

Simple way to deal with events in given hours of day is to use DATEPART

SELECT *
FROM TicketTable
WHERE DATEPART(hh, SaleDateTime) BETWEEN 8 AND 11

Upvotes: 0

M.Ali
M.Ali

Reputation: 69574

Try something like

SELECT SUM(Price) Total_Morning_Sales
FROM TableName 
WHERE CAST(Sale AS TIME) > '07:59:59'
 AND  CAST(Sale AS TIME) < '12:00:01'
 AND  MONTH(Sale) = 5   --<-- Month Number here

Upvotes: 1

Related Questions