Sql-server – SQL – Adding IF condition to the stored procedure

sql server

I have a stored procedure where I Increment the sub_days value daily,

I want to add a condition where

if sub_days = 30 then sub_months=sub_months+1 and
sub_days=0

How to add this condition to my stored procedure?

My SP:

    ALTER PROCEDURE [dbo].[Update_Users_With_Month] 
     @ID bigint


    AS
    BEGIN

       update Subscribers_Profile 
       set Sub_Updated = GETDATE() , sub_days = sub_days+1
       where sub_ID = @ID


**add condition here**

    END

Best Answer

Something like this?

ALTER PROCEDURE [dbo].[Update_Users_With_Month] 
@ID bigint

AS
BEGIN

   update Subscribers_Profile 
   set Sub_Updated = GETDATE()
      , sub_days = CASE WHEN sub_days+1 >= 30 THEN 0 ELSE sub_days+1 END
      , sub_months = CASE WHEN sub_days+1 >= 30 THEN sub_months+1 ELSE sub_months END
   where sub_ID = @ID

END

And in case you have NULLs in sub_months you can just use ISNULL in the case, like:

... THEN ISNULL(sub_months,0)+1 ELSE sub_months END ...