user3379269
user3379269

Reputation: 11

How can I write this update better?

A query 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. Update the date 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

Upvotes: 0

Views: 20

Answers (0)

Related Questions