New Horizons Get Schooled Blog

Use GETDATE() to pull a Monday-to-Sunday report no matter when it’s created (SQL Server)

Use GETDATE() to pull a Monday-to-Sunday report no matter when it’s created (SQL Server)In a perfect world, reports would be pulled on the same day and time every week to achieve maximum consistency. Unfortunately, mere mortals cannot maintain this regularity without fail. It only takes one unforeseen circumstance to throw the reports off.

Fortunately, the GETDATE() function replaces human fallibility and pulls a dynamic weekly performance data report starting on any specified day. This provides flexibility to the program and maintains consistency. A real win-win!

To specify the report start date use: WHERE CAST(table.Date as DATE) < CAST(DATEADD(DAYS, -DATEPART(dw, getdate())-1), getdate()) as DATE). If this code is run on a Tuesday, the results will begin on the most recent Monday and so on until Sunday.

Posted on 12/3/18 8:41 AM by Get Schooled in SQL Server, in SQL

Get Schooled

Written by Get Schooled

Search by Topic

    Lists by Topic

    see all

    Knowledge Ninjas

    a web-based service that allows software application end users to access a subject matter expert in real-time via a chat interface to resolve questions or problems they encounter in their specific software.

     

    Subscribe