Domanda

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

  • 1 converts to 0001
  • 123 converts to 0123
È stato utile?

Soluzione

Here a possible solution:

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

Altri suggerimenti

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top