My table:
CREATE TABLE [dbo].[Balance] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[Balance] DECIMAL (18, 2) NOT NULL,
[Today_Date] AS (CONVERT([char](10),getdate(),(126))),
[Date_end] DATE NOT NULL,
[Remaining_Days] AS (datediff(day,CONVERT([char](10),getdate(),(126)),[Date_end])),
[In_Months] AS (datediff(day,CONVERT([char](10),getdate(),(126)),[Date_end]))/(30),
[Amount_Monthly] AS CAST((case when ((datediff(day,CONVERT([char](10),getdate(),(126)),[Date_end]))/30) = 0 then NULL else [Balance]/((datediff(day,CONVERT([char](10),getdate(),(126)),[Date_end]))/30) end) as DECIMAL(18,2)),
PRIMARY KEY CLUSTERED ([Id] ASC)
);
How it looks like:
I want it to automatically insert the Amount_Monthly into a new table so it would look like this:
E.g. if it says In_Months = 2 it should fill out January and February's Balance_monthly to 7058,82. I want it to calculate it automatically Just like I made it automatically calculate remaining_days depending on the input.
Thank you!
@Mahesh So, @Usedbyalready's answer seemed pretty overkill, I tried making it myself with case inside an update and it works perfectly.
I also made a trigger that automatically inserts the values into my Months table: