dbTalk Databases Forums  

Convert a int to a varchar with a specific format

comp.databases.sybase comp.databases.sybase


Discuss Convert a int to a varchar with a specific format in the comp.databases.sybase forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Fernando Silva
 
Posts: n/a

Default Convert a int to a varchar with a specific format - 07-12-2004 , 04:46 AM






Hello!

I would like to convert a int to a varchar with a specific format.
For exemple:
- 67 result in the varchar 0067
- 125 result in the varchar 0125
- 2345 result in the varchar 2345

If anyone knows a easy way to do this without using cases I would appreciate.

Thanks.

Regards,
Fernando
Edit/Delete Message

Reply With Quote
  #2  
Old   
Willie Kraatz
 
Posts: n/a

Default Re: Convert a int to a varchar with a specific format - 07-12-2004 , 08:41 AM






/* Good for positive numbers only. */

select field1, "Positives Only" =
right( replicate("0", 12) + convert(varchar, field1), 12)
from tempdb..Numbers where field1 > -1
go
field1 Positives Only
----------- --------------
1000 000000001000
0 000000000000

(2 rows affected)

/* Good for positive and negative numbers. */


select field1, "Both Signs" =
substring( "- +", (sign(field1) + 2), 1) +
right( replicate("0", 12) + convert(varchar, abs(field1)), 12)
from tempdb..Numbers
go

"Fernando Silva" <famls (AT) zmail (DOT) pt> wrote

Quote:
Hello!

I would like to convert a int to a varchar with a specific format.
For exemple:
- 67 result in the varchar 0067
- 125 result in the varchar 0125
- 2345 result in the varchar 2345

If anyone knows a easy way to do this without using cases I would
appreciate.

Thanks.

Regards,
Fernando
Edit/Delete Message



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.