0

How do I write the following SQL Query (SSMS)

My Table has 2 columns - ColumnA and ColumnB

IF my_table.columnA < 15
  update my_table set my_table.ColumnB = 0
else IF my_table.columnA < 28
  update my_table set my_table.columnB = 1
else IF my_table.columnA < 43
  update my_table set my_table.columnB = 2
else IF my_table.columnA < 60
  update my_table set my_table.columnB = 3

Thanks for your help!

2
  • 2
    Tag your question with the database you are using. Commented Apr 23, 2018 at 15:37
  • Possible duplicate of MySQL: Update Query using If else Commented Apr 23, 2018 at 15:38

1 Answer 1

2

You seem to want a case expression:

update mytable
    set columnB = (case when columnA < 15 then 0
                        when columnA < 28 then 1
                        when columnA < 43 then 2
                        when columnA < 60 then 3
                   end)
    where columnA < 60;
Sign up to request clarification or add additional context in comments.

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.