'NOT LIKE' in an SQL query - sql

Why does this simple query return 'ORA-00936: missing expression' (the database is Oracle as you can tell):
SELECT * FROM transactions WHERE id NOT LIKE '1%' AND NOT LIKE '2%'
I feel silly, but what am I doing wrong?

You have missed out the field name id in the second NOT LIKE. Try:
SELECT * FROM transactions WHERE id NOT LIKE '1%' AND id NOT LIKE '2%'
The AND in the where clause joins 2 full condition expressions such as id NOT LIKE '1%' and can't be used to list multiple values that the id is 'not like'.

You need to specify the column in both expressions.
SELECT * FROM transactions WHERE id NOT LIKE '1%' AND id NOT LIKE '2%'

You've missed the id out before the NOT; it needs to be specified.
SELECT * FROM transactions WHERE id NOT LIKE '1%' AND id NOT LIKE '2%'

After "AND" and after "OR" the QUERY has forgotten what it is all about.
I would also not know that it is about in any SQL / programming language.
if(SOMETHING equals "X" or SOMETHING equals "Y")
COLUMN NOT LIKE "A%" AND COLUMN NOT LIKE "B%"

Related

Using oracle sql in like together

I`m trying to find results between two different tables. Query is like below
select * from atable
where acolumn like in (select bcolumn from btable)
Acolumn is like someid_123 and Bcolumn is like someid with these datas i cant use only in statement, i need to use something that can act like "like and in" statements
I googled it but couldn't find something like, could you help me?
Thank you
You can use exists:
select a.*
from atable a
where exists (select 1
from btable b
where a.acolumn like b.bcolumn
);
You can add wildcards to b.bcolumn if that is also needed, but your question doesn't suggest that is necessary.
you are looking for this:
select * from atable
join btable
on acolumn like '%'|| bcolumn || '%'

SQL search on part of column

Let's say I have multiple 6 character Alphanumeric strings. abc123, abc231, abc456, cba123, bac231, and bac123.
Basically I want a select statement that can search and list all the abc instances.
I just want a select statement that can list all instances with keyword "abc".
Use LIKE and wildcards %
SELECT *
FROM yourtable
WHERE yourfield LIKE '%abc%'
Input
yourfield
abc123
abc231
abc456
cba123
bac231
bac123
Output:
yourfield
abc123
abc231
abc456
SQL Fiddle:
Could you try like this way
SELECT *
FROM yourtable
WHERE field LIKE '%a%' Or field LIKE '%b%' Or field LIKE '%c%'
Select * from tablename
where yourfield like 'yourconstantcharacters%'
You can use LIKE operator in sql
Starting With Case
Select * From Table Where Column LIKE 'abc%';
Ending With Case
Select * From Tablo Where Column Like '%abc';
Contains With Case
Select * From Table Where Column LIKE '%abc%';
So If you want to use escape character in LIKE condition , you must make small changes on your condition
For Example :
SELECT * FROM Table WHERE column LIKE '!%' escape '!';
More information for you is here

Selecting only integer data within a column with characters

I am trying to write a query that will select items with only integer data such as: 019280498 as opposed to 0129830INK. These are sku's in a product database and my current solution is something to this effect:
Column name is SKU
SELECT *
FROM mydb
WHERE SKU not like '%a%' or SKU not like '%b%' or SKU not like '%c%' ..... or SKU not like '%z%'
This would only return values with no characters in them. However it does not like what I have written and i'm sure there is a more elegant way to execute this.
Using your original method with like, you can do:
SELECT *
FROM mydb
WHERE SKU not like '%[^0-9]%';
This has the advantage that expressions like '3e9' are not accepted as a numeric value.
Or, if is specifically alphabetic characters that you want to keep out:
SELECT *
FROM mydb
WHERE SKU not like '%[a-z]%'; -- or for case sensitive collations '%[a-zA-Z]%'
There's an IsNumeric() function that could be used.
Select *
from mydb
Where IsNumeric(sku) = 0x1
SELECT DISTINCT Pincode
FROM ps_all_india_pin_code
WHERE Pincode between '100000' and '999999'

MYSQL - SELECT query with LIKE using %% returns incorrect value

Query:
SELECT * FROM table
WHERE fieldA LIKE '%%'
AND fieldB LIKE '%%'
AND fieldC LIKE '%%'
This returns only records that have all fields completed. My thought would be that it should return all records in the table.
Does the '%%' actually represent that a value is needed?
UPDATE1:
Thanks to some good questions the solution was found:
Query should be like:
SELECT * FROM table
WHERE if(fieldA IS NOT NULL,fieldA LIKE '%%',fieldA IS NULL)
...
LIKE '%%' matches any string, even ones with zero length. The result of your query is it is returning all rows where the three fields each have a string in them.
My guess is that the fields that are not completed are NULL. Maybe you should be checking for IS NOT NULL instead of LIKE '%%'?

Is there any way to combine IN with LIKE in an SQL statement?

I am trying to find a way, if possible, to use IN and LIKE together. What I want to accomplish is putting a subquery that pulls up a list of data into an IN statement. The problem is the list of data contains wildcards. Is there any way to do this?
Just something I was curious on.
Example of data in the 2 tables
Parent table
ID Office_Code Employee_Name
1 GG234 Tom
2 GG654 Bill
3 PQ123 Chris
Second table
ID Code_Wildcard
1 GG%
2 PQ%
Clarifying note (via third-party)
Since I'm seeing several responses which don't seems to address what Ziltoid asks, I thought I try clarifying what I think he means.
In SQL, "WHERE col IN (1,2,3)" is roughly the equivalent of "WHERE col = 1 OR col = 2 OR col = 3".
He's looking for something which I'll pseudo-code as
WHERE col IN_LIKE ('A%', 'TH%E', '%C')
which would be roughly the equivalent of
WHERE col LIKE 'A%' OR col LIKE 'TH%E' OR col LIKE '%C'
The Regex answers seem to come closest; the rest seem way off the mark.
I'm not sure which database you're using, but with Oracle you could accomplish something equivalent by aliasing your subquery in the FROM clause rather than using it in an IN clause. Using your example:
select p.*
from
(select code_wildcard
from second
where id = 1) s
join parent p
on p.office_code like s.code_wildcard
In MySQL, use REGEXP:
WHERE field1 REGEXP('(value1)|(value2)|(value3)')
Same in Oracle:
WHERE REGEXP_LIKE(field1, '(value1)|(value2)|(value3)')
Do you mean somethign like:
select * FROM table where column IN (
SELECT column from table where column like '%%'
)
Really this should be written like:
SELECT * FROM table where column like '%%'
Using a sub select query is really beneficial when you have to pull records based on a set of logic that you won't want in the main query.
something like:
SELECT * FROM TableA WHERE TableA_IdColumn IN
(
SELECT TableA_IdColumn FROM TableB WHERE TableA_IDColumn like '%%'
)
update to question:
You can't combine an IN statement with a like statement:
You'll have to do three different like statements to search on the various wildcards.
You could use a LIKE statement to obtain a list of IDs and then use that in the IN statement.
But you can't directly combine IN and LIKE.
Perhaps something like this?
SELECT DISTINCT
my_column
FROM
My_Table T
INNER JOIN My_List_Of_Value V ON
T.my_column LIKE '%' + V.search_value + '%'
In this example I've used a table with the values for simplicity, but you could easily change that to a subquery. If you have a large list (like tens of thousands) then performance might be rough.
select *
from parent
where exists( select *
from second
where office_code like trim( code_wildcard ) );
Trim code_wildcard just in case it has trailing blanks.
You could do the Like part in a subquery perhaps?
Select * From TableA Where X in (Select A from TableB where B Like '%123%')
tsql has the contains statement for a full-text-search enabled table.
CONTAINS(Description, '"sea*" OR "bread*"')
If I'm reading the question correctly, we want all Parent rows that have an Office_code that matches any Code_Wildcard in the "Second" table.
In Oracle, at least, this query achieves that:
SELECT *
FROM parent, second
WHERE office_code LIKE code_wildcard;
Am I missing something?