Is it possible to combine SQL statements such as
SELECT * FROM X WHERE Y LIKE '%a%' OR Y LIKE '%b%' OR Y LIKE '%c%' OR Y LIKE '%d%'
with something like
SELECT * FROM X WHERE Y IN ('a', 'b', 'c', 'd')
so I don't have to write one big statement such as it is now:
IF NOT EXISTS(SELECT * FROM X WHERE Y LIKE '%a%' OR Y LIKE '%b%' OR Y LIKE '%c%' OR Y LIKE '%d%')
BEGIN
/* code */
END
Would be very nice to use something like SELECT * FROM X WHERE Y IN LIKE ('%a%', '%b%', etc..)
Appreciate all help and suggestions, thanks.
Please use the below code for your problem ,try and let me know if still there is issue.
SELECT * FROM X WHERE Y LIKE '%[a-d]%'
If your database supports RegEx, you can use that. For example:
SELECT * FROM X WHERE Y SIMILAR TO '%(a|b|c|d)%' in PostgreSQL, and
SELECT * FROM X WHERE Y REGEXP 'a|b|c|d' in MySQL.
LIKE '%[a-d]%' won't work because LIKEs in most databases don't accept regex patterns. This will just expect the literal text [a-d] somewhere in the string. How each database interprets regular expression (or native pattern) varies a lot. So you need to tailor these things to your database. For example, the following will not work in PostgreSQL:
SELECT * FROM X WHERE Y SIMILAR TO 'a|b|c|d'
because PostgreSQL will expect the whole string to be 'a', 'b', 'c', or 'd'.
Related
I have a table filled with company code prefixes (cp-partnum,ag-partnum,ff-partnum). I would like to select all codes which begin with 'cp-partnum' where partnum does not begin with 'I' or 'i'. So 'cp-aaa' is acceptable but 'cp-iaa' is not. How can this be achieved?
edit: to be clear, I would like ONLY the codes that begin with 'cp-[letter where letter is not "I" or "i"]'
You can use SIMILAR TO and a (SQL) regular expression:
yourcolumn SIMILAR TO 'cp-[^iI]%'
If company are always 2 (alpha) characters, you can do something like:
yourcolumn SIMILAR TO '[:ALPHA:]{2}-[^iI]%'
If company code can be any two characters, you can use
yourcolumn SIMILAR TO '__-[^iI]%'
For more complex patterns, study the documentation. SIMILAR TO is available starting with Firebird 2.5.
Use below query:
select *
from companies
where company_code like '__-%'
and company_code not like '__-I%'
and company_code not like '__-i%'
You can use SUBSTRING and NOT IN with only 1 LIKE :
WHERE YourColumn LIKE 'cp-%'
AND SUBSTRING(YourColumn from 4 for 1) NOT IN('i','l')
You can do this:
where col like 'cp-%' and
col not like 'cp-l%' and
col not like 'cp-i%'
For multiple prefixes:
where left(col, 2) in ('cp', 'ag', 'ff') and
substring(col, 4, 1) not in ('l', 'i') and
col like '__-%'
I guess I did not need to support lower case letters so the following worked.
SELECT * FROM product
WHERE product.num LIKE 'CP-%'
AND product.num NOT IN (SELECT product.num FROM product
WHERE product.num LIKE 'CP-I%')
I was wondering if is there a way to do a query like this one (where Z is a variable)
SELECT * FROM table t WHERE t.X = Z OR t.Y = Z
writing only one time the Z. To be more specific I would like to do the same query like so
SELECT * FROM table t WHERE (t.X OR t.Y) = Z
I am using an Oracle DB and it (obviously) gives me an error when I try to execute it BUT I really like a way to do it like in the second query.
To make you know my situation X and Y are both VARCHAR2 and I already try something like
SELECT * FROM table t WHERE (t.X || t.Y) like '%Z%'
But, unluckily, it is not as accurate as the first one.
You can use in:
WHERE Z IN (t.X, t.Y)
Columns can be in the IN list as well as constants.
Try Like This
SELECT * FROM table t WHERE (t.X like %Z% OR t.Y like %Z% )
can we combine the AND operator and NOT IN in mysql? for example:
WHERE x AND y NOT IN ( SELECT X,Y
FROM ....
);
Is the syntax correct?
Whether or not this is possible depends on the database server you use. Some databases (eg PostgreSQL) support row values, which allow you to do this:
where (x, y) in (select colX, colY from ....)
Otherwise you can do something like
where exists (select 1 from ... where colX = x and colY = y)
yes but you should add "AND" operator to your query:
WHERE x AND y AND NOT IN ( SELECT X,Y FROM ....
How do I do a like search on a number column in SQL?
I want numbers which are like '0.0000%'.
I tried with
select * from emp where emp_id & '' like '123%'
select * from emp where CONVERT(varchar(20), emp_id) like '123%'
but in vain.
Please help me
Regardless of which DBMS you are using AND assuming you have a valid reason to do this, you have several ways to solve problems like these. I can think of three right now:
Convert the number to a string and use a LIKE operator on this:
select *
from emp
where to_char(emp_id) like '123%';
Use mathematical operators directly (like Andrey suggests), for example:
select *
from table
where num between 0 and 0.0001;
Construct a mathematical expression (actually, this is just another case of method 2), for example:
select *
from table
where abs(num - round(num, 5)) < 0.00001;
Use comparison operators (> and <):
select * from table where num > 0 and num < 0.00001
select 0.0001*1000 from dual if it is <1 means the 0.0001 has 3 or more zeros.
so i did like
select * from emp where emp_id*10000<1
I want to compare and select a field from DB using Like keyword or any other technique.
My query is the following:
SELECT * FROM Test WHERE name LIKE '%xxxxxx_Ramakrishnan_zzzzz%';
but my fields only contain 'Ramakrishnan'
My Input string contain some extra character xxxxxx_Ramakrishnan_zzzzz
I want the SQL query for this. Can any one please help me?
You mean you want it the other way round? Like this?
Select * from Test where 'xxxxxx_Ramakrishnan_zzzzz' LIKE '%' + name + '%';
You can use the MySQL functions, LOCATE() precisely like,
SELECT * FROM WHERE LOCATE("Ramakrishnan",input) > 0
Are the xxxxxx and zzzzz bits always 6 and 5 characters? If so, then this is doable with a bit of string cutting.
with Test (id,name) as (
select 1, 'Ramakrishnan'
union
select 2, 'Coxy'
union
select 3, 'xxxxxx_Ramakrishnan_zzzzz'
)
Select * from Test where name like '%'+SUBSTRING('xxxxxx_Ramakrishnan_zzzzz', 8, CHARINDEX('_',SUBSTRING('xxxxxx_Ramakrishnan_zzzzz',8,100))-1)+'%'
Results in:
id name
1 Ramakrishnan
3 xxxxxx_Ramakrishnan_zzzzz
If they are variable lengths, then it will be a horrible construction of SUBSTRING,CHARINDEX, REVERSE and LEN functions.