April 5, 2023 at 11:25 am
Hi
I have a View that shows orders entered into a system using GETDATE(). However, I need it to show all orders entered between 1600 and 1559 as the 24 hour period. This then displays in a spreadsheet
So at 1610 today it would just show anything entered since 1600 today
And at 1559 tomorrow to show anything enter since 1600 yesterday until 1559 today.
I just cannot work out how to make this dynamic.
TIA
April 5, 2023 at 11:47 am
If you're on 2022 then you could use DATE_BUCKET
:
WHERE YourDateTimeColumn >= DATE_BUCKET(DAY,1, GETDATE(), CONVERT(datetime,'19000101 16:00:00'))
AND YourDateTimeColumn < DATEADD(DAY, 1, DATE_BUCKET(DAY,1, GETDATE(), CONVERT(datetime,'19000101 16:00:00')))
Another method would be to use some "date maths":
WHERE YourDateTimeColumn >= DATEADD(HOUR, 16,CONVERT(datetime,CONVERT(date,DATEADD(HOUR, -16, GETDATE()))))
AND YourDateTimeColumn < DATEADD(HOUR, 40,CONVERT(datetime,CONVERT(date,DATEADD(HOUR, -16, GETDATE()))))
Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
Larnu.uk
April 5, 2023 at 12:05 pm
Thank you so much. That has done exactly what I need.
April 5, 2023 at 7:36 pm
If you're on 2022 then you could use
DATE_BUCKET
:WHERE YourDateTimeColumn >= DATE_BUCKET(DAY,1, GETDATE(), CONVERT(datetime,'19000101 16:00:00'))
AND YourDateTimeColumn < DATEADD(DAY, 1, DATE_BUCKET(DAY,1, GETDATE(), CONVERT(datetime,'19000101 16:00:00')))Another method would be to use some "date maths":
WHERE YourDateTimeColumn >= DATEADD(HOUR, 16,CONVERT(datetime,CONVERT(date,DATEADD(HOUR, -16, GETDATE()))))
AND YourDateTimeColumn < DATEADD(HOUR, 40,CONVERT(datetime,CONVERT(date,DATEADD(HOUR, -16, GETDATE()))))
Nicely done, Thom. The "date maths" method does a million rows to a secondary table in about 289ms for both formulas... same speed as the DATE_BUCKET method.
--Jeff Moden
Change is inevitable... Change for the better is not.
April 7, 2023 at 9:00 am
This was removed by the editor as SPAM
April 10, 2023 at 12:32 am
That (now removed) ChatGPT "solution" was amazing garbage...
I saw that post and I wish they hadn't deleted it as "spam"... it provided an excellent example of just how bad the ChatGPT answers can actually be. As had been said before, "Confidently Incorrect". 😀
It was also an incredible reminder that the "A" in "AI" does NOT stand for "Actual' or "Accurate". For the low intermediate questions that I've asked of it to see what it's capable of, "AI" currently means "Agonizingly Incorrect".
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply