Oracle NVL wild card search - oracle

I have a search page where for null values I use NVL for null conditions. But for one field I have to do a wildcard search. Is nvl possible with wildcard search eg. NVL(null,%name%)?

You can use NVL in this case only as "subfunction"
If you want get also null-s results do it like this:
SELECT anything
FROM your_table
WHERE NVL(column_can_be_null, 'substring_you_search_for')
LIKE '%substring_you_search_for%'

select * from your_table
where column_name like NVL('%name%',column_name)

Related

Multi values search from one field PLSQL

I'm creating a report in PLSQL.
The report is working fine and additionally, I need to add when users search multiple values from one field separating by ;. It should select all the data that he entered.
Example: Salesman - Amanda; Michelle; Sharmain
Then it should select data regarding amenda, michelle and sharmain.
My code only returns data for one value only.
please refer this line from the code and ifsapp.Customer_Order_API.Get_Salesman_Code(i.order_no) LIKE '&Salesman'
where i.catalog_group='FPMB'
and i.order_no like 'M%'
and ((i.invoice_date between to_date( '&From_Date', 'YYYY/MM/DD' ) and to_date( '&To_Date', 'YYYY/MM/DD' ) ) or ('&From_Date' is null and '&To_Date' is null))
and t.source_ref1=i.order_no
and (t.source_ref3=i.release_no)
and (t.source_ref2=i.line_no)
and i.contract=t.contract
and t.transaction_code='OESHIP'
and t.qty_reversed=0
and t.source_ref1=i.order_no
and t.serial_no <> '*'
and t.cost<>0.00
and i.order_no LIKE '&Order_No
and ifsapp.Customer_Order_API.Get_Salesman_Code(i.order_no) LIKE '&Salesman'
You can use hierarchy query as follows:
ifsapp.Customer_Order_API.Get_Salesman_Code(i.order_no) IN
(SELECT TRIM(REGEXP_SUBSTR('&Salesman','[^;]+',1,LEVEL))
FROM DUAL CONNECT BY TRIM(REGEXP_SUBSTR('&Salesman','[^;]+',1,LEVEL)) IS NOT NULL)

Formatting query result

Let's say I have one query that returns column1, column2. I'd like to be able to format the result to a desirable output in Oracle.
I want to mess up with strings that's why I'm asking that question ~~.
create table taTest(
column1 varchar(50),
column2 varchar(50)
)
this is the basic query that I want to format the result from :
select distinct(column1),column2 from taTest;
desired output(for each query result) : column1value(column2value)
I've tried something like this :
select wm_concat(distinct(column1)||'('||column2||')') as result from taTest;
But it seems like I'm not using wm_concat the right way.
Thanks for any input you might provide.
This distinct(column1), column2 is one of the biggest fallacies that I've seen. Because this is same as
select distinct column1, column2. . .
Hence, all you need is this
select
column1 || '('|| column2 || ')' as result
from
taTest
group by
column1, column2;
In this case group by and distinct perform same function
Going back to distinct(column1) - this is false function. The rule of SQL parsing is that anything within ( ) considered a single word\group. Hence (column1) is separated from distinct using () vs empty space -same thing. You can do
select distinct (column1), (column2) . . -- same thing

How to use replace function in Oracle to remove a string?

In my table, I have data like PAT5DSA-(ALRP), LAR6DAOP-(RAH) etc..But I want to remove the strings like -(xxxx) or -(xxx) which can be any alphabets inside braces. Tried using the below:
select replace(:code,'-(Aa-Zz)',null) from employee;
But this didn't work..Can anyone please help?
We can do a regex replacement using REGEXP_REPLACE:
SELECT REGEXP_REPLACE('PAT5DSA-(ALRP)', '-\(.*?\)', '')
FROM dual;
PAT5DSA
The plain replace() doesn't understand patterns. You could use a regular expression replace, e.g.:
-- CTE for sample data
with cte (code) as (
select 'PAT5DSA-(ALRP)' from dual
union all
select 'LAR6DAOP-(RAH)' from dual
)
select code, regexp_replace(code, '-\(.*?\)$') as result
from cte;
CODE RESULT
-------------- --------------
PAT5DSA-(ALRP) PAT5DSA
LAR6DAOP-(RAH) LAR6DAOP
This will remove anything inside a pair of parentheses which is preceded by a dash, at the end of the original string. If the parentheses to be removed could be anywhere in the string then remove the $.
Use INSTR and SUBSTR:
WITH cteVals AS (SELECT 'PAT5DSA-(ALRP)' AS COL_VAL FROM DUAL UNION ALL
SELECT 'LAR6DAOP-(RAH)' AS COL_VAL FROM DUAL)
SELECT SUBSTR(COL_VAL, 1, INSTR(COL_VAL, '-')-1)
FROM cteVals;
Best of luck.

Oracle Select where NCLOB is Like some string

I have an Oracle table, and in this table I have a column of type NCLOB. I would like to perform a SELECT LIKE on it like so:
SELECT
*
FROM
T_WEB_TASK_IT
WHERE DBMS_LOB.substr( T_WEB_TASK_IT.ISSUE_DESCRIPTION , 32000, 1)
LIKE '%Turning on the%'
But it isn't working, I get an error saying:
String buffer too small
But I don't understand how can that be, cause I know for a fact that there aren't that many characters in that column for that particular record!
You can use DBMS_LOB.INSTR function to search for strings in the lob. Like this:
SELECT *
FROM T_WEB_TASK_IT
WHERE DBMS_LOB.INSTR( T_WEB_TASK_IT.ISSUE_DESCRIPTION , 'Turning on the') > 0
Apart from DBMS_LOB.INSTR, you could also use Regular Expressions:
SELECT *
FROM T_WEB_TASK_IT
WHERE regexp_like(issue_description, 'Turning on the')

Oracle appending string to a select statement

I have a column in a oracle table Lic_num char(7 byte)
SELECT column1, 'ABC' + Lic_num
FROM TABLE One
I wanted ABC appended to all the rows that are returned with lic_num
appended to it.
I tried tha above query and it is not working.
In Oracle it's:
SELECT column1, 'ABC' || Lic_num
FROM TABLE_ONE
This would be the way of doing it.
SELECT column1, 'ABC' || Lic_num FROM TABLE_ONE;
SELECT CONCAT(CONCAT(column1, 'ABC'), Lic_num) FROM TABLE_ONE;
If you need you can rename the concatenated Column name using AS keyword so it would be meaningful in terms of reporting.
Below info is included to help someone looking at concatenation in detail.
There are two ways to concatenate Strings in Oracle SQL. Either using CONCAT function or || operator.
CONCAT function allows you to concatenate two strings together
SELECT CONCAT( string1, string2 ) FROM dual;
Since CONCAT function will only allow you to concatenate two values together. If you want to concatenate more values than two, you can nest multiple CONCAT function calls.
SELECT CONCAT(CONCAT('A', 'B'),'C') FROM dual;
An alternative to using the CONCAT function would be to use the || operator
SELECT 'My Name' || 'My Age' FROM dual;

Resources