Solved

Determine date for Monday of current week in t-sql

Posted on 2011-03-11
3
653 Views
Last Modified: 2012-05-11
I've been asked to combine two sets of data in a query.

the first set needs to come from one table that was entered on or before noon on Monday of the current week.

the second set  comes from another table  and will show only the data that was available after that same date time.

I know that I need to do this as a union statement...but I don't know how to get the date time for the Monday date programatically.
0
Comment
Question by:thedeltacompanies
3 Comments
 
LVL 32

Accepted Solution

by:
ewangoya earned 250 total points
ID: 35113166

to get Mondays date of this week

declare @MondayDate Datetime
SET @MondayDate = DATEADD(dd, -DATEPART(dw, GETDATE()) + 2, GETDATE())
SELECT @MondayDate

or simply

SELECT DATEADD(dd, -DATEPART(dw, GETDATE()) + 2, GETDATE())
0
 
LVL 15

Assisted Solution

by:Aaron Shilo
Aaron Shilo earned 250 total points
ID: 35121331
SELECT DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 0) MondayOfCurrentWeek

0
 

Author Closing Comment

by:thedeltacompanies
ID: 35151734
Because we have a calendar table with the weekstart date and Monday as first day of week, I was able to calcuate the date using the calendar and a case statement:  

(SELECT     (CASE WHEN LS.KPI_NKI_Actuals_Day = 'Monday' THEN
                                                               (SELECT     C.WeekStart
                                                                 FROM          Reporting.dbo.Calendar C
                                                                 WHERE      C.CalendarID = FLOOR(CONVERT(float, getdate()))) WHEN LS.KPI_NKI_Actuals_Day = 'Tuesday' THEN 1 +
                                                               (SELECT     C.WeekStart
                                                                 FROM          Reporting.dbo.Calendar C
                                                                 WHERE      C.CalendarID = FLOOR(CONVERT(float, getdate()))) WHEN LS.KPI_NKI_Actuals_Day = 'Wednesday' THEN 2 +
                                                               (SELECT     C.WeekStart
                                                                 FROM          Reporting.dbo.Calendar C
                                                                 WHERE      C.CalendarID = FLOOR(CONVERT(float, getdate()))) WHEN LS.KPI_NKI_Actuals_Day = 'Thursday' THEN 3 +
                                                               (SELECT     C.WeekStart
                                                                 FROM          Reporting.dbo.Calendar C
                                                                 WHERE      C.CalendarID = FLOOR(CONVERT(float, getdate()))) WHEN LS.KPI_NKI_Actuals_Day = 'Friday' THEN 4 +
                                                               (SELECT     C.WeekStart
                                                                 FROM          Reporting.dbo.Calendar C
                                                                 WHERE      C.CalendarID = FLOOR(CONVERT(float, getdate()))) WHEN LS.KPI_NKI_Actuals_Day = 'Saturday' THEN 5 +
                                                               (SELECT     C.WeekStart
                                                                 FROM          Reporting.dbo.Calendar C
                                                                 WHERE      C.CalendarID = FLOOR(CONVERT(float, getdate()))) WHEN LS.KPI_NKI_Actuals_Day = 'Sunday' THEN 6 +
                                                               (SELECT     C.WeekStart
                                                                 FROM          Reporting.dbo.Calendar C
                                                                 WHERE      C.CalendarID = FLOOR(CONVERT(float, getdate()))) END) + CONVERT(datetime, KPI_NKI_Actuals_Time) AS Expr1
                                     FROM         LeadershipAdminApplication.dbo.Lockdown_Settings AS LS)
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question