Oracle LIKE pattern match - sql

I have 10 records in a table. Out of these records, I need to perform the following 3 operations using a single Oracle query(the reason for saying single query is that this is part of an automation framework and I need to keep a single generic query)
Operation 1: get all the 10 records
Select * from table_name where col01 like ('%')
<<10 records fetched>>
Operation 2: get records that start with the string "Tivoli"
Select * from table_name where col01 like ('Tivoli%')
<<1 record fetched>>
Operation 3: get records that DOES NOT start with "Tivoli"
<<should give 9 records>>
I am Not able to write the query for operation 3 but I do not want to use a separate NOT LIKE clause as this would make me create a separate query altogether.

Then, you can use minus set operator :
Select * from table_name -- all records
minus
Select * from table_name where col01 like 'Tivoli%'
-- records for col01 column starts with Tivoli
If all types of Tivoli( such as TIVOLI.. or tIvOli or .. ) should be included case-insensitively, then you can consider the condition as
where lower(col01) like 'tivoli%'

You can use:
select *
from table_name
where col01 not like 'Tivoli%'
If col01 can be NULL, then you need to take that into account:
select *
from table_name
where col01 not like 'Tivoli%' or col01 is null;

Select * from table_name where not (col01 like ('Tivoli%') )

You are asking for a generic query that covers all three options. Use two variables for this and set them null when you don't want to use them:
Select *
from table_name
where (col01 like :must_match or :must_match is null)
and (col01 not like :must_not_match or col01 is null or :must_not_match is null);
Or, if you want to have your framework deal with match or no match, select all rows with a flag:
Select
t.*,
case when col01 like :pattern then 'match' else 'no match' end as is_match
from table_name t;

Related

How to filter out null values

The results of my SQL query include null values. How do I filter out null values?
The syntax may vary depending on the database you are using but you can explicitly exclude nulls in the where clause. For example, the following will exclude null values in the primary_author field:
SELECT
date,
primary_author,
ISBN
FROM
books
WHERE
primary_author IS NOT NULL;
My example works on every database I know, so it should work for you =)
SELECT *
FROM TABLE_NAME
WHERE COLUMN_NAME IS NOT NULL
Here you can find a simple explanation and some examples: https://www.w3schools.com/sql/sql_null_values.asp
But some times you want to replace null values for a default value, like 'X', in this case, we should know the database for correct syntax, here some examples:
Oracle:
SELECT nvl(column_name,'X')
FROM TABLE_NAME
Sqlite:
SELECT ifnull(column_name,'X')
FROM TABLE_NAME
SqlServer:
SELECT coalesce(column_name,'X')
FROM TABLE_NAME

sql query to find a column value with specific text available

Is their any other way to find a column values with the specified text available in it.
i know their is one way like,
SELECT COLUMN_NAME FROM TABLE_NAME
WHERE COLUMN_NAME LIKE '%sample_text%'
and i tried
SELECT COLUMN_NAME FROM TABLE_NAME
WHERE CONTAINS(COLUMN_NAME,'sample_text')
also but it requires table to be full-text indexed
You can use PATINDEX():
SELECT COLUMN_NAME FROM TABLE_NAME
WHERE PATINDEX('%sample_text%', COLUMN_NAME) != 0
Query 1
select 1 where patindex('%find_string%','find_stringfdasfsa')> 0
Query 2
select 1
where len(REPLACE('find_stringfdasfsa','find_string',''))<LEN('find_stringfdasfsa')
Query 3
select 1
where CHARINDEX('find_string','find_stringfdasfsa') > 0
Execution plan is same as Like. You will have to check the performance.

How do I do an IF in Microsoft SQL SELECT query?

