Is there any shorter way to look for multiple matches:
SELECT *
from table
WHERE column LIKE "AAA%"
OR column LIKE "BBB%"
OR column LIKE "CCC%"
This questions applies to PostgreSQL 9.1, but if there is a generic solution it would be even better.
Perhaps using SIMILAR TO would work ?
SELECT * from table WHERE column SIMILAR TO '(AAA|BBB|CCC)%';
Use LIKE ANY(ARRAY['AAA%', 'BBB%', 'CCC%']) as per this cool trick #maniek showed earlier today.
Using array or set comparisons:
create table t (str text);
insert into t values ('AAA'), ('BBB'), ('DDD999YYY'), ('DDD099YYY');
select str from t
where str like any ('{"AAA%", "BBB%", "CCC%"}');
select str from t
where str like any (values('AAA%'), ('BBB%'), ('CCC%'));
It is also possible to do an AND which would not be easy with a regex if it were to match any order:
select str from t
where str like all ('{"%999%", "DDD%"}');
select str from t
where str like all (values('%999%'), ('DDD%'));
You can use regular expression operator (~), separated by (|) as described in Pattern Matching
select column_a from table where column_a ~* 'aaa|bbb|ccc'
Following query helped me. Instead of using LIKE, you can use ~*.
select id, name from hosts where name ~* 'julia|lena|jack';
You might be able to use IN, if you don't actually need wildcards.
SELECT *
from table
WHERE column IN ('AAA', 'BBB', 'CCC')
Related
I am using the below query to get Ids like 12766% and 39998%. How I can use regexp_like to check 12%66 and 39%98?
select * from table t where regexp_like( UPPER(ID),'12766|39998')
You may use the following regex pattern:
^(12[0-9]*66|39[0-9]*98)$
Oracle query:
SELECT *
FROM yourTable
WHERE REGEXP_LIKE(ID, '^(12[0-9]*66|39[0-9]*98)$');
Demo
But actually, you might just want to use LIKE here:
SELECT *
FROM yourTable
WHERE ID LIKE '12%66' OR ID LIKE '39%98';
This would work find, so long as you don't mind exactly what comes in between the digits.
I found solution for this. We can use the below query for a%d to match strings like abcd,acd,aabcd etc. A period character (.) is a perfect replacement for % in regexp which can support one or more occurrence of any characters supported in database.
select * from table where REGEXP_LIKE (UPPER (ID), '^12.66.|EY39.98.')
Following is the query to select column data from table, where column data starts with a OR b OR c. But the answer i am looking for is to Select data which starts with List of Strings.
SELECT * FROM Table WHERE Name LIKE '[abc]%'
But i want something like
SELECT * FROM Table WHERE Name LIKE '[ab,ac,ad,ae]%'
Can anybody suggest what is the best way of selecting column data which starts with list of String, I don't want to use OR operator, List of strings specifically.
The most general solution you would have to use is this:
SELECT *
FROM Table
WHERE Name LIKE 'ab%' OR Name LIKE 'ac%' OR Name LIKE 'ad%' OR Name LIKE 'ae%';
However, certain databases offer some regex support which you might be able to use. For example, in SQL Server you could write:
SELECT *
FROM Table
WHERE NAME LIKE 'a[bcde]%';
MySQL has a REGEXP operator which supports regex LIKE operations, and you could write:
SELECT *
FROM Table
WHERE NAME REGEXP '^a[bcde]';
Oracle and Postgres also have regex like support.
To add to Tim's answer, another approach could be to join your table with a sub-query of those values:
SELECT *
FROM mytable t
JOIN (SELECT 'ab' AS value
UNION ALL
SELECT 'ac'
UNION ALL
SELECT 'ad'
UNION ALL
SELECT 'ae') v ON t.vame LIKE v.value || '%'
I have a string "abc", and I want to search, in a SQL table, for the values that contain either a, b or c.
This is what I have right now:
Select * from TABLE where NAME like "abc" ;
This didn't work, but I know if I try something like
where Name like "a" or Name like "b" or ....
it will work.
Is there an easier way to do this?
Since I don't want to separate my string into characters.
You can use regular expression for this.
Have a look at the following :
Select * from TABLE where NAME REGEXP "[abc]";
select * from ab where name REGEXP '[abc]'
This will do
Select * from TABLE where NAME like "%abc%" ;
For more check information here http://www.techonthenet.com/sql/like.php
Is there anyway to use wildcards in a clause similar to a "in", like this
select * from table where columnx xxxxxxx ('%a%','%b%')?
I know I can do:
select * from table where (columnx like '%a%' or columnx like '%b%')
But I'm looking for an alternative to make the querystring shorter.
BTW: I'm not able to register any custom functions, nor temp tables, it should be a native DB2 function.
I found this similar answer for oracle and SQLServer:
Is there a combination of "LIKE" and "IN" in SQL?
There's no native regular expression support in "pureSQL" for DB2, you can either create your own as in:
http://www.ibm.com/developerworks/data/library/techarticle/0301stolze/0301stolze.html
or use pureXML as in: http://publib.boulder.ibm.com/infocenter/db2luw/v9r7/topic/com.ibm.db2.luw.xml.doc/doc/xqrfnmat.html
Example:
where xmlcast(xmlquery('fn:matches(\$TEXT,''^[A-Za-z 0-9]*$'')') as integer) = 0
Yet another variant that may be shorter:
select t.*
from table t
join ( values '%a%', '%b%' ) u (columnx)
on t.columnx like u.columnx
I'm looking for a character that I can use in an Oracle contains to get ALL results.
If I search for the string "test" in a title column I use this statement:
select *
from my_table
where contains (title,
'<query><textquery grammar="CTXCAT">test</textquery></query>') > 0
With this statement I get the rows which have the "test"-string included in title-column.
BUT: Is there any way to use contains and select ALL rows?
Would this work for you?
select * from my_table
where title like
'<query><textquery grammar="CTXCAT">%</textquery></query>'
This uses the LIKE syntax and the % wildcard to achieve what I think you want.
The documentation says wildcards with the Oracle Text CONTAINS() predicate are % and _ just like the LIKE predicate.
Does the following work? I don't have an instance of Oracle handy to test.
select *
from my_table
where contains (title,
'<query><textquery grammar="CTXCAT">%</textquery></query>') > 0