Character to number conversion - oracle

I have a column with for eg values:
*.4176,
*0.2734,
$1.53,
$122.00,
D0.4645,
D.2464,
*0.3426,
*.3426,
$0.0/$2.50,
-0.0/-2.50
I need to write a query to display only the numerical part in numerical format. I want to create a detail object for the main object in business object.
I tried using translate but it has a lot of hardcoding and the slash is creating problem to convert it into TO_BINARY_DOUBLE. Is there anyother way to avoid hardcoding and still display numbers in numerical format?

Try this:
SELECT TRANSLATE('*4176', ' */''', ' ') FROM DUAL;
This is the case when you have the symbol * in the column, just to get the idea. Perform some more translates for the other cases as well.
Cheers

I require output in following format:
Cost Formula Cost Formula Code Cost Formula Value Cost calculation
$520.80 $ 520.8
*1 * 1
-0.0/-$20 -0.0/-$20
+0.0/-$36 +0.0/-$36
select
cost_formula,
case
when TRANSLATE(COST_FORMULA,'1234567890.-+ABCDEFGHIJKLMNOPQRSTUVWXYZ!##%^&*()~`_[]{};<>:"*$',' ')='/'
then ''
else TRANSLATE(COST_FORMULA,'1234567890.-+',' ')
END code,
case
when TRANSLATE(COST_FORMULA,'1234567890.-+ABCDEFGHIJKLMNOPQRSTUVWXYZ!##%^&*()~`_[]{};<>:"*$',' ')='/'
then '0'
else TO_BINARY_DOUBLE(RTRIM(LTRIM(TRANSLATE(UPPER(COST_FORMULA),'ABCDEFGHIJKLMNOPQRSTUVWXYZ!##%^&*()~`_[]{};<>:"*$','0'))))
END value,
case
when TRANSLATE(COST_FORMULA,'1234567890.-+ABCDEFGHIJKLMNOPQRSTUVWXYZ!##%^&*()~`_[]{};<>:"*$',' ')='/'
then cost_formula
else ''
END Calculation
FROM RSE.RSE_SO_F_ORDERS;
I am using this query to get the required format. But the numerical part is throwing the following error:
ORA-00932: inconsistent datatypes: expected CHAR got BINARY_DOUBLE
00932. 00000 - "inconsistent datatypes: expected %s got %s"
And i want the numerical part to be as a float.

Related

ORA-01722 invalid number on using the expression in select and while joining

CASE WHEN (r.code_value4 = 0 AND LENGTH(ltrim(rtrim(xx.AFFILIATE_CODE))) > 0) AND
r_intercompany.code_value1 is not null AND
ltrim(rtrim(xx.AFFILIATE_CODE)) <> (CASE WHEN xx.COMPANY_CODE_JE_EXCEPTION_FLAG = 1
THEN r.code_value3 ELSE r.code_value1 END)
THEN r_intercompany.code_value1
ELSE NVL(r_mga_acct.code_value2, xx.ACCOUNT_NUMBER) END
I have view in which the above part of the sql is being used in select statement and as well as to compare with a field while joining. When I run the view after making this change, it is throwing the error ORA-01722 invalid number.
Please let me know on how to correct this.
Thanks
" it is throwing the error ORA-01722 invalid number."
It is likely somewhere you are comparing a numeric column with a string column . Oracle is implicitly casting the string to a number but the column contains values which cannot be converted, so it hurls.
"Please let me know on how to correct this."
Don't rely on implicit data conversion. Go through your code, check the data types of all the columns. Where you find a varchar2 column being compared to a number you need to cast the number to a string. For literals that means quoting them - '1' instead of 1 - and for columns that means wrapping them in to_char() calls.
My Suspicion is either r.code_value4 field or xx.COMPANY_CODE_JE_EXCEPTION_FLAG field is String type hence try the below
CASE WHEN (r.code_value4 = '0' AND LENGTH(ltrim(rtrim(xx.AFFILIATE_CODE))) > 0) AND
r_intercompany.code_value1 is not null AND
ltrim(rtrim(xx.AFFILIATE_CODE)) <> (CASE WHEN xx.COMPANY_CODE_JE_EXCEPTION_FLAG = '1'
THEN r.code_value3 ELSE r.code_value1 END)
THEN r_intercompany.code_value1
ELSE NVL(r_mga_acct.code_value2, xx.ACCOUNT_NUMBER) END

Cannot cast object '(10)' with class 'java.lang.String' to class 'java.lang.Integer'

