I am trying to get orders from between two dates and where those orders were between 5PM and 6:20 PM.
SELECT CREATION_DATE_TIME_STAMP
FROM SHIPMENT_HEADER
WHERE CREATION_DATE_TIME_STAMP between '2019-08-01 17:00:00:000' and '2019-10-31 18:20:000' AND
ORDER_TYPE = 'Catalog'
You need separate conditions on the date and time components. Date and time functions are notoriously database specific, but something like this:
SELECT CREATION_DATE_TIME_STAMP
FROM SHIPMENT_HEADER
WHERE CREATION_DATE_TIME_STAMP >= '2019-08-01' AND
CREATION_DATE_TIME_STAMP < '2019-11-01' AND
CAST(CREATION_DATE_TIME_STAMP as TIME) >= '17:00:00' AND
CAST(CREATION_DATE_TIME_STAMP as TIME) <= '18:20:00' AND
ORDER_TYPE = 'Catalog';
This assumes that a simple cast() is sufficient to extract the time.
Related
Let's say I want to get the profit between two dates. Then I can do something like this:
SELECT SUM(Profit)
FROM Sales
WHERE date BETWEEN '2014-01-01' AND '2014-02-01' AND <other_filters>
I would then like to compare it to a previous period offset by a fixed amount. It could be written something like this to get it in two rows:
SELECT SUM(Profit)
FROM Sales
WHERE date BETWEEN '2014-01-01' AND '2014-02-01' AND <other_filters>
UNION ALL
SELECT SUM(Profit)
FROM Sales
WHERE date BETWEEN '2014-01-01' - INTERVAL 1 YEAR AND '2014-02-01' - INTERVAL 1 YEAR AND <other_filters>
Is there a way to do this without a union? I am looking for something like this:
SELECT
SELECT SUM(Profit),
???
FROM Sales
WHERE date BETWEEN '2014-01-01' AND '2014-02-01' AND <other_filters>
I think the tricky part here is how to 'un-do' the where filter for the offseted-time calculation.
You can use conditional aggregation and OR the range checks in the WHERE clause (unless they are subsequent in which case you can combine them directly of course).
SELECT sum(CASE
WHEN date >= '2014-01-01'
AND date < '2014-02-02' THEN
profit
ELSE
0
END),
sum(CASE
WHEN date >= '2014-01-01' - INTERVAL 1 YEAR
AND date < '2014-02-02' - INTERVAL 1 YEAR THEN
profit
ELSE
0
END)
FROM sales
WHERE date >= '2014-01-01'
AND date < '2014-02-02'
OR date >= '2014-01-01' - INTERVAL 1 YEAR
AND date < '2014-02-02' - INTERVAL 1 YEAR;
Note: Prefer not to use BETWEEN here but check for a right half open range check. That way, if the precision of date changes, records on the end past midnight are still in the results.
I have codes as below:
select date
from date_table
where date between '2019-05-01' and '2019-07-31'
and date between '2020-05-01' and '2020-07-31'
but when i run the code above it shows no rows to display despite i run successfully when only using
select date
from date_table
where date between '2019-05-01' and '2019-07-31'
Does anyone know why?
Presumably you want to OR these two date ranges:
SELECT date
FROM date_table
WHERE
date BETWEEN '2019-05-01' AND '2019-07-31'
OR
date BETWEEN '2020-05-01' AND '2020-07-31';
ANDing together the two ranges will never return any records, since the two ranges are mutually exclusive, and any date can only fall into one of the ranges.
As a side note, if you want to include 1st May 2019 up to, and including, 31st July 2019 (with similar logic on the other range as well), then you should be using inequalities:
SELECT date
FROM date_table
WHERE
date >= '2019-05-01' AND date < '2019-08-01'
OR
date >= '2020-05-01' AND date < '2020-08-01';
I'm just wondering how can I join these two queries?
SELECT SUM(TOTAL_SALES) FROM TBL_SALES WHERE LOGIN_DATE >= '2020-01-03' AND LOGOUT_DATE < '2020-01-04'
SELECT SUM(TOTAL_MONEY) FROM TBL_SALES_INFO WHERE LOGIN_DATE >= '2020-01-03' AND LOGOUT_DATE < '2020-01-04'
here's my code but I'm getting the wrong result:
SELECT SUM(A.TOTAL_SALES) AS TOTAL_SALES,
SUM(B.TOTAL_MONEY) AS TOTAL_MONEY
FROM TBL_SALES A RIGHT JOIN
TBL_SALES_INFO B
ON A.SALES_NO= B.SALES_NO
WHERE A.LOGIN_DATE>= '2020-01-01 09:00:00' AND A.LOGOUT_DATE < '2020-01-02 09:00:00' AND
B.LOGIN_DATE>= '2020-01-01 09:00:00' AND B.LOGOUT_DATE < '2020-01-02 09:00:00'
Your current queries are an entirely separate tables. The best I can see would be to just leave them as is, an aggregate in a top level SELECT:
SELECT
(SELECT SUM(TOTAL_SALES) FROM TBL_SALES
WHERE LOGIN_DATE >= '2020-01-03' AND LOGOUT_DATE < '2020-01-04') AS TOTAL_SALES,
(SELECT SUM(TOTAL_MONEY) FROM TBL_SALES_INFO
WHERE LOGIN_DATE >= '2020-01-03' AND LOGOUT_DATE < '2020-01-04') AS TOTAL_MONEY;
You might be over-complicating things with the a. and b. stuff? In addition, rather than a right join maybe an inner join would work better? Then you wouldn't need to sort by timestamps from both tables. You'd only need the total sales and timestamps based on one tables time.
It would help if you posted the results you were getting, even if they were incorrect.
I am using a where clause to extract data but into database I have datetime stamp and I want to extract data by using only date information.
select *
from invoice
where invoice_date = '2019-06-24'
But I have into database invoice_date = 2019-06-24 04:30:00.000
I would personally use "proper" date logic:
SELECT {Column List}
FROM dbo.invoice i
WHERE i.invoice_date >= '2019-06-24'
AND i.invoice_date < '2019-06-25';
If you're using a parameter, then you would use DATEADD:
SELECT {Column List}
FROM dbo.invoice i
WHERE i.invoice_date >= #DateParam
AND i.invoice_date < DATEADD(DAY, 1, #DateParam);
There are two ways to achieve what you want. Either cast the timestamp to date, which is very readable, or use a time range.
select *
from invoice
where invoice_date >= '2019-06-24' and invoice_date < '2019-06-25';
Working with a time range is slightly less readable, but if you have an index on invoice_date it can be used, so the query may run faster.
Cast it to date:
select *
from invoice
where CAST(invoice_date as DATE) = '2019-06-24'
In fact I have a table to store the details of calls and I need to filter the calls which entered the IVR after 16:00:00 till 06:59:00 the next day for ENTIRE MONTH I have used BETWEEN clause but it includes the details of all times of the month.
SELECT
[conversationId],
[conversationStart]
FROM
[Customer].[dbo].[vw_Conversations]
WHERE
conversationStart BETWEEN '2018-01-01 16:00:00.000' AND '2018-01-31 06:59:59.000'
Any help would be appreciated
Use DATEPART() function to test hour of the day. In your case, checking hours is enough:
SELECT conversationId,
conversationStart
FROM [Customer].[dbo].[vw_Conversations]
WHERE conversationStart BETWEEN '2018-01-01' AND '2018-02-01'
AND DATEPART(hour, conversationStart) NOT BETWEEN 7 AND 15
You need to just test the time component. I am not sure what date range you want, but the query is something like this:
SELECT conversationId, conversationStart
FROM Customer.dbo.[w_Conversations c
WHERE CONVERT(date, conversationStart) >= '2018-01-01' AND
CONVERT(date, conversationStart) < '2018-02-01' AND
(CONVERT(time, conversationStart) >= '16:00:00') OR
CONVERT(time, conversationStart) < '07:00:00')
);