Increment int by 1 in sql server?

How to return 1

if SQL below returns NULL

?

Something like (pseudocode):

if sql return NULL value 
then set value to one 
otherwise returning sql result value. 

      

Is there a SQL to define a default value of 1 if the SQL result is NULL?

SELECT  Max(iCategoryOrder)+1 
FROM    [IVRFlowManager].[dbo].[tblCategory] 
WHERE   iCategoryLevel = 1

      

+3


source to share


2 answers


Use the ISNULL operator, for example:

ISNULL(your_field, 1)

      



Try the following:

Select ISNULL(Max(iCategoryOrder), 0) + 1  
from [IVRFlowManager].[dbo].[tblCategory]  
where iCategoryLevel = 1 

      

+4


source


Option 1

Use ISNULL()

Description

Replaces a null value with the specified replacement value.

SELECT    MAX(ISNULL(iCategoryOrder, 0))+1 
FROM      [IVRFlowManager].[dbo].[tblCategory] 
WHERE     iCategoryLevel = 1

      



Option 2

Use COALESCE()

SELECT    MAX(COALESCE(iCategoryOrder, 0))+1 
FROM      [IVRFlowManager].[dbo].[tblCategory] 
WHERE     iCategoryLevel = 1

      

Description

Returns the first non-null expression among its arguments.

+5


source







All Articles