You are here: Re: How can I conserve the initial zero when convert numeric to string using STR() « MsSQL Server « IT news, forums, messages
Re: How can I conserve the initial zero when convert numeric to string using STR()

Posted by Tony Rogerson on 05/29/06 13:28

Hi Angellian,

For 2 character string you can just use CASE...

declare @number tinyint
set @number = 2

select case when @number between 0 and 9 then '0' else '' end + cast(
@number as varchar(2) )

Otherwise, if your resultant string needs to be bigger than 2 characters do
this...

declare @number int
declare @string varchar(10)
declare @size_of_fixed_string tinyint
set @size_of_fixed_string = 10
set @number = 40

print replicate( '0', @size_of_fixed_string )

set @string = left( replicate( '0', @size_of_fixed_string ),
@size_of_fixed_string - len( @number ) ) + cast( @number as varchar(10) )

print @string

http://sqlblogcasts.com/blogs/tonyrogerson/archive/2006/05/29/765.aspx

--
Tony Rogerson
SQL Server MVP
http://sqlblogcasts.com/blogs/tonyrogerson - technical commentary from a SQL
Server Consultant
http://sqlserverfaq.com - free video tutorials


<angellian@gmail.com> wrote in message
news:1148779910.070643.296910@g10g2000cwb.googlegroups.com...
> Sorry to raise a stupid question but I tried many methods which did
> work.
> how can I conserve the initial zero when I try to convert STR(06) into
> string in SQL statment?
> It always gives me 6 instead of 06.
>
> Thanks a lot.
>

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация