Nuno_147 Nuno_147 - 1 year ago 80
SQL Question

Limit a value in SQL

Not that familiar with SQL.

I have the follow SQL :

UPDATE my_Table
SET num = num + 1
WHERE id = 1

I would like limit that
column that it won't exceed a threshold, assuming 100. If
will reach 100, I would like it to stay 100 and will not increase.

I assume if statement should be included here somehow. but cant figure out how.

Answer Source

You can use a CASE expression:

UPDATE my_Table 
SET num = CASE WHEN num+1 >= 100 THEN 100 ELSE num+1 END
WHERE id=1
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download