I want to get the results by applying LIKE operator. But its not giving me the result.
Below is my Query.
Select * from TBL_ZONAL_HEAD_INFO where upper(ZONE_HEAD_NAME) LIKE '%ami%';
The above query is not fetching me any results from the data below
Query in procedure
Select * from TBL_ZONAL_HEAD_INFO where upper(ZONE_HEAD_NAME) LIKE '%' || upper(P_ZONENAME) || '%';
upper(ZONE_HEAD_NAME) is all uppercase, so it will never be LIKE '%ami%' (which is all lowercase).
You have to match it with UPPER CASE.
SELECT
*
FROM
TBL_ZONAL_HEAD_INFO
WHERE
UPPER(ZONE_HEAD_NAME) LIKE upper('%Ami%'); -- used upper case AMI here
Cheers!!
Use LOWER not UPPER:
Select * from TBL_ZONAL_HEAD_INFO where LOWER(ZONE_HEAD_NAME) LIKE '%ami%';
or use upper case in the LIKE:
Select * from TBL_ZONAL_HEAD_INFO where UPPER(ZONE_HEAD_NAME) LIKE '%AMI%';
or wrap both sides of the LIKE in the same case-transforming function:
Select * from TBL_ZONAL_HEAD_INFO where UPPER(ZONE_HEAD_NAME) LIKE UPPER('%Ami%');
try this :
Select * from TBL_ZONAL_HEAD_INFO where upper(ZONE_HEAD_NAME) LIKE upper('%ami%');
You have used upper for ZONE_HEAD_NAME and used '%ami%' which won't ever return any records, ever.
So, either make both in upper() function, or just use Uppercase for select query here, like '%AMI%'.
Related
Is it possible to run a query which matches ANY rows in another table column? I'm trying to run this for example:
SELECT *
FROM emails
WHERE address ILIKE '%#' || IN (select * from dictionary.wordlist) || '.%'
However this returns [Vertica]VJDBC ERROR: Subquery used as an expression returned more than one row
Now that's a strange way of formulating it...
If you go back to a basic SQL tutorial, you will understand that a string literal like '%#' , which can be the operand of an ILIKE predicate, cannot be concatenated with an IN () clause - which is a predicate in itself.
I assume that you are looking for all rows in the emails table whose address contains any of the words in dictionary.wordlist between the at-sign and a dot.
I hope (correct me if I'm wrong) that dictionary.wordlist is a table with one column in VARCHAR() or other string format. If that is the case, you can go like this:
WITH
-- out of "dictionary.wordlist", create an in-line-table containing a column
-- with the wildcard operand to be later used in an ILIKE predicate
operands(operand) AS (
SELECT
'%#'||wordlist.word||'.%'
FROM dictionary.wordlist
)
SELECT
emails.*
FROM emails
INNER JOIN operands
ON email.address ILIKE operands.operand
;
There are other ways of doing it, of course, but this is one of them.
I'm not trying to say it will be very fast - an ILIKE predicate as a JOIN condition can't be performant ...
Good luck
Marco the Sane
I have a problem which I can't solve. Maybe you have an idea about how to solve it.
I do have a given parameter-table like this:
P_VALUE P_NAME
----------- ----------
X85 A_03
XH1 A_04
XH2 A_04
XH3 A_04
C84 A_05
As you can see there are parameters with multiple entries. At the moment this parameters are used in this way:
SELECT * FROM tablex
WHERE code IN (SELECT p_value
FROM parameter_table
WHERE p_name LIKE 'A_04');
As the query is very big these parameter sub-select are used very often. I was trying to implement a function in Oracle to get my parameters. This works very fine as long as there is just 1 row per parameter. When I want to use it in "IN-Statements", it won't work because functions just return a single value.
--WORKS
SELECT * FROM tablex
WHERE code = (f_get_param('A_03'));
--DOES NOT WORK
SELECT * FROM tablex
WHERE code IN (f_get_param('A_04'));
Please note that I need it for plain SQL statements, so procedures won't work as they are just good for PL/SQL.
I would be really thankful for good ideas or help!
Use collections. Here you have an example http://www.adp-gmbh.ch/ora/plsql/coll/return_table.html
Technically you can achieve using the function this way but doing this will cause index not to be used on code column on tablex and may affect performance .Using function index you can reduce performance impact
CREATE OR REPLACE FUNCTION f_get_param(p_value1 IN VARCHAR2,p_name1 in VARCHAR2) return NUMBER
DETERMINISTIC
IS
l_count NUMBER;
BEGIN
select count(1) into l_count from parameter_table where p_value =p_value1
and p_name=p_name1;
if l_count > 0
then
return 1;
else
return 0;
end if;
end f_get_param;
AND use the select statement like this
SELECT * FROM tablex
WHERE f_get_param(code,'A_04')=1;
EDIT 1:-
Also to reduce the performance impact in database 10.2 and greater If the parameter_table is static you can use the DETERMINISTIC clause in the Function to say that the function returns the same value if called with same parameters every time
Please find the link on the article about using functions in SELECT statement
--DOES NOT WORK
SELECT * FROM tablex
WHERE code IN (f_get_param('A_04'));
-- Try this
SELECT * FROM tablex
WHERE code IN (select * from TABLE(f_get_param('A_04')));
You have to "CAST" a collection onto SQL TABLE.
Also when you use cast you can also use inner joint:
SELECT * FROM tablex join TABLE(f_get_param('A_04') using (code);
I think - generally - your problem is called "Dynamic where clause". Try to search some articles about it on AskTom.
I think the actual solution to your problem is to simply join the two tables and create the appropriate indexes rather than invoking a PL/SQL function at all:
SELECT x.* FROM tablex x, parameter_table p
WHERE x.code = p.p_value
AND p.p_name LIKE '%A_04%';
Note that you also have a semantic error in your LIKE clause. You're not using the % sign therefore your LIKE 'A_04' is just the same as = 'A_04'
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)
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')
I'm trying to search DB for records based on Date. But the search is based in month and year. i.e mm/yyyy and dd is to be wild-card.
My search query looks like this:
Select ucid, uc_name, From (UC_Table1)
where UC_Date like To_Date('11/*/2011','mm/dd/yyyy')
this gives me the following error:
ORA-01858: a non-numeric character was found where a numeric was expected, So obviously it doesn't like * or % or _ or ? as wild-cards for dd.
Wildcards do not work like that within a function. The To_Date() function parses out the * before the LIKE has a chance to see it. Consider:
SELECT ucid, uc_name
FROM UC_Table1
WHERE UC_Date >= To_Date('11/01/2011', 'mm/dd/yyyy')
AND UC_Date < To_Date('12/01/2011', 'mm/dd/yyyy')