Solved

SQL Select end of week date from last year

Posted on 2016-09-12
6
99 Views
Last Modified: 2016-09-12
Hello EE,

if today we are 09/16/2016, I would like to be able from the same day last year to get the Friday of the week

for that example, i woud like to return 09/18/2015
if I chose 09/09/2016   i would need to return 09/11/2015

the user will always chose a date that is a friday.  09/16/2016 is a friday and 09/09/2016 as well.

i need to return the friday's date of last year

can you help me ?
0
Comment
Question by:PhilippeRenaud
[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
  • 2
6 Comments
 
LVL 43

Expert Comment

by:zephyr_hex (Megan)
ID: 41794464
To get Friday of the current week:  

SELECT DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 4) AS Friday

Open in new window


Just replace GETDATE() with a different date to get the Friday of that week.
0
 
LVL 43

Expert Comment

by:zephyr_hex (Megan)
ID: 41794465
Oh wait, you want last year's Friday... hold on...
0
 
LVL 43

Assisted Solution

by:zephyr_hex (Megan)
zephyr_hex (Megan) earned 250 total points
ID: 41794472
So, to get last year's Friday, you would use DATEADD to get the date from last year, and then figure out the Friday from that date:
SELECT DATEADD(wk, DATEDIFF(wk,0,DATEADD(YEAR,-1,GETDATE())), 4) AS Friday

Open in new window


or, using a date other than today:

SELECT DATEADD(wk, DATEDIFF(wk,0,DATEADD(YEAR,-1,'2016-09-16')), 4) AS Friday

Open in new window

0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 70

Accepted Solution

by:
Éric Moreau earned 250 total points
ID: 41794476
or something like this which can be embedded in a UDF;
DECLARE @date DATETIME = '2016-09-09'

SET @date = DATEADD(YEAR, -1, @date)

WHILE DATEPART(WEEKDAY, @date) <> 6
	SET @date = DATEADD(DAY, 1, @date)

SELECT @date

Open in new window

0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 41794605
Code below will work with any DATEFIRST setting and does not require a loop.  I use a CROSS APPLY just to assign a name to the date calc --  you could instead just repeat the expression in the main code, as the second code block shows.

--more "self-documented" code
SELECT
    user_date_entered,
    /* back up next years max possible date to the last Friday */
    DATEADD(DAY, -(DATEDIFF(DAY, Friday, prev_year_max_date) % 7), prev_year_max_date) AS last_years_Fri_date
FROM (
    VALUES('20160916'),('20160909'),
          ('20160910'),/*test a Saturday input date to verify the code always returns Friday*/
          ('20160911'),/*test a Sunday input date to verify the code always returns Friday*/
          ('20160912')/*test a Monday input date to verify the code always returns Friday*/
) AS test_data(user_date_entered)
CROSS APPLY (
    /*push the date forward 6 days so we can "back up" to Friday*/
    SELECT 4 AS Friday, DATEADD(DAY, +6, DATEADD(YEAR, -1, user_date_entered)) AS prev_year_max_date
) AS assign_alias_names


--minimum amount of code
SELECT
    user_date_entered,
    DATEADD(DAY, -(DATEDIFF(DAY, 4,DATEADD(DAY, +6, DATEADD(YEAR, -1, user_date_entered))) % 7),
        DATEADD(DAY, +6, DATEADD(YEAR, -1, user_date_entered))) AS last_years_Fri_date
FROM (
    VALUES('20160916'),('20160909'),
          ('20160910'),/*test a Saturday input date to verify the code always returns Friday*/
          ('20160911'),/*test a Sunday input date to verify the code always returns Friday*/
          ('20160912')/*test a Monday input date to verify the code always returns Friday*/
) AS test_data(user_date_entered)
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 41794909
Just don't use that code in Europe :-) :

SET LANGUAGE ENGLISH

DECLARE @date DATETIME = '2016-09-09'

SET @date = DATEADD(YEAR, -1, @date)

WHILE DATEPART(WEEKDAY, @date) <> 6
      SET @date = DATEADD(DAY, 1, @date)

SELECT @date


SET LANGUAGE GERMAN

SET @date = DATEADD(YEAR, -1, @date)

WHILE DATEPART(WEEKDAY, @date) <> 6
      SET @date = DATEADD(DAY, 1, @date)

SELECT @date
0

Featured Post

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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.
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…

635 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