?
Solved

Determine date for Monday of current week in t-sql

Posted on 2011-03-11
3
Medium Priority
?
665 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Suggested Courses

762 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