Solomon Behera
Solomon Behera

Reputation: 27

Auto Increment column values in SQL Server

I have two columns SALARY_CODE and FN_YEAR I want to generate table like below on the basis of FN_YEAR column: If FN_YEAR value is 18-19 then SALARY_CODE will be SAL/01-18-19 and next value of SALARY_CODE for 18-19 will be SAL/02-18-19

enter image description here

Please help me to write the SQL to perform the discussed task.

Upvotes: 0

Views: 237

Answers (4)

Divya Agrawal
Divya Agrawal

Reputation: 285

DECLARE @intCount int=0;
DECLARE @CustomID nvarchar(16);
DECLARE @fn_year nvarchar(16)='19-20';

SELECT @intCount = Count(SALARY_CODE) FROM temptest WHERE FN_YEAR=@fn_year
IF(@intCount = 0)
BEGIN
    set @CustomID='SAL/1-'+ @fn_year
    insert into temptest values (@CustomID,@fn_year)
END
ELSE
BEGIN
        SET @CustomID= 'SAL/'+ CAST(@intCount+1 as nvarchar(16)) +'-'+@fn_year;
        insert into temptest values (@CustomID,@fn_year)
END

You can write function like below:

CREATE FUNCTION dbo.getSalaryCode(@fn_year NVARCHAR(16))  
RETURNS NVARCHAR(16)   
AS   
-- Returns the SALARY_CODE 
BEGIN  
    DECLARE @intCount int=0;
    DECLARE @CustomID nvarchar(16);

    SELECT @intCount = Count(SALARY_CODE) FROM temptest WHERE FN_YEAR=@fn_year
    IF(@intCount = 0)
    BEGIN
        SET @CustomID='SAL/1-'+ @fn_year
    END
    ELSE
    BEGIN
        SET @CustomID= 'SAL/'+ CAST(@intCount+1 as nvarchar(16)) +'-'+@fn_year;
    END
    RETURN @CustomID;  
END; 

Upvotes: 1

A_kat
A_kat

Reputation: 1527

Divya Agrawal answer is pretty good. But what i think you need is a trigger to fill those values automatically when you insert something on the table. The below procedure is a sample of how to pretty much do it. Id is your table primary key.

CREATE TRIGGER [dbo].[Salary_MyTable]
ON [dbo].[MyTable]
AFTER INSERT
AS
BEGIN
@Id
SELECT @Id = INSERTED.Id FROM INSERTED
SELECT @fn_year= INSERTED.FN_YEAR FROM INSERTED
SELECT @intCount = Count(SALARY_CODE) FROM temptest WHERE FN_YEAR=@fn_year

UPDATE [dbo].[MyTable] SET [SALARY_CODE]= 'SAL/' + CAST(@intCount+1 as nvarchar(16)) +'-'+@fn_year) WHERE @Id = Id

END

Upvotes: 0

Dov Vachman
Dov Vachman

Reputation: 68

Create a table (let's call it mnths) with 12 rows (assuming all months are required) and an index colum with numbers between 1 and 12, and another table for the required years. then add rows from that table for each month and year, like this:

Table name: mnths

Field name: mnt (values: 1-12)

Table name: years

Field name: yr (values: 18-30)

SELECT 'SAL/'+CAST(mnt as nvarchar(2))+'-'+
CAST(yr as nvarchar(2))+'-'+CAST(yr+1 as nvarchar(2)) as SALARY_CODE, 
CAST(yr as nvarchar(2))+'-'+CAST(yr+1 as nvarchar(2)) as FN_YEAR
FROM mnths cross join years

If you already have the data, you can validate it against those tables and add missing entries.

Upvotes: 0

Arun Palanisamy
Arun Palanisamy

Reputation: 5459

You can use row_number() and concatenate with the other column to get the desired result

with data as ( 
(select '18-19' FN_YEAR union all 
 select '18-19' union all 
 select '18-19' union all 
 select '19-20' union all 
 select '19-20' union all 
 select '20-21')   ) 
 select  FN_YEAR, 
        ('SAL/'+CAST((row_number() OVER (PARTITION BY FN_YEAR order by FN_YEAR)) AS VARCHAR(50))+'-'+FN_YEAR) 
        as SALARY_CODE from data

Check Demo Here

Upvotes: 0

Related Questions