ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Select last week data from current date based on condition in SQL Server<p>Hi <a class="username" rel="AliYilmaz">AliYilmaz</a>,</p> <p>Refer below sample query.</p> <p><span style="text-decoration: underline;"><strong>SQL</strong></span></p> <pre class="brush: sql">CREATE TABLE #Employess (Id INT NOT NULL ,FName VARCHAR(MAX) NOT NULL,LName VARCHAR(MAX) NOT NULL) CREATE TABLE #EffortTable (Id INT NOT NULL ,FName VARCHAR(MAX) NOT NULL, EfortDate DATETIME,[Hours] INT) CREATE TABLE #HolidayTable (Id INT NOT NULL ,StartDate datetime NOT NULL,FinishDate datetime NOT NULL,HolidayName varchar(MAX) NOT NULL) INSERT INTO #Employess VALUES(1, &#39;John&#39;,&#39;Cameron&#39;) INSERT INTO #Employess VALUES(2, &#39;Cosh&#39;,&#39;Tango&#39;) INSERT INTO #Employess VALUES(3, &#39;Sam&#39;,&#39;Smith&#39;) INSERT INTO #EffortTable VALUES(1, &#39;John&#39;, &#39;2019-07-10&#39;,8) INSERT INTO #EffortTable VALUES(2, &#39;Cosh&#39;, &#39;2019-07-10&#39;,8) INSERT INTO #HolidayTable VALUES(1, &#39;2019-04-23&#39;,&#39;2019-07-20&#39;, &#39;23 Nisan Bayramı&#39;) INSERT INTO #HolidayTable VALUES(2, &#39;2019-08-10&#39;,&#39;2019-08-10&#39;, &#39;10 Agustos Bayramı&#39;) INSERT INTO #HolidayTable VALUES (3, &#39;2019-10-29&#39;, &#39;2019-10-29&#39;, &#39;29 Ekim Bayramı&#39;) DECLARE @Week_StarDate AS DATETIME SET @Week_StarDate = (SELECT DATEADD(DAY, 2 - DATEPART(WEEKDAY, &#39;2019/07/17&#39;), CAST(&#39;2019/07/17&#39; AS DATETIME)) [Week_Start_Date]) DECLARE @Week_EndDate AS DATETIME SET @Week_EndDate = (Select DATEADD(DAY, 8 - DATEPART(WEEKDAY, GETDATE()), CAST(GETDATE() AS DATETIME)) [Week_End_Date] ) IF EXISTS(SELECT * FROM #HolidayTable WHERE StartDate BETWEEN @Week_StarDate AND @Week_EndDate OR FinishDate BETWEEN @Week_StarDate AND @Week_EndDate) BEGIN SELECT [Hours] FROM #EffortTable Hours END ELSE BEGIN SELECT 0 Hours END DROP TABLE #Employess DROP TABLE #EffortTable DROP TABLE #HolidayTable</pre> <p>&nbsp;</p>https://www.aspforums.net:443/Threads/422452/Select-last-week-data-from-current-date-based-on-condition-in-SQL-Server/https://www.aspforums.net:443/Threads/422452/Select-last-week-data-from-current-date-based-on-condition-in-SQL-Server/Tue, 16 Jul 2019 08:04:23 GMT