I am an absolute beginner in SQL.
Example:
I want to do a query to select people whose names begin with X if the result is 0, I want to select people whose names begin with Y.
How do I do this? Thanks.
I interpret the question as: show names starting with Y if no names starting with X are found.
This solution will be fast as it will short-cut the exists from the moment 1 record is found
if exists(select * from table where name like 'X%')
begin
select * from table where name like 'X%'
end
else
begin
select * from table where name like 'Y%'
end
Ideally the name column is indexed for this to work well.
May be this is a better solution:
select * from table where name like 'X%'
if ##ROWCOUNT = 0
select * from table where name like 'Y%'
There are ways to do perform this in a single SQL statement, but they are extremely expensive (both in time and resources). You would be best served to do this kind of logic in a stored procedure.
Here's a set-based solution:
SELECT *
FROM table
WHERE NAME LIKE 'X%'
UNION
SELECT *
FROM table
WHERE NAME LIKE 'Y%'
AND NOT EXISTS (
SELECT *
FROM table AS T1
WHERE T1.NAME LIKE 'X%'
);

Using LIKE in an Oracle IN clause

I know I can write a query that will return all rows that contain any number of values in a given column, like so:
Select * from tbl where my_col in (val1, val2, val3,... valn)
but if val1, for example, can appear anywhere in my_col, which has datatype varchar(300), I might instead write:
select * from tbl where my_col LIKE '%val1%'
Is there a way of combing these two techniques. I need to search for some 30 possible values that may appear anywhere in the free-form text of the column.
Combining these two statements in the following ways does not seem to work:
select * from tbl where my_col LIKE ('%val1%', '%val2%', 'val3%',....)
select * from tbl where my_col in ('%val1%', '%val2%', 'val3%',....)
What would be useful here would be a LIKE ANY predicate as is available in PostgreSQL
SELECT *
FROM tbl
WHERE my_col LIKE ANY (ARRAY['%val1%', '%val2%', '%val3%', ...])
Unfortunately, that syntax is not available in Oracle. You can expand the quantified comparison predicate using OR, however:
SELECT *
FROM tbl
WHERE my_col LIKE '%val1%' OR my_col LIKE '%val2%' OR my_col LIKE '%val3%', ...
Or alternatively, create a semi join using an EXISTS predicate and an auxiliary array data structure (see this question for details):
SELECT *
FROM tbl t
WHERE EXISTS (
SELECT 1
-- Alternatively, store those values in a temp table:
FROM TABLE (sys.ora_mining_varchar2_nt('%val1%', '%val2%', '%val3%'/*, ...*/))
WHERE t.my_col LIKE column_value
)
For true full-text search, you might want to look at Oracle Text: http://www.oracle.com/technetwork/database/enterprise-edition/index-098492.html
A REGEXP_LIKE will do a case-insensitive regexp search.
select * from Users where Regexp_Like (User_Name, 'karl|anders|leif','i')
This will be executed as a full table scan - just as the LIKE or solution, so the performance will be really bad if the table is not small. If it's not used often at all, it might be ok.
If you need some kind of performance, you will need Oracle Text (or some external indexer).
To get substring indexing with Oracle Text you will need a CONTEXT index. It's a bit involved as it's made for indexing large documents and text using a lot of smarts. If you have particular needs, such as substring searches in numbers and all words (including "the" "an" "a", spaces, etc) , you need to create custom lexers to remove some of the smart stuff...
If you insert a lot of data, Oracle Text will not make things faster, especially if you need the index to be updated within the transactions and not periodically.
No, you cannot do this. The values in the IN clause must be exact matches. You could modify the select thusly:
SELECT *
FROM tbl
WHERE my_col LIKE %val1%
OR my_col LIKE %val2%
OR my_col LIKE %val3%
...
If the val1, val2, val3... are similar enough, you might be able to use regular expressions in the REGEXP_LIKE operator.
Yes, you can use this query (Instead of 'Specialist' and 'Developer', type any strings you want separated by comma and change employees table with your table)
SELECT * FROM employees em
WHERE EXISTS (select 1 from table(sys.dbms_debug_vc2coll('Specialist', 'Developer')) mt
where em.job like ('%' || mt.column_value || '%'));
Why my query is better than the accepted answer: You don't need a CREATE TABLE permission to run it. This can be executed with just SELECT permissions.
In Oracle you can use regexp_like as follows:
select *
from table_name
where regexp_like (name, '^(value-1|value-2|value-3....)');
The caret (^) operator to indicate a beginning-of-line character &
The pipe (|) operator to indicate OR operation.
This one is pretty fast :
select * from listofvalue l
inner join tbl on tbl.mycol like '%' || l.value || '%'
Just to add on #Lukas Eder answer.
An improvement to avoid creating tables and inserting values
(we could use select from dual and unpivot to achieve the same result "on the fly"):
with all_likes as
(select * from
(select '%val1%' like_1, '%val2%' like_2, '%val3%' like_3, '%val4%' as like_4, '%val5%' as like_5 from dual)
unpivot (
united_columns for subquery_column in ("LIKE_1", "LIKE_2", "LIKE_3", "LIKE_4", "LIKE_5"))
)
select * from tbl
where exists (select 1 from all_likes where tbl.my_col like all_likes.united_columns)
I prefer this
WHERE CASE WHEN my_col LIKE '%val1%' THEN 1
WHEN my_col LIKE '%val2%' THEN 1
WHEN my_col LIKE '%val3%' THEN 1
ELSE 0
END = 1
I'm not saying it's optimal but it works and it's easily understood. Most of my queries are adhoc used once so performance is generally not an issue for me.
select * from tbl
where exists (select 1 from all_likes where all_likes.value = substr(tbl.my_col,0, length(tbl.my_col)))
You can put your values in ODCIVARCHAR2LIST and then join it as a regular table.
select tabl1.* FROM tabl1 LEFT JOIN
(select column_value txt from table(sys.ODCIVARCHAR2LIST
('%val1%','%val2%','%val3%')
)) Vals ON tabl1.column LIKE Vals.txt WHERE Vals.txt IS NOT NULL
You don't need a collection type as mentioned in https://stackoverflow.com/a/6074261/802058. Just use an subquery:
SELECT *
FROM tbl t
WHERE EXISTS (
SELECT 1
FROM (
SELECT 'val1%' AS val FROM dual
UNION ALL
SELECT 'val2%' AS val FROM dual
-- ...
-- or simply use an subquery here
)
WHERE t.my_col LIKE val
)

