Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin.
Search for scripts directly from SSMS, and instantly access any saved scripts in your
SSC briefcase from the favorites tab.
Download now (direct download link)
Thank this author by sharing:
By Sean Smith,
Over the years I have had some of the strangest / most complex requirements for data querying / reporting that you could imagine. The most difficult often involves some type of strange business rule or requirement surrounding a date parameter, logic, range, etc. I've put this code together in order to pre-calculate many elements of a date's characteristics and be able to quickly determine various date characteristics / values without the need to break my head every time things get hairy.
To run it, simply choose the database in which you want the table to reside, populate the @Date_Start and @Date_End variables with the date range you want populated in the calendar, and execute.
Below is a listing of the output fields and their description using a date of 10/06/2009 (MM/DD/YYYY) as the reference example (the code should compensate for how any SQL Server instance is set up to handle the internal settings for start / end of week, weekdays, etc.). All values after the calendar_date field are specific to the date value found in each individual record.
Any friendly feedback is always welcome. Enjoy!
Find number of weekdays
Calculate last 3 Month End/Quarter End Dates
find month end/quarter end
Need SQL(Oracle) code to determine the week number of each calendar quarter
I recently implemented an MDX expression to count the number of weekdays in a given timeframe. Thi...