The title explains it. I want to update a column value to add +1 to itself if it is a number, but if it is currently null, change it to a 1. This is what I've tried so far:
"UPDATE table SET colname=ISNULL(colname, 0)+1 WHERE othercolname IN('$val', '$val2')";
I'm fairly new to SQL and have looked at a bunch of things, but can't get it to work.
My Answer:
is null?
You can achieve this by using the COALESCE function in SQL. Here is an example query:
sql
UPDATE table_name
SET column_name = COALESCE(column_name, 0) + 1
WHERE condition;
In this query, the COALESCE function checks if the column value is null. If it is null, it sets the value to 0. Then, it increments the value by 1. This way, if the column value is null, it will be set to 1 and then incremented by 1.
Rate this post
3 of 5 based on 4885 votesComments