Sybase Question:
Download Questions PDF

How to pad with leading zeros an int or smallint in Sybase?

Answer:

By example:
declare @Integer int
/* Good for positive numbers only. */
select @Integer = 1000

select "Positives Only" =
right( replicate("0", 12) + convert(varchar, @Integer), 12)

/* Good for positive and negative numbers. */
select @Integer = -1000

select "Both Signs" =
substring( "- +", (sign(@Integer) + 2), 1) +
right( replicate("0", 12) + convert(varchar, abs(@Integer)), 12)

select @Integer = 1000

select "Both Signs" =
substring( "- +", (sign(@Integer) + 2), 1) +
right( replicate("0", 12) + convert(varchar, abs(@Integer)), 12)

go

Produces the following results:

Positives Only
--------------
000000001000

Both Signs
-------------
-000000001000

Both Signs
-------------
+000000001000

Download Sybase Interview Questions And Answers PDF

Previous QuestionNext Question
Divide by zero and nulls in Sybase?What is my identity burn factor vulnerability right now in Sybase?