I am using ireport 3.7.1. I have made a connection with my database.I have a procedure which when given an input in number ,it returns the word format of the number i.e if I give input 10,it will return ten. The problem is when I am executing the procedure in pl/sql developer,I am getting the proper output but when I am firing the same procedure in ireport it's giving me this exception
Cannot cast object '(10)' with class 'java.lang.String' to class 'java.lang.Integer' .
Casting straight from a String to an Integer is not possible. You'll want to use the function Integer.parseInt(stringNumber);
(10) isn't a properly formated integer. Not even for PL/SQL:
select '(10)' +0 from dual;
> ORA-01722: invalid number
I could only suggest you to trace back the point where those ( ) come from. And fix your code at that position instead. Just a wild guess, some number formats use parenthesis to represent negative numbers. Maybe this is your case?
That being said, if you still want to locally remove the parenthesis that have somehow lurked inside of your string:
String str = "(10)";
int value = Integer.parseInt(str.substring(1, str.length()-1));
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// *blindly* get away of first and last character
// assuming those are `(` and `)`
For something a little bit more robust, and assuming parenthesis denotes negative numbers, you should try some regex:
String str = "(10)";
str = str.replaceFirst("\\(([0-9]+)\\)", "-$1");
// ^^^ ^^^ ^
// replace integer between parenthesis by its negative value
// i.e.: "(10)" become "-10" (as a *string*)
int value = Integer.parseInt(str);

Select in ADO (vb6) with a numeric variable

Excuse me, occasionally I refer with some problem that maybe it's already been fixed. In any case, I would appreciate a clarification on vs.
I have a TariffeEstere table with the fields country, Min, Max, tariff
from which to extract the rate for the country concerned, depending on whether the value is between a minimum and a maximum and I should return a single record from which to extract its tariff:
The query is:
stsql = "Select * from QPagEstContanti Where country = ' Spain '
and min <= ImpAss and max >= ImpAss"
Where ImpAss is a variable of type double.
When I do
rstariffa.open ststql,.....
the recodset contains a record if e.g. ImpAss = 160 (i.e. an integer without decimals), and then the query works, but if it contains 21,77 ImpAss (Italian format) does not work anymore and gives me a syntax error.
To verify the contents of the query string (stsql) in fact I find:
Select * from QPagEstContanti Where country = 'Spain' and min < = 21,77 and max > = 21,77
in practice the bothering and would like a comma decimal, but do not know how do.
I tried to pass even a
format (ImpAss, "####0.00"),
but the value you found in a stsql is 21,77 always.
How can I fix the problem??
It sounds like the underlying language setting in SQL is expecting '.' decimals instead of ',' decimal notation.
To check this out - run the DBCC useroptions command and see what the 'language' value is set to. If the language is set to English or another '.' decimal notation - it explains why your SQL string is failing with values of double.
If that's the problem, the simplest way to fix it is to insert the following line after your stsql = statement:
  stsql = REPLACE(stsql, ",", ".")
Another way to fix it would be to change the DEFAULT_LANGUAGE for the login using the ALTER LOGIN command (but this changes the setting permanently)
Another way to fix it would be to add this command to the beginning of your stsql, which should change the language for the duration of the rs.Open:
  "SET LANGUAGE Italian;"

comp_cd > to_char('0000000000','9999999999')

I have come across an SQL statement where one of the conditions is comp_cd > to_char('0000000000','9999999999').
Running select to_char('0000000000','9999999999') from dual i am getting the result "0".
Does anyone has come across this?
The function TO_CHAR expects a NUMBER or DATE as its first parameter, but you're providing a string ('0000000000') instead.
Therefore, Oracle uses an implicit conversion to convert it to a NUMBER first; '0000000000' is converted to the number 0.
Then, TO_CHAR converts 0 back to a string using the '9999999999' format model. This should result in the string:
' 0'
Finally:
comp_cd > ' 0'
would do a lexical (alphabetical) comparison between two strings.

addig char in select statement

I want to add char in Select statement.
Ex:
SELECT '.' + OUTTRUNK as NUMBER
Expected Result:
.348977834
.456935534
.090922834
.234999734
How can I do this?
Thanks.
Try the more SQL-ish:
SELECT '.' || OUTTRUNK as NUMBER
but keep in mind that, if OUTRUNK is a numeric rather than string type, you'll probably find that 090922834 will actually be 90922834 and will render as .90922834, not what you want.
If that's the case, you're probably looking for something more like:
SELECT OUTTRUNK / 1000000000 as NUMBER
(check the number of zeros there, I tried to get it right but testing is rightly your concern).

Resources