Multiple searches in LIKE SQL - sql

Hey guys I'm currently trying to search a table for any location names that start with B or G, I am running into an error when trying to run this code:
SELECT *
FROM Location
WHERE LocName LIKE 'B%' or 'G%'
I am a massive SQL noob, infact this is my first ever SQL experience, any help would be appreciated!

Try below Query. If you want to search records with two conditions, you have write something like this
SELECT * FROM Location
WHERE LocName LIKE 'B%'
or LocName LIKE 'G%'

Try like this,
SELECT *
FROM Location
WHERE LocName LIKE 'B%' OR LocName LIKE 'G%'

try like this:
SELECT *
FROM Location
WHERE LocName LIKE 'B%' or LocName LIKE 'G%'

You can try this:
SELECT * FROM Location WHERE LocName LIKE 'B%'
UNION
SELECT * FROM Location WHERE LocName LIKE 'G%'

Here is one with REGEXP_LIKE operator
SELECT *
FROM location
WHERE REGEXP_LIKE (LocName, '^(B|G)');

You can add as much condition as you want by appending Or and writing your condition at the end. Try the following.
SELECT * FROM Location
WHERE LocName LIKE 'B%'
or LocName LIKE 'G%'
The Live SQL SERVER working fiddle here.
The Live ORACLE working fiddle here.

Related

I can't find the number data with SQL Server like

The data is registered in the database with the number 5056381825. But when I like this number with 905056381825, a blank result is returned. What's the solution?
The LIKE seems to be in the wrong direction. Try:
where '905056381825' LIKE CONCAT('%', number, '%')
It is not clear from your question what the name of the column is in the database.
I assume that the correct query is:
SELECT *
FROM prCurrAccCommunication
WHERE CommAddress LIKE CONCAT('%', number, '%')

How to select 3 specific pattern in column using SQL? What is wrong with my code?

SELECT * FROM Customers
WHERE CustomerName LIKE ('%aa%','%bb%','%cc%');
Try splitting your conditions like this
WHERE CustomerName LIKE '%aa%' OR CustomerName LIKE '%bb%' OR CustomerName LIKE '%cc%'
Try
SELECT * FROM Customers
WHERE CustomerName LIKE '%aa%' OR CustomerName LIKE '%bb%' OR CustomerName LIKE '%cc%';
LIKE does not work the same as IN where you can list a set of possibilities to match against. You have to use an OR between LIKE statements to match multiple patterns.
SELECT * FROM Customers WHERE CustomerName LIKE '%aa%' OR CustomerName LIKE '%bb%' OR CustomerName LIKE '%cc%'
You can also use the following code
SELECT * FROM Customers
WHERE
instr(CustomerName,'aa')>0 or instr(CustomerName,'bb')>0 or instr(CustomerName,'cc')>0
However this is working in oracle 11G, Please check the compatibility as well

How to cast column within select query

I have this query:
SELECT * FROM students
WHERE name LIKE '%ka%' OR
tel LIKE '%12%' OR
address LIKE '%ka%';
but tel is an integer-type column and therefore I can't use the %abc% operator there (although I need to do it like that)
How can I cast the tel column within this query?
Try this
SELECT * FROM students
WHERE name LIKE '%ka%' OR
CAST(tel as text) LIKE '%12%' OR
address LIKE '%ka%';
Try this:
SELECT * FROM students
WHERE name LIKE '%ka%' OR
CONVERT(VARCHAR, tel) LIKE '%12%' OR
address LIKE '%ka%';
Convert the tel to a text type first
cast(tel as text) LIKE '%12%' OR
You should consider changing your column type, if you are not using an int type like an int
Indexes and other db optimizations are ineffective with a query like this.

sql LIKE statement combined from three "like"`s

how to combine sql query from three like statements? I`m trying like this:
SELECT * FROM myTable
WHERE
(NAME LIKE '%someChars%' AND
(CITY LIKE '%someChars%' AND
TYPE LIKE'% someChars%')
);
It doesn`t work, can you help me with that please ?
The query is correct except the fact that Type is a sql keyword, so try putting it between bracket like that:
SELECT * FROM [myTable]
WHERE ([NAME] LIKE '%someChars%' AND
([CITY] LIKE '%someChars%' AND
[TYPE] LIKE '%someChars%'));
PS: The parenthesis are not needed
SELECT *
FROM myTable
WHERE
NAME LIKE '%someChars%' AND
CITY LIKE '%someChars%' AND
TYPE LIKE '%someChars%';

'NOT LIKE' in an SQL query

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%"