-- Get Orders for Last Month
SELECT order_date
FROM Orders
WHERE order_date >= DATEADD(MONTH,
DATEDIFF(MONTH, 0, CURRENT_TIMESTAMP) - 1, 0)
AND order_date < DATEADD(MONTH,
DATEDIFF(MONTH, 0, CURRENT_TIMESTAMP), 0);
-- Get Orders for Current Month
SELECT order_date
FROM Orders
WHERE order_date >= DATEADD(MONTH,
DATEDIFF(MONTH, 0, CURRENT_TIMESTAMP), 0)
AND order_date < DATEADD(MONTH,
DATEDIFF(MONTH, 0, CURRENT_TIMESTAMP) + 1, 0);
-- Get Year to Date Orders
SELECT order_date
FROM Orders
WHERE order_date >= DATEADD(YEAR,
DATEDIFF(YEAR, 0, CURRENT_TIMESTAMP), 0)
AND order_date < DATEDIFF(DAY, -1, CURRENT_TIMESTAMP);
-- Get Month to Date Orders
SELECT order_date
FROM Orders
WHERE order_date >= DATEADD(MONTH,
DATEDIFF(MONTH, 0, CURRENT_TIMESTAMP), 0)
AND order_date < DATEDIFF(DAY, -1, CURRENT_TIMESTAMP);
-- Get Last Year's Orders
SELECT order_date
FROM Orders
WHERE order_date >= DATEADD(YEAR,
DATEDIFF(YEAR, 0, CURRENT_TIMESTAMP)-1, 0)
AND order_date < DATEADD(YEAR,
DATEDIFF(YEAR, 0, CURRENT_TIMESTAMP), 0);
-- Get Today's Orders
SELECT order_date
FROM Orders
WHERE order_date >= DATEDIFF(DAY, 0, CURRENT_TIMESTAMP)
AND order_date < DATEDIFF(DAY, -1, CURRENT_TIMESTAMP);
-- Get Yesterday's Orders
SELECT order_date
FROM Orders
WHERE order_date >= DATEDIFF(DAY, 1, CURRENT_TIMESTAMP)
AND order_date < DATEDIFF(DAY, 0, CURRENT_TIMESTAMP);
-- Get Today's Orders Between 9:00 am And 11:00 am
SELECT order_date
FROM Orders
WHERE order_date BETWEEN
CONVERT(VARCHAR(10), CURRENT_TIMESTAMP, 101) + ' 09:00:00.000'
AND CONVERT(VARCHAR(10), CURRENT_TIMESTAMP, 101) + ' 11:00:00.000';
-- Or
SELECT order_date
FROM Orders
WHERE order_date BETWEEN
DATEADD(DAY, DATEDIFF(DAY, 0, CURRENT_TIMESTAMP), '09:00:00')
AND DATEADD(DAY, DATEDIFF(DAY, 0, CURRENT_TIMESTAMP), '11:00:00');
-- Get Orders for Last Week
SELECT order_date
FROM Orders
WHERE order_date >= DATEADD(WEEK,
DATEDIFF(WEEK, 0, CURRENT_TIMESTAMP) - 1, 0)
AND order_date < DATEADD(WEEK,
DATEDIFF(WEEK, 0, CURRENT_TIMESTAMP), 0);
-- Get Orders for Current Week
SELECT order_date
FROM Orders
WHERE order_date >= DATEADD(WEEK,
DATEDIFF(WEEK, 0, CURRENT_TIMESTAMP), 0)
AND order_date < DATEADD(WEEK,
DATEDIFF(WEEK, 0, CURRENT_TIMESTAMP) + 1, 0);
-- Get Orders for One Year Back From Current Date
SELECT order_date
FROM Orders
WHERE order_date >= DATEDIFF(DAY, 0,
DATEADD(YEAR, -1, CURRENT_TIMESTAMP))
AND order_date < DATEDIFF(DAY, -1, CURRENT_TIMESTAMP);