convert bigint to nvarchar by format
-
20-02-2021 - |
Question
How can I convert bigint
to nvarchar
using the following scheme:
1
converts to0001
123
converts to0123
Solution
Here a possible solution:
declare @i bigint
SET @i = 125
select right( '0000' + ltrim( str( @i ) ), 4 )
OTHER TIPS
Slight variation of danihp
's, but using the REPLICATE
function.
DECLARE @aVar bigint
SELECT @aVar = 123;
SELECT RIGHT(REPLICATE('0', 4) + LTRIM(STR(@aVar)), 4)
Will return 0123
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow