Tuesday, August 21, 2007

SQL Integer Padding

I've found a very easy way for integer padding in SQL 2005.

cast(replace(str(INT_N, 3),' ','0')as char(3)),
where INT_N is an integer number, and 3 - the size of the string required, e.g

DECLARE INT_N int
SET INT_N = 33

SELECT cast(replace(str(INT_N, 8), ' ', '0') as char(8)) as PADDED_INT

Will result in:


PADDED_INT

00000033.

No comments: