Groups | Blog | Home
all groups > sql server (alternate) > august 2003 >

sql server (alternate) : Getting 0 padded values in the columns.


bschandramohan NO[at]SPAM yahoo.com
8/20/2003 1:36:53 AM
Getting 0 padded values in the columns.

Hi All,

I have a requirement to convert a integer to string and display it in
Sql server with fixed length say 3 chars. (in c, we wud use %03d in
printf)

If the number is small say, 9 then it has to be displayed as 009,
56 -> 056, 897-> 897, 6786 -> xxx

Checked through STR and CAST functions, couldn't find any relevant
paramters.

if you have any ideas, please mail me.

Thanks & Regards,
manrajshekar NO[at]SPAM yahoo.com
8/20/2003 11:47:33 AM
[quoted text, click to view]

Hi ,

You could use this :

select replicate('0', 3-datalength(cast(column as varchar(10)))) +
cast (column as varchar(10)) from table


Replace the column and table with the right values and here the
assumption is the column is of int datatype.

Regards,
Anith Sen
8/20/2003 6:18:46 PM
Do:

SELECT CASE WHEN LEN(@n) <= 3
THEN RIGHT('000' + CAST(@n AS VARCHAR), 3)
ELSE 'xxx'
END ;

--
- Anith
( Please reply to newsgroups only )

AddThis Social Bookmark Button