Matching a String having '%' as a character - sql

I want to match a String using Like operator. The challenge is having '%' as a character in my string.
i.e. Row1 : Column = CT%CNV!XYZABCD...
Row2 : Column = CTXXXCNV!XYZABCDE...
If I use "SELECT * FROM table WHERE Column like 'CT%CNV!%'. It doesn't consider '%' as a character and the statement returns both rows.
I need to return the first row only.

You shoud use escape keyword:
select *
from MyTable
where Column like 'CT\%CNV!XYZABCD%' escape '\'
here '\%' is treated as a plain symbol, while '%' is wild card one

You can use brackets to escape the percent sign :
SELECT * FROM table WHERE Column like 'CT[%]CNV!%'

Related

Athena/Presto Escape Underscore

I'm trying to escape an underscore in a like operator but not getting any results. I'm trying to find any rows with a value like 'aa_'.
WHERE value LIKE '%aa\\_%'
Use ESCAPE:
Wildcard characters can be escaped using the single character specified for the ESCAPE parameter.
WITH dataset (str) AS (
VALUES ('aa_1'),
('aa_2'),
('aa1')
)
SELECT *
FROM dataset
WHERE str like 'aa\_%' ESCAPE '\'
Output:
str
aa_1
aa_2

How to get ℅ from a column in a table

I have a table called students with a column marks, the data in the marks column is like 80,90,70%,50%,30.
How do I get the data from marks column which is like 70%,50%.
Assuming data type of marks is varchar.
select marks from students where marks like'%\%' escape '\';
You should escape % sing within WHERE clause.
Try following
SELECT *
FROM students
WHERE marks LIKE '70[%]%'
OR marks LIKE '50[%]%'
You can use LIKE operator,
SELECT * FROM students WHERE marks LIKE '\%%'
SELECT * FROM students(
WHERE marks LIKE '%\%')
/
In above code first % will be treated as wildcard which means it will try to find any character.
Next % is actual string '%' (since it has escape character '/').
So query will return any character followed by '%'

Using wildcards

Can anyone help me out with this-
There is a column in the database as "TEXT".
This column hold some string value.
I want to search any row that is having '%' in this column .
For eg how will i search for a row having value 'Vivek%123' in the column TEXT
In sql there is something known as an escape character, basically if you use this character it will be ignored and the character right behind it will be used as a literal instead of a wildcard in the case of %
WHERE Text LIKE '%!%%'
ESCAPE '!'
The above sql statement will allow you to search for any string containing a percentage character '%' so it could find anything in the format of
string%string
You must escape the % character
WHERE COL1 LIKE 'Vivek#%123' ESCAPE '#' ;

in ms azure database how do you search for a string containing _

I have this situation on my azure database where I need to search for any rows that contains the _ character. This is a special character on the database so I try to escape it but I get every row as a result.
select * from table where fieldColumn like '%_%'
will return everything on the table
select * from table where fieldColumn like '%\_%'
returns nothing
select * from table where fieldColumn = '_'
works
so how can i get that row that has only one _ and all the other ones that may have the _ on the string?
You can set whatever escape character you want, like this:
select * from table where fieldColumn like '%!_%' ESCAPE '!'
Here I am using the ! as an escape character to tell SQL Server to treat the following character, the _ , as a string literal.
See the documentation for more info: http://technet.microsoft.com/en-us/library/ms179859.aspx
select * from table where fieldColumn like '%_%' escape '\';
LIKE:
escape_character
Is a character that is put in front of a wildcard character to indicate that the wildcard should be interpreted as a regular character and not as a wildcard. escape_character is a character expression that has no default and must evaluate to only one character.

SQL 'LIKE' query using '%' where the search criteria contains '%'

I have an SQL query as below.
Select * from table
where name like '%' + search_criteria + '%'
If search_criteria = 'abc', it will return data containing xxxabcxxxx which is fine.
But if my search_criteria = 'abc%', it will still return data containing xxxabcxxx, which should not be the case.
How do I handle this situation?
If you want a % symbol in search_criteria to be treated as a literal character rather than as a wildcard, escape it to [%]
... where name like '%' + replace(search_criteria, '%', '[%]') + '%'
Use an escape clause:
select *
from (select '123abc456' AS result from dual
union all
select '123abc%456' AS result from dual
)
WHERE result LIKE '%abc\%%' escape '\'
Result
123abc%456
You can set your escape character to whatever you want. In this case, the default '\'. The escaped '\%' becomes a literal, the second '%' is not escaped, so again wild card.
See List of special characters for SQL LIKE clause
The easiest solution is to dispense with "like" altogether:
Select *
from table
where charindex(search_criteria, name) > 0
I prefer charindex over like. Historically, it had better performance, but I'm not sure if it makes much of difference now.
To escape a character in sql you can use !:
EXAMPLE - USING ESCAPE CHARACTERS
It is important to understand how to "Escape Characters" when pattern matching. These examples deal specifically with escaping characters in Oracle.
Let's say you wanted to search for a % or a _ character in the SQL LIKE condition. You can do this using an Escape character.
Please note that you can only define an escape character as a single character (length of 1).
For example:
SELECT *
FROM suppliers
WHERE supplier_name LIKE '!%' escape '!';
This SQL LIKE condition example identifies the ! character as an escape character. This statement will return all suppliers whose name is %.
Here is another more complicated example using escape characters in the SQL LIKE condition.
SELECT *
FROM suppliers
WHERE supplier_name LIKE 'H%!%' escape '!';
This SQL LIKE condition example returns all suppliers whose name starts with H and ends in %. For example, it would return a value such as 'Hello%'.
You can also use the escape character with the _ character in the SQL LIKE condition.
For example:
SELECT *
FROM suppliers
WHERE supplier_name LIKE 'H%!_' escape '!';
This SQL LIKE condition example returns all suppliers whose name starts with H and ends in _ . For example, it would return a value such as 'Hello_'.
Reference: sql/like
Select * from table where name like search_criteria
if you are expecting the user to add their own wildcards...
You need to escape it: on many databases this is done by preceding it with backslash, \%.
So abc becomes abc\%.
Your programming language will have a database-specific function to do this for you. For example, PHP has mysql_escape_string() for the MySQL database.
Escape the percent sign \% to make it part of your comparison value.
May be this one help :)
DECLARE #SearchCriteria VARCHAR(25)
SET #SearchCriteria = 'employee'
IF CHARINDEX('%', #SearchCriteria) = 0
BEGIN
SET #SearchCriteria = '%' + #SearchCriteria + '%'
END
SELECT *
FROM Employee
WHERE Name LIKE #SearchCriteria