0

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'

skomisa
  • 16,436
  • 7
  • 61
  • 102

1 Answers1

0

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())
Ajan Balakumaran
  • 1,639
  • 1
  • 8
  • 16