How to escape square bracket when using LIKE? [duplicate] - sql

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
SQL Server LIKE containing bracket characters
I am having a problem with pattern matching.I have created two objects say,with codes
1)[blah1]
2)[blah2] respectively
in the search tab,suppose if i give "[blah" as the pattern,its returning all the strings
i.e., [blah1] and [blah2]
The query is
select *
from table1
where code like N'%[blah%'
I guess the problem is with the condition and special characters. Please do revert if you have as solution. Is there any solution where we can escape the character"[". I tried to change the condition as N'%[[blah%'.But even then its returning all the objects that is in the table.

When you don't close the square bracket, the result is not specified.
However, the story is different when you close the bracket, i.e.
select *
from table1
where code like N'%[blah%]%'
In this case, it becomes a match for (any) + any of ('b','l','a','h','%') + (any). For SQL Server, you can escape characters using the ESCAPE clause.
select * from table1 where code like N'%\[blah%\]%' escape '\'
SQL Fiddle with examples

You can escape a literal bracket character this way:
select *
from table1
where code like N'%[[]blah%'
Source: LIKE (Transact-SQL), under the section "Using Wildcard Characters As Literals."
I guess this is Microsoft's way of being consistent, since they use brackets to delimit table and column identifiers too. But the use of brackets is not standard SQL. For that matter, bracket as a metacharacter in LIKE patterns is not standard SQL either, so it's not necessary to escape it at all in other brands of database.

As per My understanding, the symbol '[', there is no effect in query. like if you query with symbol and without symbol it shows same result.
Either you can skip the unwanted character at UI Level.
select * from table1 where code like '%[blah%'
select * from table1 where code like '%blah%'
Both shows same result.

Related

How to find a row where col have special characters or numbers (except hyphen,apostrophe and space) in Oracle SQL

