0

I am trying to selected all columns upon a certain condition. That condition is that the datetimes must be between yesterday at 5:30pm and today at 6pm. My only question is how to I get the 5:30pm? I understood how to get 5pm but how would I add the minutes.

Here is the query that I am using based off of this post

SELECT * 
FROM tbl 
WHERE datelog BETWEEN dateadd(hour,17,datediff(day,1,GETDATE())) 
                  AND dateadd(hour,18,datediff(day,0,GETDATE()))
greatFritz
  • 165
  • 1
  • 14
  • This will answer your question. - https://dba.stackexchange.com/questions/110730/query-to-get-yesterdays-data-only-for-a-specific-time-interval – Nouman Janjua Nov 20 '19 at 17:45

2 Answers2

1

Your current dateadd will return yesterday at 5:00 PM. Simply add 30 minutes to that dateadd using another dateadd. It will look like: dateadd(minute, 30, [your current dateadd])

Using your code:

dateadd(MINUTE, 30, dateadd(hour, 17, datediff(day, 1, GETDATE())))
dvo
  • 2,113
  • 1
  • 8
  • 19
0

If you are using SQL Server, I would expect:

where datelog >= dateadd(minute, 17*60+30, dateadd(hour, -24, convert(date, getdate()))) and
      datelog < dateadd(minute, 18*60, convert(date, getdate()))

datediff() does not make sense in this construct.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786