walangala
walangala

Reputation: 241

Performing an Update query with a date

Can I perform an update query by checking the value of the date whether the date entered is greater than the date stored in the database.

IF @input_date > table.date
  UPDATE table SET table.value = 0, table.date = @input_date
ELSE 
  UPDATE table SET table.value = @input_value, table.date = @input_date

Upvotes: 0

Views: 35

Answers (2)

Rob Hesje
Rob Hesje

Reputation: 82

Another way to do this would be putting the test into the where condition of the update statement. So that the update only happens when the condition is met

update
    table
set
    table.value = @input_date
where
    table.value < @input_date

Upvotes: 0

Vignesh Kumar A
Vignesh Kumar A

Reputation: 28403

Use CASE Statement

Try this

    UPDATE table 
    SET table.value = CASE WHEN @input_date > table.date THEN 0 ELSE @input_value END,
        table.date = @input_date

Upvotes: 3

Related Questions