I have following table structure in my DB
ID Name
--------------------------
ID_1 Name1
ID1 Name2
when I execute the following query
SELECT * FROM tblNames
WHERE ID LIKE '_1'
I get both records selected...
any suggestions how to get rid off it??
An underscore (_) is a special character in LIKE expressions that matches a single character.
To match an actual underscore, you need to escape it:
select * from tblNames where id like '%\_1' escape '\'
Here I'm telling SQL Server to consider backslash an escape character, and using it to escape the underscore.
Alternatively, you can represent the underscore as a character range with a single character in it - it will be interpreted literally in this case:
select * from tblNames where id like '%[_]1'
Which is a bit more succinct.
Reference: MSDN.
LIKE operator
Quickie: _ is a wildcard character that matches anything.
SELECT * FROM tblNames
WHERE ID LIKE '\_1' ESCAPE '\'
SELECT * FROM tblNames
WHERE ID LIKE '[_]1'
The underscore character is a wildcard for the like operator, and it matches any one character. In your case the pattern '_1' matches both '_1' and 'D1' in the data.
To use a literal underscore character in a pattern you need to put brackets around it so that it becomes a character range containing only one character:
SELECT * FROM tblNames
WHERE ID LIKE '[_]1'
Related
I am trying to query Keyword 100% using Like command.
LIKE (‘%100%%’)
But the command is querying for all keywords with 100 which is not what I
want
Use Escape Character.
Try:
Select * from MyTable m where m.Column1 like '%100\%%' escape '\'
Escape Character can be set as per your convenience.
In the above query, replace MyTable with your table name and Column1 with your Column Name.
You could also take advantage of SQL Server's LIKE operator's regex syntax, and use [%] to represent a literal percent:
SELECT *
FROM yourTable
WHERE col LIKE '%100[%]%';
Demo
I prefer this method to the accepted answer because it makes more explicit the intention to represent a literal character, and it avoids the possible need for an ESCAPE clause.
I have few formats of records in a table, ABCDEF, [123]ABCDEF, ABCDEF[ABC] and ABCDEF[123] Numbers of letters and digits are not fixed, they vary.
I can easily find the ones with brackets by something like this and it's variations.
SELECT * FROM X WHERE Y LIKE '%/[%' ESCAPE '/'
But I want to find the records where there are only three numbers inside the brackets and only if they appear at the beginning or ending of the line. i.e [123]ABCDEF and ABCDEF[123] formats.
I tried LIKE '%[___/]' ESCAPE '/' and hoped to manipulate it only accepting numbers inside the bracket but wasn't able to make it work.
A huge caveat is there might be records such as ABC[123]DEF format. This might troublesome because I don't want such records returned. I don't know if there are such records but I can't dismiss the possibility.
How can I do what I'm trying to do?
Brackets are used to provide valid set of characters. [0-9] means any digit from 0 to 9. If bracket is used literally, it must be escaped:
DECLARE #t TABLE(T nvarchar(20))
INSERT #t values ('ABCDEF'), ('[123]ABCDEF'), ('ABCDEF[ABC]'), ('ABCDEF[123]'), ('AS[123]AS')
SELECT *
FROM #t
WHERE T LIKE '\[[0-9][0-9][0-9]\]%' ESCAPE '\'
OR T LIKE '%\[[0-9][0-9][0-9]\]' ESCAPE '\'
--Or match anywhere
--WHERE T LIKE '%\[[0-9][0-9][0-9]\]%' ESCAPE '\'
Result
T
--------------------
[123]ABCDEF
ABCDEF[123]
This question already has answers here:
How do I escape a percentage sign in T-SQL?
(5 answers)
Closed 7 years ago.
I need a SELECT statement that will return all rows where a given field contains a percentage sign (%).
I've tried the following but it just pulls out all records:
select * from MyTable where MyColumn LIKE '%%%'
Escape using [] bracket symbol
select * from MyTable where MyColumn LIKE '%[%]%'
or by using ESCAPE keyword
select * from MyTable where MyColumn LIKE '%\%%' ESCAPE '\'
There are 2 ways to escape these wild cards.
You can use the default square braces “[]” like this- [%] or [_]
You can define your own escape character by using the keyword ESCAPE at the end of your where clause.
Ex. select * from name_column where name like ‘gyan_sagar’ ESCAPE ‘\’
In the example above we escape “_” by defining “\” as an escape character using the ESCAPE keyword.
Note: You can only define a single character for escaping and not a string.
You can search any special charecters by using this
SELECT * FROM mytable WHERE email LIKE '%[^0-9a-zA-Z #\.\-]%'
I am using Hibernate Criteria with sql server 2000. I want to use the like method to get records from the database that are literal %. Can that be done?
example:
table base_target_unit:
id code name
8a91b32c3be0a5fe013c03cdb37f0002 13010410 户
8a91b32c3af8cd71013bd4fe06160044 12122609 %
8a91b32c3af8cd71013bd106e9dc002e 12122507 元/MB
402881b03a8c83ae013a8ca467f70041 12102399 亿分钟
402881b03a8c83ae013a8ca451610040 12102398 万分钟
402881b03a8c83ae013a8ca42cb3003f 12102397 万户
The sql I have is:
select * from dbo.base_target_unit where name LIKE '%/%%' ESCAPE '/'
I want to get the record that name which is %.
Use brackets. So to look for 88%
WHERE MyColumn LIKE '%88[%]%'
You can escape the percent sign with the SQL LIKE function by using two percents (%%)
This tells you more about how to do it:
http://msdn.microsoft.com/en-us/library/aa933232%28v=sql.80%29.aspx
select * from dbo.base_target_unit where name LIKE '%%' ESCAPE '%'
How do I escape a string in SQL Server's stored procedure so that it is safe to use in LIKE expression.
Suppose I have an NVARCHAR variable like so:
declare #myString NVARCHAR(100);
And I want to use it in a LIKE expression:
... WHERE ... LIKE '%' + #myString + '%';
How do I escape the string (more specifically, characters that are meaningful to LIKE pattern matching, e.g. % or ?) in T-SQL, so that it is safe to use in this manner?
For example, given:
#myString = 'aa%bb'
I want:
WHERE ... LIKE '%' + #somehowEscapedMyString + '%'
to match 'aa%bb', 'caa%bbc' but not 'aaxbb' or 'caaxbb'.
To escape special characters in a LIKE expression you prefix them with an escape character. You get to choose which escape char to use with the ESCAPE keyword. (MSDN Ref)
For example this escapes the % symbol, using \ as the escape char:
select * from table where myfield like '%15\% off%' ESCAPE '\'
If you don't know what characters will be in your string, and you don't want to treat them as wildcards, you can prefix all wildcard characters with an escape char, eg:
set #myString = replace(
replace(
replace(
replace( #myString
, '\', '\\' )
, '%', '\%' )
, '_', '\_' )
, '[', '\[' )
(Note that you have to escape your escape char too, and make sure that's the inner replace so you don't escape the ones added from the other replace statements). Then you can use something like this:
select * from table where myfield like '%' + #myString + '%' ESCAPE '\'
Also remember to allocate more space for your #myString variable as it will become longer with the string replacement.
Had a similar problem (using NHibernate, so the ESCAPE keyword would have been very difficult) and solved it using the bracket characters. So your sample would become
WHERE ... LIKE '%aa[%]bb%'
If you need proof:
create table test (field nvarchar(100))
go
insert test values ('abcdef%hijklm')
insert test values ('abcdefghijklm')
go
select * from test where field like 'abcdef[%]hijklm'
go
Rather than escaping all characters in a string that have particular significance in the pattern syntax given that you are using a leading wildcard in the pattern it is quicker and easier just to do.
SELECT *
FROM YourTable
WHERE CHARINDEX(#myString , YourColumn) > 0
In cases where you are not using a leading wildcard the approach above should be avoided however as it cannot use an index on YourColumn.
Additionally in cases where the optimum execution plan will vary according to the number of matching rows the estimates may be better when using LIKE with the square bracket escaping syntax when compared to both CHARINDEX and the ESCAPE keyword.
You specify the escape character. Documentation here:
http://msdn.microsoft.com/en-us/library/ms179859.aspx
Do you want to look for strings that include an escape character? For instance you want this:
select * from table where myfield like '%10%%'.
Where you want to search for all fields with 10%? If that is the case then you may use the ESCAPE clause to specify an escape character and escape the wildcard character.
select * from table where myfield like '%10!%%' ESCAPE '!'
Alternative escaping syntax:
LIKE Wildcard Literals
The JDBC driver supports the {escape 'escape character'} syntax for using LIKE clause wildcards as literals.
SELECT *
FROM tab
WHERE col LIKE 'a\_c' {escape '\'};
db<>fiddle demo