Auto Increment no

51 views Asked by At

I want to auto-increment a serial number:

"ATN/01-1920" WHERE ATN--given text,01-auto increment,1920--current financial

The result should be like this:

'ATN/01-1920','ATN/02-1920','ATN/03-1920'.....

Also, in a new financial year it should be reset like this: 'ATN/01-2021'

1

There are 1 answers

3
Ajan Balakumaran On

Give your start date and end date then the script will generate the serial accordingly pass the date in the format YYYYMMDD

SELECT  concat('ATN/',format(DATEADD(month, nbr - 1, '19200101'),'MM'),'-', year(DATEADD(month, nbr - 1, '19200101')))
FROM    ( SELECT    ROW_NUMBER() OVER ( ORDER BY c.object_id ) AS Nbr
          FROM      sys.columns c
        ) nbrs
WHERE   nbr - 1 <= DATEDIFF(MONTH, '19200101', SYSDATETIME())