Re: How can I conserve the initial zero when convert numeric to string using STR()



You can't "preserve" the zero. Integer 06 = Integer 6 = Integer
00000000006.

If you want to convert an integer to a varchar you can prepend a 0 character
to the result:

SELECT '0' + CAST(06 AS VARCHAR)

Returns '06'. The downside to this method is that if you do something like

SELECT '0' + CAST(10 AS VARCHAR)

You'll end up with '010' which may or may not be what you want. You can
build on this example with the SUBSTRING function to get exactly what you
really want out of it.

<angellian@xxxxxxxxx> wrote in message
news:1148779910.070643.296910@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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.



.



Relevant Pages

  • Re: Conversion error
    ... You canimport both types of data into a varchar column now. ... Allan Mitchell MCSE,MCDBA, (Microsoft SQL Server MVP) ... www.SQLDTS.com - The site for all your DTS needs. ... >>character data has to be one or the other. ...
    (microsoft.public.sqlserver.dts)
  • Re: Char and Varchar
    ... >>If changing an element to full size causes it to no longer fit on the ... > forwarding pointer is changed. ... >>Would you ever create a table with lets say 100 single character VARCHAR ...
    (microsoft.public.sqlserver.server)
  • Re: Char and Varchar
    ... >>If changing an element to full size causes it to no longer fit on the ... > forwarding pointer is changed. ... >>Would you ever create a table with lets say 100 single character VARCHAR ...
    (microsoft.public.sqlserver.programming)
  • Re: trying to write my 1st stored procedure
    ... non-Unicode character data with a length of n bytes. ... for varchar are char varying or character varying. ... @bugtext nvarchar, @idkey int OUTPUT ... it is a new bug and return -1, ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: varchar problem
    ... A space is just another character. ... >> want to strip them, ... VARCHAR strings will only be as long as ... > the stored string itself. ...
    (alt.php)