[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Determine date for Monday of current week in t-sql

Posted on 2011-03-11
3
Medium Priority
?
672 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:
Ephraim Wangoya earned 750 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 750 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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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 documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Suggested Courses

826 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