'SQL Get Data Between Two dates and Time specifically then break and go to next Date with same Time

Hello guys hope you're having a nice day.

What I'm trying to do is to get data from my db monthly. But what I want is to get data every day for example of all January:

DECLARE

  @FromDate Datetime = Convert(datetime,'2022-01-01 12:00:01'),-- all January

  @Datetime = Convert(datetime,'2022-01-31 17:00:00')

I have a table called Delivery_order_dtm which has the date and the time of item delivery.

I want every day the data between 12 :00:01 and 17:00:00 ONLY. so when the program finishes fetching data of 1-1-2022 between 12:00:01 and 17:00:00 I want it to go to 02-02-2022 and do the same thing between 12:00:01 and 17:00:00 then go to 03-03-2022 and so on.

Right now how I wrote it it is giving me all the data starting from 01-01-2022 12 PM and ending in 31-01-2022 5 PM that is not what I want.

I want everyday after fetching from 12 pm to 5 pm of this day to break and go to the next day from 12 pm to 5 pm.

Hope you can help me thank you!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source