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

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.

Related

I've attempted a case when statement

I have attempted to use
case
when cc.create_date_time < 04/17/2019 '
then 'counted'
when cc.create_date_time > 04/17/2019
then 'need counted'
else null
end as TIME_GAP
with no luck - whatever is on the first line as in the 'counted' returns for all data even though there are results done before that date that should say need counted... how do I fix this...?
If something was counted 04/17/2019 and later its good if it has been counted before that date then I need it to tell me that... thanks
If CREATE_DATE_TIME column's datatype is DATE (it should be), why are you comparing it to strings? '04/17/2019' is a string. Either use DATE literal, or convert that string to date with the TO_DATE function and appropriate format mask, e.g.
case when cc.create_date_time < date '2019-04-17' then ...
or
case when cc.create_date_time < to_date('04/17/2019', 'mm/dd/yyyy') then ...

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

Character to number conversion

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.

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;"

Oracle Pattern matching

In Oracle I want to check whether the string has "=' sign at the end. could you please let me know how to check it. If it has '=' sign at the end of string, I need to trailing that '=' sign.
for eg,
varStr VARCHAR2(20);
varStr = 'abcdef='; --needs to trailing '=' sign
I don't think you need "pattern matching" here. Just check if the last character is the =
where substr(varstr, -1, 1) = '='
substr when called with a negative position will work from the end of the string, so substr(varstr,-1,1) extracts the last character of the given string.
Use the REGEX_EXP function. I'm putting a sql command since you didn't specify on your question.:
select *
from someTable
where regexp_like( someField, '=$' );
The pattern $ means that the precedent character should be at the end of the string.
see it here on sql fiddle: http://sqlfiddle.com/#!4/d8afd/3
It seems that substr is the way to go, at lease with my sample data of about 400K address lines this returns 1043 entries that end in 'r' in an average of 0.2 seconds.
select count(*) from addrline where substr(text, -1, 1) = 'r';
On the other hand, the following returns the same results but takes 1.1 seconds.
select count(*) from addrline where regexp_like(text, 'r$' );

Resources