Select rows where column is null

How do you write a SELECT statement that only returns rows where the value for a certain column is null?
Do you mean something like:
SELECT COLUMN1, COLUMN2 FROM MY_TABLE WHERE COLUMN1 = 'Value' OR COLUMN1 IS NULL
?
I'm not sure if this answers your question, but using the IS NULL construct, you can test whether any given scalar expression is NULL:
SELECT * FROM customers WHERE first_name IS NULL
On MS SQL Server, the ISNULL() function returns the first argument if it's not NULL, otherwise it returns the second. You can effectively use this to make sure a query always yields a value instead of NULL, e.g.:
SELECT ISNULL(column1, 'No value found') FROM mytable WHERE column2 = 23
Other DBMSes have similar functionality available.
If you want to know whether a column can be null (i.e., is defined to be nullable), without querying for actual data, you should look into information_schema.
Use Is Null
select * from tblName where clmnName is null
You want to know if the column is null
select * from foo where bar is null
If you want to check for some value not equal to something and the column also contains null values you will not get the columns with null in it
does not work:
select * from foo where bar <> 'value'
does work:
select * from foo where bar <> 'value' or bar is null
in Oracle (don't know on other DBMS) some people use this
select * from foo where NVL(bar,'n/a') <> 'value'
if I read the answer from tdammers correctly then in MS SQL Server this is like that
select * from foo where ISNULL(bar,'n/a') <> 'value'
in my opinion it is a bit of a hack and the moment 'value' becomes a variable the statement tends to become buggy if the variable contains 'n/a'.
select Column from Table where Column is null;
select * from tableName where columnName is null
For some reasons IS NULL may not work with some column data type. I was in need to get all the employees that their English full name is missing, I've used:
SELECT emp_id, Full_Name_Ar, Full_Name_En
FROM employees
WHERE Full_Name_En = '' or Full_Name_En is null