Treating a null field as zero to request an update

I am using SQL Express 2010 query builder. I need to refine the field.

In my code behind I call, for example

tableAdapter.IncrementLikeCount(id); 

If I just use the increment, then such a field can be null, so I want either a. treat zero as zero in this field OR b. set to 1 if null, and increment otherwise.

The very last thing I tried is option b with the following code in the query builder:

 UPDATE [dbo].[myTable] SET [LikeCount] = IIF(ISNULL([LikeCount]), 1, LikeCount + 1) WHERE ([ID] = @Original_ID) 

However, this does not work. The query designer continues to rewrite the expression inside ISNULL without square brackets and with a comma, as shown below:

 UPDATE [dbo].[myTable] SET [LikeCount] = IIF(ISNULL(LikeCount,), 1, LikeCount + 1) WHERE ([ID] = @Original_ID) 

Is there a simple and easy way to do this?

0
increment dbnull sql-server-express query-builder
Feb 03 2018-12-12T00:
source share
1 answer

The ISNULL statement requires a default return, for example

 ISNULL(LikeCount, 0) 

where 0 is the value that LikeCount becomes IF, in fact it is null.

So try

 UPDATE [dbo].[myTable] SET [LikeCount] = (ISNULL(LikeCount, 0) + 1) WHERE ([ID] = @Original_ID) 

UPDATE

Regarding the request that you sent in your comment:

 UPDATE Documents SET docLikeCount = ISNULL(docLikeCount, 0) + 1 WHERE docID = @Original_docID 
+5
Feb 03 2018-12-12T00:
source share



All Articles