Matching literal percent in h2/postgres? - sql

I have the following value in a db column:
some%thing
I'm using h2 and postgres, but can't figure out why this query, where I have escaped the % to perform a literal search, is not matching the row highlighted?:
LIKE 'some\%thing'

Because 'value' is a character string literal and not the name of your column. You need to use double quotes instead.
"value" LIKE 'some\%hing'

One option is to use regular expressions:
where value ~ '%'
LIKE also supports an explicit escape character:
where value like '%$%%' escape '$'
Or the default one, \.

Related

Semicolon in LIKE is not working for SQL server 2017

I have a query like this which is not retrieving the values from DB table even if the required value exist there.
Here's the query, which return zero rows:
Select * from SitePanel_FieldValue WHere SiteFieldIdfk =111
And SiteFieldvalue like '%!##$%&*()-_=+{}|:"<>?[]\;'',./%'
Following is the value in the table:
'!##$%&*()-_=+{}|:"<>?[]\;'',./'
When I run the query without ";" it is returning the value.
Can any one help me in figuring this out?
Thanks
Ritu
You are using multiple characters which are reserved when using LIKE statement.
i.e. %, _, []
Use the escape character clause (where I have used backtick to treat special characters as regular) such as
Select * from SitePanel_FieldValue WHere SiteFieldIdfk =111
And SiteFieldvalue like '%!##$`%&*()-`_=+{}|:"<>?`[`]\;'',./%' escape '`'
The value in your table is:
!##$%&*()-_=+{};; :"<>?[]\;'',./
And the one in the like is:
(!##$%&*()-_=+{};;
Starting with ( it will never match, also you should scape the percent (%) in the middle of the string like this:
Select *
FROM SitePanel_FieldValue
WHERE SiteFieldIdfk =111
AND SiteFieldvalue like '%!##$\%&*()-_=+{};;%' ESCAPE '\'
The problem is your brackets ([]), it has nothing to do with semicolons. If we remove the brackets, the above works:
SELECT CASE WHEN '!##$%&*()-_=+{}|:"<>?\;'',./' LIKE '%!##$%&*()-_=+{}|:"<>?\;'',./%' THEN 1 END AS WithoutBrackets,
CASE WHEN '!##$%&*()-_=+{}|:"<>?[]\;'',./' LIKE '%!##$%&*()-_=+{}|:"<>?[]\;'',./%' THEN 1 END AS WithBrackets
Notice that WithoutBrackets returns 1, where as WithBrackets returns NULL.
Brackets in a LIKE are to denote a pattern. For example SomeExpress LIKE '[ABC]' would match the characters, A, B, and C. If you are going to include special characters, you need to ESCAPE them. You have both brackets, a percent sign (%) and an underscore (_) you need to escape. You don't need to escape the hyphen (-), as it doesn't appear in a pattern (for example [A-Z]). I choose to use a backtick as the ESCAPE character, as it doesn't appear in your string, and demonstrate with a CASE expression again:
SELECT CASE WHEN '!##$%&*()-_=+{}|:"<>?[]\;'',./' LIKE '%!##$`%&*()-`_=+{}|:"<>?`[`]\;'',./%' ESCAPE '`' THEN 1 END;
If you wanted to use a backslash (\ ), which many do, you would need to also escape the backslash in your string:
SELECT CASE WHEN '!##$\%&*()-_=+{}|:"<>?[]\;'',./' LIKE '%!##$%&*()-\_=+{}|:"<>?\[\]\\;'',./%' ESCAPE '\' THEN 1 END;
db<>fiddle
I think the issue is actually with the backslash. This is an escape character and so if you want it to be included, you have to put it in twice.
Select * from SitePanel_FieldValue WHere SiteFieldIdfk =111
And SiteFieldvalue like '%!##$%&*()-_=+{}|:"<>?[]\\;'',./%'

How to escape single quotes in RLIKE Hive? Double single quotes not working

I would like to escape the single quote in RLIKE input. I used double single quotes like so:
SELECT * FROM TABLE
WHERE column RLIKE 'o''brien'
But it returned the results with "obrien" rather than "o'brien". I tried "\\'" instead of double single quotes too, but that doesn't work either. So what is the correct escape character for single quote?
Three methods:
1 Put the whole regexp into double-quotes, single quote is shielded inside double-quotes:
where column rlike "o'brien"
See also: https://stackoverflow.com/a/66175603/2700344
2 Use unicode \u0027
where column rlike 'o\\u0027brien'
3 Use HEX \x27
where column rlike 'o\\x27brien'
Using \\x or \\u codes you can check any special character if you know it's code.
You can just use =:
WHERE column = 'o''brien'
I'm not sure why you are using RLIKE unless you intent:
WHERE column LIKE '%o''brien%'

How to escape '|' in SQL Server or T-SQL Query?

SELECT *
FROM performance_table
WHERE ad_group like '%|%'
I have no idea on how to escape the Pipe operator here.
You don't need to escape | in T-SQL as it has no special meaning inside like. However, if for example you would like to find texts containing % character, what you're looking for is:
SELECT *
FROM performance_table
WHERE ad_group like '%#%%' escape '#'
where escape defines escape character.
The pipe character does not need to be escaped.
Your query will find all records that contain a pipe character in the ad_group column.
When used inside a string literal ('|'), the character is not treated as an operator. Its function as an operator is bitwise OR, as for example in
select 8|3
will be 11.

How to handle/use special characters like percent (%) and ampersand (&) in Oracle SQL queries

I need to include the special character "%" in my LIKE clause in a SQL query, e.g.:
Select * From Some_Table Where Field_Name Like 'bla%bla&2';
How do I write that?
If you want to match Field_Name values that contain 'bla%bla&2', then you need to write this:
set define off
Select * From Some_Table Where Field_Name Like '%bla\%bla&2%' escape '\';
You get to specify which character you want to use to escape a following character (thanks should go to mathguy, not me). You also have to set define off to prevent sqlplus from trying to substitute values in a string.
If, however, you want to match Field_Name values that exactly equal the given string, then you do this instead:
set define off
Select * From Some_Table Where Field_Name = 'bla%bla&2';
If I am not mistakend you escape them with a backslash (\)
Select * From Some_Table Where Field_Name Like 'bla\%bla&2' ESCAPE '\';
Use escape \ to treat is a literal
SELECT *
FROM Some_Table
WHERE Field_Name LIKE 'blah\%'|| 'blah' ||'&'|| '2';
I'll guess that you're using a tool which treats &n, where n is a digit, as a variable marker. If you're using SQL*Plus or SQL Developer you'd need to issue the SQL*Plus command SET DEFINE OFF. Other tools may use other methods to accomplish this.
Best of luck.
I do not think the backslash escape character will work here for the ampersand. Instead, you will want to divide your search into concatenated strings using double pipes. Use single quotes around each piece of literal text. Next, replace the & with chr(38) which is the ampersand. You can see this with:
select chr(38) from dual
You will still want to include the backslash before the % and finish your statement with escape '\'. Notice, I did not quote the chr(38).
select * From Some_Table Where Field_Name Like 'bla\%bla'||chr(38)||'bla' escape '\'

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