I need to find rows where col have special characters or numbers (except hyphen,apostrophe and space) in Oracle SQL.
I am doing like below:
SELECT *
FROM test
WHERE Name_test LIKE '%[^A-Za-z _]%'
But It is not working and I also need to exclude any apostrophe.
Kindly help.
If you need to find all rows where column have ONLY numbers and special characters (and you can specify all of required special characters):
SELECT *
FROM test
WHERE regexp_like(Name_test, q['^[0-9'%##]+$]')
as you can see you just need to add your special characters after 0-9.
^ - start
$ - end
About format q'[SOMETHING]' please see TEXT LITERALS here: https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/Literals.html#GUID-1824CBAA-6E16-4921-B2A6-112FB02248DA
If you need to find all rows where column have no alpha-characters:
SELECT *
FROM test
WHERE regexp_like(Name_test, '^[^a-zA-Z]*$');
or
SELECT *
FROM test
WHERE regexp_like(Name_test, '^\W*$');
about \W - please see "Table 8-5 PERL-Influenced Operators in Oracle SQL Regular Expressions" here:
https://docs.oracle.com/database/121/ADFNS/adfns_regexp.htm#ADFNS235
I need to find rows where col have special characters or numbers (except hyphen, apostrophe and space [and presumably single quotes]) in Oracle SQL.
You can use double single quotes to put a single quote in:
WHERE Name_test LIKE '%[^-A-Za-z _'']%'
However, this is not Oracle syntax. If the above works, then I would guess you are using SQL Server. In Oracle:
WHERE REGEXP_LIKE(Name_test, '[^A-Za-z _'']')

SQL : REGEX MATCH - Character followed by numbers inside quotes

I have a column in sql which holds value inside double quotes like "P1234567" , "P1234" etc..
I need to identify only columns which start with letter P and is followed by seven digits (numbers) only. I tried where column like'"P[0-9][0-9][0-9][0-9][0-9][0-9][0-9]"' but it doesn't seem to work.
Can someone please correct me or point me to a thread which can help me out?
Thanks
Standard SQL has no regex support, but most SQL engines have regex extensions added to them on top of the standard SQL. So, for example, if you're using MySQL then you'd do this:
... WHERE column REGEXP '^"P[0-9]{7}"'
And if you're using Postgres then that would be:
... WHERE column ~ '^"P[0-9]{7}"'
(updated to match the double-quote part of the question, I'd misunderstood that to begin with)
How about using length and isnumeric:
Select
*
from
mytable
where
mycolumn like '"P%'
and len(mycolumn) = 10 --2 chars for quotes + 1 for 'P' + 7 for the digits
and isnumeric(substring(mycolumn, 3, 7))=1
This answer is for SQL Server, other DBMS's may have a different syntax for length

SQL SELECT WHERE Columns Data begins and ends with [ ] i.e. [Unchecked] [duplicate]

This question already has answers here:
How can I escape square brackets in a LIKE clause?
(10 answers)
Closed 8 years ago.
As the title says im trying to do a select statement in my database to find data that begins and ends with square brackets i.e. [Unchecked]
I've tried the following select query with like but it doesn't seem to be correct...
SELECT * FROM [DB_Table] WHERE [DB_Column] LIKE '[%'
This doesn't seem to get any of the data that starts with [. although if i change the [ to a letter such as A it gets all the data beginning with A.
is it possible to do Like '[%' (beginning with) and '%]' (and ending with)?
This link How can I escape square brackets in a LIKE clause? is not a duplicate of mine it is completely different.
Change your query to
SELECT * FROM [DB_Table] WHERE [DB_Column] Like '[[]%'
You need to wrap the '[' in square brackets to escape it and treat it as text to be used for searching. Note that this is for SQL Server.
Also, if you wanted to search for values starting with '[' and ending with ']', you could do this:
SELECT * FROM [DB_Table] WHERE [DB_Column] Like '[[]%]'
Try this...
SELECT * FROM [DB_Table]
WHERE [DB_Column] Like '\[%' ESCAPE '\'
OR
SELECT * FROM [DB_Table]
WHERE [DB_Column] Like '%\[Unchecked\]%' ESCAPE '\'

Escaping a single quote in Oracle regex query

This is really starting to hurt!
I'm attempting to write a query in Oracle developer using a regex condition
My objective is to find all last names that contain charachters not commonly contained in names (non-alpha, spaces, hyphens and single quotes)
i.e.
I need to find
J00ls
McDonald "Macca"
Smithy (Smith)
and NOT find
Smith
Mckenzie-Smith
El Hassan
O'Dowd
My present query is
select * from dm_name
WHERE regexp_like(last_name, '([^A-Za-z -])')
and batch_id = 'ATEST';
which excludes everything expected except the single quote. When it comes to putting the single quote character, the Oracvel SQL Develoepr parser takes it as a literal.
I've tried:
\' -- but got a "missing right parenthesis" error
||chr(39)|| -- but the search returned nothing
'' -- negated the previous character in the matching group e.g. '([^A-Za-z -''])' made names with '-' return.
I'd appreciate anything you could offer.
Just double the single quote to escape your quote.
So
select *
from dm_name
where regexp_like(last_name, '[^A-Za-z ''-]')
and batch_id = 'ATEST'
See also this sqlfiddle. Note, I tried a similar query in SQL developer and that worked as well as the fiddle.
Note also, for this to work the - character has to be the last character in the group as otherwise it tries to find the group SPACE to ' rather than the character -.
The following works:
select *
from dm_name
WHERE regexp_like(last_name, '([^A-Za-z ''-])');
See this SQLFiddle.
Whether SQL Developer will like it or not is something I cannot attest to as I don't have that product installed.
Share and enjoy.

regarding like query operator

For the below data (well..there are many more nodes in the team foundation server table which i need to refer to..below is just a sample)
Nodes
------------------------
\node1\node2\node3\
\node1\node2\node5\
\node1\node2\node3\node4\
\node1\node2\node3\node4\node5\
I was wondering if i can apply something like (below query does not give the required results)
select * from table_a where nodes like '\node1\node2\%\'
to get the below data
\node1\node2\node3\
\node1\node2\node5\
and something like (below does not give the required results)
select * from table_a where nodes like '\node1\node2\%\%\'
to get
\node1\node2\node3\
\node1\node2\node5\
\node1\node2\node3\node4\
Can the above be done with like operator? Pls. suggest.
Thanks
You'll need to combine two terms, LIKE and NOT LIKE:
select * from table_a where
nodes like '\node1\node2\%\' AND
nodes NOT like '\node1\node2\%\%\'
for the first query, and a similar solution for the second. That's with "plain SQL". There are probably SQL Server specific functions which will count the number of "\" characters in the column, for instance.
maybe use the delimiter to get the resutls.
it is unclear what you are actually trying to get, but you could use the
substr
function to either count or find the position of the delimiter '/' character.
It seems like this would work (basically just eliminating the last backslash):
select * from table_a where nodes like '\node1\node2\%\%'
EDIT
You could also try this:
select * from table_a where
nodes like '\node1\node2\%\' or
nodes like '\node1\node2\%\%\'
A little late to the party, but it appears that the problem is still open. Could it be that the backslashes are escaping the wildcard meaning of the percent signs? And the backslash n could be getting interpreted as well.
Doesn't sql-server know a wildcard for a single character?
select * from table_a
where nodes LIKE '#node1#node2#node_#';
nodes
---------------------
#node1#node2#node5#
#node1#node2#node3#
I testet this on postgresql, where it is hard to insert a backslash, which is the reason why I replaced them with #.
Here is another possibility - negate more than one backslash (# used for my convenience):
SELECT * FROM table_a
WHERE (nodes LIKE '#node1#node2#%#'
AND NOT nodes LIKE '#node1#node2#%#%#');
On postgresql there is too the possibility to match against patterns, with SIMILAR TO, or ~:
SELECT * FROM table_a
WHERE nodes SIMILAR TO '#node1#node2#[^#]*#';
nodes
---------------------
#node1#node2#node5#
#node1#node2#node3#
[] encapsulates a group of alternatively allowed characters, for example [aeiou] would be a lowercase vocal. But when the caret is the first sign in the brackets, the sign(s) are negated so [^aeiou] would mean anything but a lowercase vocal, and [^#] means anything but a #.
The asterix behind that expression means that the preceding sign can occur as often as you like, 0 to million times. (+ would mean at least one times, ? would mean 0 or 1 times).
So '#node1#node2#[^#]*#' means '#node1#node2#', followed by anything but a hash, 0 or single or multiple times, and then, finally a hash.