How can I write this update better?

Copper Contributor

The query below is to check if a specific date falls on a weekend and if it does update the date to the Monday following the weekend. Ex: date = 6/15/2024. This falls on a Saturday. In this case, the date is set to 6/17/2024.

The query works as expected, however, curious to know if there is a shorter, efficient version.

 

declare

@day int, @date datetime

set @date = convert(varchar,getdate(),101)

select @day = datepart(dw, @date)

select

   case when @day = 7 then @date + 2

        when @day = 1 then @date + 1

        else @date

   end

update table_name set run_date = @date where id = 123

0 Replies