January 19, 2023 at 10:32 pm
Hello
I would like to ask you what does it show? From today and - 1 week or just last week?
cast(DATEADD(dd, DATEPART(DW,GETDATE())*-1, GETDATE()) as date)
January 19, 2023 at 11:10 pm
Gosh, Jeremy. Put a SELECT on it, run it, and see. If you can't guess that, then break out pieces and see what it comes up with for values. If you don't know the functions, look them up and science it out. For example, what does this do?
SELECT DATEPART(DW,GETDATE());
If you multiply that by -1, what does that do? If you add that resulting negative number of days to today, what does that do? If it's still not making any sense, the try changing GETDATE() to different dates and it'll become real obvious as to what the code is doing even if you can't science out the date math that's doing it.
I'd also avoid the number (DATEPART) version of DW like the plague because it changes if DATEFIRST is modified.
--Jeff Moden
Change is inevitable... Change for the better is not.
January 19, 2023 at 11:13 pm
Jeff you are right. It shows last week. My apology for this question. Thank you for helping.
January 19, 2023 at 11:40 pm
Don't apologize for the question. You got a good part of it right. Just not the "fine point". Very specifically, which part of last week does it ALWAYS show?
--Jeff Moden
Change is inevitable... Change for the better is not.
January 19, 2023 at 11:42 pm
Remember what I said about the DW datepart and DATEFIRST? What happens to the output when you change DATEFIRST and how does it relate to DATEFIRST insofar as the first day of a week that DATEFIRST assigns?
--Jeff Moden
Change is inevitable... Change for the better is not.
January 19, 2023 at 11:45 pm
To continue the math behind all of this and what the verbal description of what the code actually does, run the following code and let me know what number it returns.
SELECT @@DATEFIRST;
I promise I'll explain it but wanted you to give it a try first. I need to know what the code above returns so I can 'splain it for ya. 😀
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply