Is it possible to get the numbers with 2 digits using the following function?
Row_number () Over(Order By UH.Stuff) AS HSN
I want the query to return 01 , 02 , 03 instead of 1,2,3...
Is it possible to get the numbers with 2 digits using the following function?
Row_number () Over(Order By UH.Stuff) AS HSN
I want the query to return 01 , 02 , 03 instead of 1,2,3...
Do something like this:
RIGHT('0' + CONVERT(VARCHAR,ROW_NUMBER() OVER (ORDER BY UH.Stuff)), 2) AS HSN
You don't need to use Convert()
function:
To get 2 digits:
RIGHT(100 + Row_number () Over(Order By UH.Stuff), 2) AS HSN
3 digits:
RIGHT(1000 + Row_number () Over(Order By UH.Stuff), 3) AS HSN
So on ...
You can use this method to zero-pad numbers in SQL Server:
select right(1000000 + Row_number () Over(Order By UH.Stuff), 2) AS HSN
What this does is add a big number to your number. The function right()
automatically converts it to a string. The right-most two digits will be zero-padded.