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
Related
I have a string like that format:
'%asdasasd\ngalaxy\n4198461841\nasdadadqd\n11111118181gals%'
and i need to know is this string contains a word from column mName in table1:
In this case I need the output to be galaxy
I do not know what database do you use but this will work in Mysql and Oracle:
select *
from table1
where instr('%asdasasd\ngalaxy\n4198461841\nasdadadqd\n11111118181gals%', mname) <> 0;
I have created a table called test
You use instr function to find a value from a column mName in the string if it exists then you show it.
Here is a DEMO
Or as forpas suggested in his comment you can use like:
select * from table1
where '%asdasasd\ngalaxy\n4198461841\nasdadadqd\n11111118181gals%'
like '%'||mname||'%'
I have a table Product which looks like this:
Name (nvarchar(100), not null)
It has data that looks like this:
4503-U**F19
When I query like this:
select * from Product where Name = '4503-U**F19'
I get nothing back... what gives?
Is there some special way to escape a where clause that contains an asterisk?
I'm probably staring at the answer and can't see it.
Have you tried the N in the prefix?
select * from Product where Name = N'4503-U**F19'
I have a query like this
select * from table_employee where name in ('Jack','Jon','Jade');
and it gives me three results.
but I want to use % with the names I mentioned in query like:
select * from table_employee where name in ('%Jack%','%Jon%','%Jade%');
The query execution is completed but now I get no results. Whats wrong here and how can i use % in IN operator?
You should use LIKE combined with OR, IN does not support wildcards
select * from table_employee where name LIKE '%Jack%'
OR name LIKE '%Jon%';
The in keyword does not work like that. You can use a like operand and bind the three values with or
example
SELECT *
FROM table_employee
WHERE NAME LIKE '%Jack%'
OR NAME LIKE '%Jon%'
OR NAME LIKE '%Jade%';
Convert it to an OR:
SELECT *
FROM table_employee
WHERE NAME LIKE '%Jack%'
OR NAME LIKE '%Jon%'
OR NAME LIKE '%Jade%';
If your fields data are exact what you want get in the result is no needed to use like . You should use like only when you don't know the data in a field but you know that it could contains a string Jack for example .
I'm sayng this because if you have an index on the field where you use LIKE it will be not used , so you will get performance problem .
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')
I have one database table name test123 and having column name. And it contains the data like 'nir,kal,man' Now, when i am querying the table with select statement as per below :
select * from test123 where name = 'nir,kal,man';
But this will not return any result...Why this happened.? How i have to write query so that will return the result?
I am using Sql server 2008.
Thanks...!
= operator returns exact match, so if your cell contain data "like" that you need to use LIKE operator:
select * from test123 where name like '%nir,kal,man%'
where % will be replaced with any set of characters.
Also check that you're targeting correct database by using full name
select * from yourdb.dbo.test123 where....
if Nir is in first row Kal in 2nd row and man is in 3rd row then you should write query like this
select * from test123 where name in ('nir','kal','man')