HSQL execute condition LIKE ANY - sql

I'm trying to execute an SQL statement, with a where clause that does something like:
WHERE col LIKE ANY (values...)
According to the HSQLDB documentation it seems to me that I should be able to do it:
condition
{ ...
| value [NOT] LIKE value [ESCAPE] value }
value
[+ | -] { term [{ + | - | * | / | || } term]
| ( condition )
| function ( [parameter] [,...] )
| selectStatement giving one value
| {ANY|ALL} (selectStatement giving single column)
However, this does not seem to work.
I can execute this:
SELECT * FROM table WHERE col LIKE (selectStatement giving 1 column with single value)
But any of these will give me an error:
SELECT * FROM table WHERE col LIKE (selectStatement giving 1 column with multiple values)
-> cardinality violation
SELECT * FROM table WHERE col LIKE ANY (selectStatement giving 1 column with single value)
SELECT * FROM table WHERE col LIKE ANY (selectStatement giving 1 column with multiple values)
-> unexpected token: SELECT
Can you help me understand what I'm doing wrong?
Is this not supported, or am I misunderstanding the documentation?
Thanks!

Depending on what you want to achieve, you can use the following:
This query uses the value returned by the subquery for LIKE comparison. The value normally has a escape character, for example super%, which matches all words beginning with super:
SELECT * FROM table WHERE col LIKE (selectStatement giving 1 column with single value)
This query uses the values returned by the subquery as a list. Each of the list values is compared to the col value and when the first exact match is found, the result is true. This type of query is used to return a subset of the rows from the table that have a value that is in the list returned by the subquery:
SELECT * FROM table WHERE col = ANY (selectStatement giving 1 column with multiple values)

I am late to the game here, but would like to add the idea of a colleague of mine to use EXISTS() to allow multiple LIKE/ILIKE conditions.
So that query simulates the "x LIKE ANY('{}')"
SELECT
tables.table_name
FROM
information_schema.tables
WHERE
EXISTS(SELECT *
FROM
UNNEST (ARRAY['a%', 'b%', 'c%']) AS t(like_value)
WHERE
LOWER(tables.table_name) LIKE LOWER(like_value))
ORDER BY
tables.table_name
is equivalent to that in Postgresql
SELECT
tables.table_name
FROM
information_schema.tables
WHERE
tables.table_name ILIKE ANY ('{a%, b%, c%}')
ORDER BY
tables.table_name
Additionally, if you would like to use that from Java using prepared statements and to prevent Hsqldb from throwing a "General error" you have to cast the placeholder like so:
SELECT
tables.table_name
FROM
information_schema.tables
WHERE
EXISTS(SELECT *
FROM
UNNEST (CAST(? as VARCHAR(2048) ARRAY)) AS t(like_value)
WHERE
LOWER(tables.table_name) LIKE LOWER(like_value))
ORDER BY
tables.table_name

Related

How to search a row to see if any of the columns contain a certain string? In SQL

I have a table with 10 columns that each contain string values. I want to run a query that will return any of the rows which have any column value that matches a given string or set of strings.
Is there any way to do this?
The DBMS is MsSQL
if you want exact match, you can use IN keyword and check in all columns
SELECT *
FROM tablename
WHERE 'your string' IN (column1, column2, )
if you want partial match then you have to use LIKE
SELECT *
FROM tablename
WHERE column1 LIKE '%your string%' or column2 LIKE '%your string%' ...
or you can add all columns and do one LIKE check
SELECT *
FROM tablename
WHERE CONCAT(column1,'#',column2,'#',column3,'#',...) LIKE '%your string%'

Filter based on column value match in SQL

I want to write query that matches the value in a column with the value in another column.
example Column 1 value : Newport
Column 2 Value: Medical Council Newport
I want to use a filter that select the rows where the entire value in column 1 matches with the column 2 partially.
i cant to use the filter condition based on string match
select * from tb1
where column2 like '%Newport%' -- this doesnt work for me
Try something like
SELECT *
FROM TABLE_Name
WHERE Col2 LIKE '%'+ Col1 + '%'

Pattern matching on strings from a table in SQL

Just wanted to know if it was possible to do a pattern matching on a set of data from a table.
Like:
select * from Table where Column like any(select Pattern from PatternTable)
Note that the Pattern is always a substring of Column. Hence the use of like. Is it even possible to do this at a database level without the use of stored procedures?
If it helps, my RDBMS is MS SQL-Server
Edit:
Alright, I have a table containing a set of data like
PatternTable
____________
test1
test2
test3
test4
Now, a table Table has the following data:
Table
______
SomeDatatest4SomeData
SomeDataSomeData
Now, can I use a query as mentioned above to find a match: For the above query, this should return SomeDatatest4SomeData
You can do this using exists:
select *
from Table t
where exists (select 1
from PatternTable pt
where t.Column like pt.Pattern
);
SELECT t.*
FROM [Table] t
INNER JOIN PatternTable p ON t.[Column] LIKE '%' + p.Pattern + '%'

SQL query Where clause problem

I am having trouble finding a query.
I have a table 'NotificationExcludes' that contains 1 column 'Name'.
I have a table 'NotificationEvents' that contains columns: Id, Description.
Now I need to select all the NotificationEvents where the description doesn't start with the values contained in 'NotificationExcludes'.
Small example:
NotificationExcludes contains:
Name
The instance was terminated by
NotificationEvents:
ID Description
1 There was a failure on ..
2 The instance was terminated by administrator
Now I need to select everything except if the description starts with a value that is kept in the 'NotificationExcludes'.
I have tried
Select Id, Description
from NotificationEvent
WHERE Description NOT IN (select Name form NotificationExcludes)
But 2 problems with that:
1 The query obviously fails because 'select Name form NotificationExcludes' returns more than 1 record
2 The statement should contain a Like statement where I can use the '%' key. So where description not like 'The instance was terminated by%' can be used somehow
SELECT Id,Description
FROM NotificationEvents ne
WHERE NOT EXISTS(SELECT *
FROM NotificationExcludes nx
WHERE ne.Description
LIKE nx.name + '%') /*<--Might need Concat or || here
dependant upon RDBMS*/
One solution is to JOIN with a LIKE clause on name, appended with %.
SELECT *
FROM NotificationEvents ne
LEFT OUTER JOIN NotificationExcludes nex
ON ne.Description LIKE nex.Name + '%'
WHERE nex.Name IS NULL

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