I want to get auto increment default sequence as CURRENT_TIMESTAMP (with 2 digits 'year' format) + ID (5 digits)...
Like this:
If current year is 2020,
2010001, 2010002, 2010003, ...
If current year is 2021,
2110001, 2110002, 2110003, ...
Please, help me out, and give me any idea.
Create Postgresql Default Auto Increment Sequence number based in ID concat with Current Date format
1 Answer
Use a normal sequence, set its value to 2010000, and at the beginning of 2021 set it to 2110000.
3 Comments
Felix H.
I tested and it worked like this ... SELECT setval('seq_name', (CAST(TO_CHAR(now(), 'YY') || 10001 AS INTEGER)), FALSE)
Felix H.
I also thought another way ... Can I create that auto date concat sequence by using trigger for year by year?
Laurenz Albe
That would be slower, and you still would have to reset the sequence.