For this to be possible there must actually be a column that holds this information. But if there is, it is simple:
SELECT *
FROM tbl
WHERE datecol >= '20210913 05:00'
AND datecol < '20210914 05:00'
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hello All,
I want to query records between two dates and times within those dates.
So, if I wanted to get all records from 09-13-2021 at 05:00:00 in the morning through 09-14-2021 at 04:59:59 the following morning, how would I go about it?
Thanks ADawn
For this to be possible there must actually be a column that holds this information. But if there is, it is simple:
SELECT *
FROM tbl
WHERE datecol >= '20210913 05:00'
AND datecol < '20210914 05:00'
Hello ErlandSommarskog,
There are two columns: Date and Time.
thanks ADawn
Can anyone answer this post?
Hi @Cant-We-Al-lJust- Get-Along ,
Welcome to the microsoft TSQL Q&A forum!
For this type of problem we recommend that you post CREATE TABLE statements for your tables together with INSERT statements with sample data.We also need to see the expected result of the sample.So that we’ll get a right direction and make some test.
Please try:
;WITH cte
as(SELECT * FROM yourtable
WHERE Datecolumn BETWEEN '2021-09-13' AND '2021-09-14')
SELECT * FROM cte
WHERE Timecolumn BETWEEN '05:00:00' AND ' 04:59:59'
If this does not solve your problem, please provide more information.
If you have any question, please feel free to let me know.
Regards
Echo
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Just in case you are still interested, and you do not want to make datetime from date and time, then check this approach too:
select * from MyTable
where ( D = '2021-09-13' and T >= '05:00' )
or ( D = '2021-09-14' and T < '05:00' )
where D is the date column, T — the time column.