0

I have a column which I need to set to true or false based on a condition, is this possible to do as part of an existing update?

Example:

UPDATE  i
SET     i.Outstanding = i.Total - @Payments,
    i.Paid = @Payments ,
    i.Closed = (i.Total <= @Payments) -- THIS DOESNT WORK :(
FROM    Invoice i
JOIN    [Transaction] t ON t.Invoice_Id = i.Id
WHERE   i.Id = @InvoiceId
1
  • use case statement Commented Mar 4, 2014 at 10:04

3 Answers 3

3

You can use CASE statement

UPDATE  i
SET     i.Outstanding = i.Total - @Payments,
    i.Paid = @Payments ,
    i.Closed = CASE WHEN i.Total <= @Payments THEN 1 ELSE 0 END
FROM    Invoice i
JOIN    [Transaction] t ON t.Invoice_Id = i.Id
WHERE   i.Id = @InvoiceId
Sign up to request clarification or add additional context in comments.

Comments

0

You can use simple case statement like below

UPDATE  i
SET     i.Outstanding = i.Total - @Payments,
i.Paid = @Payments ,
i.Closed = CASE WHEN i.Total <= @Payments THEN 'Your value' ELSE 'your value' END
FROM    Invoice i
JOIN    [Transaction] t ON t.Invoice_Id = i.Id
WHERE   i.Id = @InvoiceId

Comments

0

Try this:

UPDATE  i
SET     i.Outstanding = i.Total - @Payments,
    i.Paid = @Payments ,
    i.Closed = CASE WHEN i.Total <= @Payments THEN 'true' ELSE 'false' END
FROM    Invoice i
JOIN    [Transaction] t ON t.Invoice_Id = i.Id
WHERE   i.Id = @InvoiceId

CASE Syntax:

CASE input_expression 
     WHEN when_expression THEN result_expression [ ...n ] 
     [ ELSE else_result_expression ] 
END 

Read more about CASE here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.