Decoding with SUBSTRING and INSTRING? - oracle

I have a table which has city column having few records with state values as well-separated by comma.
There are other records without, as well. I want to take the state values for those present into a separate field called state.
How to do that? I tried the code below and it is saying "missing right parenthesis":
SELECT DECODE(ORA_CITY,
INSTR(ORA_CITY,',') > 0,
SUBSTR(ORA_CITY, INSTR(ORA_CITY, ','), LENGTH(ORA_CITY) ) ,
NULL) AS STATE
from ADDRESS

I don't know if you still need it but use CASE:
SELECT CASE
WHEN INSTR(ORA_CITY, '5') > 0 THEN
SUBSTR(ORA_CITY, INSTR(ORA_CITY, '5'), LENGTH(ORA_CITY))
ELSE
NULL
END STATE
FROM ADDRESS

Clearly you have not understood decode syntax.
Try the following:
SELECT DECODE(INSTR(ORA_CITY,','),
0,
NULL,
SUBSTR(ORA_CITY, INSTR(ORA_CITY, ','), LENGTH(ORA_CITY) )) AS STATE
FROM ADDRESS
The correct syntax is:
DECODE( expression , search , result [, search , result]... [,
default] ), where
expression is the value to compare.
search is the value that is compared against expression.
result is the value returned, if expression is equal to search.
default is optional. If no matches are found, the DECODE function will
return default. If default is omitted, then the DECODE function will
return null (if no matches are found).
Examples here and here

SELECT REGEX_REPLACE(ORA_CITY, '.*, *', '') AS STATE
FROM ADDRESS
WHERE ORA_CITY LIKE '%,%'
This uses regular expression to replace all upto the comma, and then maybe spaces with nothing. A WHERE included.

Related

SSRS takes out leading zero if the Customer Ref contains leading zero

I have the following situation of running SSRS report with Report Builder 3.0 (SQL Server 2012).
The data field CustomerRef contains Customer Reference No which may have Cust1234 or 00001234. I want to retain the Cust1234 whereas to trim out the leading zero of 00001234 with below expression.
=IIF(Fields!CustomerRef.Value.Contains("Cust"), Fields!CustomerRef.Value, CStr(Cint(Fields!CustomerRef.Value)))
As a result, Customer Ref No with 00001234 can be changed to 1234. However, all other Custxxxx returns #Error. How do I solve this?
This is not tested but try this
=IIF(Fields!CustomerRef.Value.Contains("Cust")
, Fields!CustomerRef.Value
, CStr(Cint(
IIF(Fields!CustomerRef.Value.Contains("Cust")
,0
,Fields!CustomerRef.Value)
)
)
)
The idea here is that is the field does contain "Cust" then the CINT function sees 0 as the operand rather than the CUst1234 which will fail, even though that but of code will never get executed.
Another option (again untested) is the simpler
=IIF(Fields!CustomerRef.Value.Contains("Cust")
, Fields!CustomerRef.Value
, CStr(VAL(Fields!CustomerRef.Value))
)
As VAL() will try to turn a string into a value by extracting only the numeric parts of the string, it does not fail when presented with a string as an argument.

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

Replace NULL using a function

Is there any function to replace NULL or empty space with special character in hive? when I execute the below statement it returns a semantic exception stating trim works only on string/nvarchar
CASE
WHEN TRIM(q.address) = '' OR q.address IS NULL THEN '?'
ELSE q.address END as address
Please help.
Use LENGTH() to check the length of the column value. It returns > 0, if there is some value else return 0 for empty or NULL value.
Also frame the column value in CASE WHEN ... END block
The final query may look like:
SELECT CASE WHEN LENGTH(address) > 0 THEN address ELSE '?' END AS address
FROM table_name;
Please refer Replace the empty or NULL value with specific value in HIVE query result
Hope this help you!!!
In order to replace nulls you can use Coalesce
Coalesce( q.address, '?')
But it seems your field adress is not of the proper type to use trim, can you show us the type of that field?

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$' );

xquery- how to select value from a specific element even when that element has null values/multiple return-separated values

Please consider the following XML--
<table class="rel_patent"><tbody>
<tr><td>Name</td><td>Description</td></tr>
<tr><td>A</td><td>Type-A</td></tr>
<tr><td>B</td><td>Type-B</td></tr>
<tr><td>C</td><td>Type-C</td></tr>
<tr><td>AC</td><td>Type-C
Type-A</td></tr>
<tr><td>D</td><td></td></tr>
</tbody></table>
Now I want to select and display all values of "Name" with corresp. values of "Description" element...even when Description element has null values viz element with name=D, and also, when description element has values separated by enter then I want those values (of Description) in separate rows- viz Type-C and Type-A for element with name=AC
This is the type of query I have written--
let $rows_data:= $doc//table[#class="rel_patent"]/tbody/tr[1]/following-sibling::tr
for $data_single_row in $rows_data
return
let $cited_name:= $data_single_row/td[1]
let $original_types_w_return:= $data_single_row/td[4]
let $original_types_list:= tokenize($original_types_w_return, '(\r?\n|\r)$')
for $cited_type_each at $pos2 in $original_types_list
return concat( $cited_name, '^', $original_type_each, '^', $pos2)
However, I am getting the following type of response--
A^Type-A^1
B^Type-B^1
C^Type-C^1
AC^Type-C
Type-A^1
Now, I need to get the following correct in the above code+response---
(1) The data for "AC" should be 2 separate rows with "Type-C" and "Type-A" being in each of the 2 rows along with corresp. value for last field in each row as 1 and 2 (because these are 2 values)
(2) The data for "D" is not being shown at all.
How do I correct the above code to conform with these 2 requirements?
This works:
for $data_single_row in $rows_data
return
let $cited_name:= $data_single_row/td[1]
let $original_types_w_return:= $data_single_row/td[2]
let $original_types_list:= tokenize(concat($original_types_w_return, " "), '(\r?\n|\r)')
for $cited_type_each at $pos2 in $original_types_list
return concat( $cited_name, '^', normalize-space($cited_type_each), '^', $pos2)
(The first change was to replace $original_type_each with $cited_type_each and [4] with [2] which may ).
The first problem can be solved by removing the $ at the end of the tokenize parameter, since in the default mode $ only match the end of the string.
The second one is solved by adding an space $original_types_w_return, so it is not empty and tokenize returns something, and then removing it again with normalize-space (in XQuery 3.0 it could probably be solved by using 'allowing empty' in the for expression)

Resources