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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Char to decimal 1

Status
Not open for further replies.

Pekka

Technical User
Mar 19, 2002
107
FI
Hi,
does anyone know how to convert character field to numeric in db2 udb. My problem is the thousand separator period(.) I can use translate function to get rid off the period but it doesn't help much.
 
Hi Greg,
yep, I have tried dec function but maybe I haven't found the proper syntax. The char value is:'10.233,50'. and when I use dec function I get error. Maybe I should first split the value from left and right of the period and then concat them again and then use dec to get it decimal?
 
Try something along these lines:

Select FIELD,DEC(REPLACE(FIELD,'.',''),7,2,',') from ..............


T. Blom
Information analyst
tbl@shimano-eu.com
 
T.Blom,
I'm Db2 udb for iSeries V5R2M0 and it doesn't support replace function and if I use translate it left space in thousand separator place.
 
If this is for one value, then why not concatenate 2 substrings (making sure you get rid of the '.') and converting the resulting string with the DEC function...?

T. Blom
Information analyst
tbl@shimano-eu.com
 
Hi Pekka,

it's a little long winded, but you could try the following (assuming in this case your value is in a field called CHAR_10):

SELECT DEC(STRIP(TRANSLATE(SUBSTR(CHAR_10,1,2),' ','.'),B)
|| STRIP(TRANSLATE(SUBSTR(CHAR_10,3,2),' ','.'),B)
|| STRIP(TRANSLATE(SUBSTR(CHAR_10,5,2),' ','.'),B)
|| STRIP(TRANSLATE(SUBSTR(CHAR_10,7,2),' ','.'),B)
|| STRIP(TRANSLATE(SUBSTR(CHAR_10,9,2),' ','.'),B),10,2)


This takes the string a pair of characters at a time, converts '.' to a space, and then trims off any spaces at either end, concatenates them all together and finally converts to a decimal (10,2)

Hope this helps
Zeat
 
Beautiful solution Zeat! It worked like a charm!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top