Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

convert numeric to char

Status
Not open for further replies.

swoodring

Programmer
Dec 7, 2000
114
0
0
US
I have a numeric field that is 2 long so it has values like 1,2,55, etc, I want to convert it to a character field and then concatante a string onto into. For the numeric portion I would like it to keep the leading zeros so it wuld be 01 , 02 etc. When I use the convert or the cast function it just convert it to 1,2 =. Is there some function or option that would convert it to 01 instead? I need the leading zero so that it will sort the char field properly.
 
Use the REPLACE function in conjunction with STR. Note the second parameter (2) in the STR function. It limits the length of the output from the STR function to 2 characters. REPLACE substitutes '0' any space in the string.

Select replace(str(MyCol,2),' ','0') + 'more text' As rslt
From MyTbl Terry
------------------------------------
Experience is the hardest kind of teacher. It gives you the test first, and the lesson afterward.
 
you can also utilize the STUFF function - here's a query example:

declare @a tinyint
declare @b char(2)
select @a = 5
select @a

select @b = LTRIM(STR(@a))
select @b

select @b = STUFF(@a, 1, 0, "0")
select @b


----
5


----
5


----
05
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top