Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For Mondays I need to set default date to previous Friday for App?

Tags:

sql

sql-server

When a Report is run on a Monday it needs to set the default date to Friday ? Also 4 other conditions looking at the requirments. How to do this in an sql statement is the question.

So in pseudo code,

If today's date is Monday then set default date to Friday with full date format.

If today's date is Saturday then set default date to Friday with full date format.

If today's date is Sunday then set default date to Friday with full date format.

If any other day , then set default date to previous working day .

So need one sql statement maybe with a case statement .

Now I found these statements which give day of week so now need to do next part which may be a case statement or maybe a function ? This is the part I need assistance pl.

select datename(dw,getdate()) --Monday
select datepart(dw,getdate()) -- 1
like image 522
James Khan Avatar asked Feb 13 '13 22:02

James Khan


Video Answer


1 Answers

SET DATEFIRST 1;

DECLARE @day DATE = SYSDATETIME();

SELECT @day = DATEADD
(
  DAY, 
  CASE 
    WHEN DATEPART(WEEKDAY, @day) IN (1,2) 
      THEN  -(DATEPART(WEEKDAY, @day)+1) 
    ELSE -1 
  END, 
  @day);

SELECT @day;
like image 148
Aaron Bertrand Avatar answered Oct 14 '22 06:10

Aaron Bertrand