Generating a year prefixed serial number in SQL
An existing SQL function I was using created year-prefixed serial numbers. This year it eventually ran into a problem where it couldn’t go higher than 201502015. Here’s my complete rewrite of that function:
The function above generates an incrementing zero padded serial number with the current year prefixed, as such:
201500001, 201500002, …, 201502016
What you want to avoid is using the Replace()
function to trim out the year from the preceding serial number, because, well, it will Replace()
all over that value like nobody’s business. Here I’m using
Stuff(@CurrentMax, CharIndex(@Year, @CurrentMax), Len(@Year), '')
to only trim out the first instance of the year.