sql LIKE statement combined from three "like"`s - sql

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%';

Related

SQL Server query on nvarchar with asterisk in data and in where clause

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'

using % in IN operator of sql

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 .

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.

Multiple searches in LIKE 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.

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'