Solved

Determine date for Monday of current week in t-sql

Posted on 2011-03-11
3
649 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

863 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now