Home > database >  attributing value to a variable(date) in SQL Server
attributing value to a variable(date) in SQL Server

Time:12-22

I need to attribute a value to a variable and I'm in troubles. Here is my query.

DECLARE @DATA DATE

SET @DATA 
IF (SELECT DATEPART(DW, GETDATE())-1) = 1 
    SELECT CAST((GETDATE() - 3) AS DATE)
ELSE 
    SELECT CAST((GETDATE() - 1) AS DATE)

I need to place a date like '2022-12-20' in the variable, that I´ll use in another part of a second query.

CodePudding user response:

You can assign the value as part of a query using either a case expression or inline if, such as:

select @Data = DateAdd(
  day
  , Iif(DatePart(DW, DateAdd(day, -1, GetDate())) = 1, -3, -1)
  , GetDate()
);
  • Related