Procedure does not update table with NULL value

Asked

Viewed 47 times

0

Hello,

I have a little problem. I need my file to update a value that is inside a column in the table "Cashshopdata", at the moment my code is this way.

UPDATE CashShopData SET WCoinC=WCoinC+50 WHERE AccountID=@Account

I don’t know if it will be necessary to give a context to this line, but basically what is happening is that it doesn’t update the "Wcoinc=Wcoinc+50" because the value that is in the "Wcoinc" column is NULL. What I want to do is, if the value is NULL it will receive the value of 0, for that I would use an "IF", correct ?

-Exp: If Wcoinc value equals NULL then it equals 0

How do I translate this to SQL ?

I hope my doubts have been understandable, I await answers :)

1 answer

0


You can use the function ISNULL in SQL Server, basically it returns an alternative value, when the value passed to it is null:

Example:

ISNULL(WCoinC, 0)

Use in your context:

UPDATE CashShopData SET WCoinC=ISNULL(WCoinC, 0)+50 WHERE AccountID=@Account

Another alternative would be the function COALESCE, which returns the first non-null value found in a list. This solution also works in other databases such as postgres, oracle, mysql...

Example:

COALESCE(WCoinC, WCoinD, WCoinE, 0)
  • Problem solved, it worked! Thanks for the lesson, Bruno. :)

  • From what you’re saying, it seems to be the case mark an answer as accepted. Here we do not write "solved" in the question. If you have an answer that really helped you, mark it as accepted. If you came to the solution on your own, put in the solution as an answer. So content is more organized and easier to find in the future by other people with similar problems.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.