Question

How can I convert bigint to nvarchar using the following scheme:

  • 1 converts to 0001
  • 123 converts to 0123
Was it helpful?

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
scroll top