I have this SQL:
update entity_table set views = views + 1 where id = {id of entity}
the views column is nullable. So this only works if the column has a value which is not null.
How can I make this statement set the value to 1 if it is null and increment otherwise?
Thanks.
UPDATE entity_table
SET views = Coalesce(views, 0) + 1
You can use Isnull
also in place of Coalesce
as Isnull
is comparatively faster than Coalesce
UPDATE entity_table
SET views = isnull(views, 0) +1
Check out this link for understanding the performance difference between the two:- http://weblogs.sqlteam.com/mladenp/articles/2937.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With