Pregunta

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

  • 1 converts to 0001
  • 123 converts to 0123
¿Fue útil?

Solución

Here a possible solution:

declare @i bigint
SET @i = 125
select right( '0000' + ltrim( str( @i ) ), 4 )

Otros consejos

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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top