Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Formatting Integer to Include leading Zeros

I am trying to write a SP to populate a column with a prefix followed by an
incremental number eg. MyData0001, MyData0002 etc. I have used a While construction
with an integer 'loop counter' which I concatenate with the prefix i.e MyData
with the resulting MyData1, MyData2 etc. I expected to be able to apply
formatting to the loop counter integer forcing leading zeros to give the
desired result but cannot find out how to do this, can anyone help?

Regards
Paul
[507 byte] By [Paul] at [2007-11-9 21:11:20]
# 1 Re: Formatting Integer to Include leading Zeros
"Paul" <enterprise.@127.0.0.1> wrote:
>
>I am trying to write a SP to populate a column with a prefix followed by
an
>incremental number eg. MyData0001, MyData0002 etc. I have used a While construction
>with an integer 'loop counter' which I concatenate with the prefix i.e MyData
>with the resulting MyData1, MyData2 etc. I expected to be able to apply
>formatting to the loop counter integer forcing leading zeros to give the
>desired result but cannot find out how to do this, can anyone help?
>
>Regards
>Paul

Use the Replicate function.
Something like:
replicate('0', 4 - len(convert(varchar(10), @next_number)))
Dave at 2007-11-11 23:51:16 >
# 2 Re: Formatting Integer to Include leading Zeros
http://www.mssqlserver.com/faq/development-zeropadinteger.asp

"Paul" <enterprise.@127.0.0.1> wrote in message
news:3e8af158$1@tnews.web.dev-archive.com...
>
> I am trying to write a SP to populate a column with a prefix followed by
an
> incremental number eg. MyData0001, MyData0002 etc. I have used a While
construction
> with an integer 'loop counter' which I concatenate with the prefix i.e
MyData
> with the resulting MyData1, MyData2 etc. I expected to be able to apply
> formatting to the loop counter integer forcing leading zeros to give the
> desired result but cannot find out how to do this, can anyone help?
>
> Regards
> Paul
David Satz at 2007-11-11 23:52:16 >