This question already has answers here:
How do I escape a percentage sign in T-SQL?
(5 answers)
Closed 3 years ago.
I have to replace % in a number of fields. I need to get a list of the records to be changed first. I know how to do the actual REPLACE easily enough, but my query to find the records isn't working correctly.
SELECT * FROM inventory WHERE desc LIKE '%%%'
I also tried the following the the same results:
SELECT * FROM inventory WHERE desc LIKE '%'+CHAR(37)+'%'
What's the best way to search for %?
I am using SQL Server 2016.
You need to escape the wildcard:
where [desc] like '%$%%' escape '$'
or, use a character class:
where [desc] like '%[%]%'
Related
This question already has answers here:
What is the meaning of the prefix N in T-SQL statements and when should I use it?
(4 answers)
Closed 2 years ago.
I have a database column that is nvarchar(50). There are some records that might have an em dash in them.
I want to find those records.
However, when I copy and paste an em-dash from another program, SQL Server Management Studio treats it like a regular hyphen.
This just returns all the parts with hyphens even though it's an em-dash in between the percent signs:
select * from part
where partnum like '%−%'
How can I make SSMS search for the em-dash and not hyphens?
emdash: −
hyphen: -
In case anyone is wondering this was solved by learning how to use NVARCHAR searches. You can search for something you copy paste from another program by prefixing the search string with an 'N' like this:
SELECT * FROM part
WHERE PartNum LIKE N'%−%'
You could try something like this.
select * from part
where partnum like '%' + NCHAR(8212) + '%'
This question already has an answer here:
Oracle SQL Regex not returning expected results
(1 answer)
Closed 3 years ago.
I tied searching on SO about my issue but didn't find one. I apologize if it is a duplicate.
Let's say I have a table as shown below:
|ACCOUNT | FEES_DETAILS |
================================
|AX001 | AOF=£20.5,VAT=25% |
|AX009 | AOF=11.25%,VAT=12.5%|
I want to fetch all the rows when FEES_DETAILS column has a %-based AOF(in the above example, I need to fetch the second record - AX009 as AOF=11.25%).
I am executing the following query but it is not returning any row(s):
SELECT * FROM ACCOUNT_FEES_DATA WHERE REGEXP_LIKE(FEES_DETAILS, 'AOF\s*=\s*\d+(?:\.\d+)?%');
I thought % at the end of my regular expression AOF\s*=\s*\d+(?:\.\d+)?% is being treated as a special character so I tried to escape it with a \. But that too didn't return any rows. Is there any other way of escaping a % in REGEXP_LIKE function? Can anyone help me with this?
Try this:
select * from test where REGEXP_LIKE(fees,'AOF=\d+.\d+%,');
This question already has answers here:
PL/SQL, how to escape single quote in a string?
(5 answers)
How to anticipate and escape single quote ' in oracle
(2 answers)
How to handle a single quote in Oracle SQL
(2 answers)
Closed 4 years ago.
I need to find a below record
O'GRAD
I wanted to use LIKE
LIKE 'O'GRAD'
But i come across a problem with apostrophe.
What would be the best way around it?
Double up the apostrophe
LIKE 'O''GRAD'
or use the Q syntax
LIKE q'{O'GRAD}'
if your Oracle DB version is 10g or upper you may use :
select *
from mytable t
where t.col1 like '%'||q'$O'GRAD$'||'%';
/
or classically add an extra quote to existing quote
select *
from mytable t
where like '%'||'O''GRAD'||'%';
/
to overcome the single quotation mark problem.
This question already has answers here:
Is there a combination of "LIKE" and "IN" in SQL?
(28 answers)
Closed 5 years ago.
I need to print names that do not start and end with vowel. I tried it like this:
SELECT DISTINCT name FROM people WHERE
lower(name) NOT LIKE IN ('a','e','i','o','u')%('a','e','i','o','u');
I got error.
You may want to try avoid using REGEXP from performance reasons in case of large data sets.
In such case is TRANSLATE your friend.
1) translate all vowels to one representative
2) perform normal LIKE predicate with the selected vowel
select txt from tab1
where translate(lower(txt),'aeiou','aaaaa') not like 'a%a';
REGEXPs are mighty, but should not be used on non-trivial data sets in case that they could be avoided. (My 8M rows test data gives 7 seconds elapsed using TRANSLATE vs. 2+ minutes with REGEXP).
You can use regexp_like with match parameter i for case insensitive matching:
select distinct name from people
where not regexp_like(name, '^[aeiou]($|.*[aeiou]$)', 'i');
Pattern details:
^[aeiou] - starts with a vowel
($|.*[aeiou]$) - either there is only one character (matched in the first step) or ends with a vowel.
This question already has answers here:
How do I escape a percentage sign in T-SQL?
(5 answers)
Closed 6 years ago.
Since the percentage sign (%) is the wild card in SQL I am having trouble with filtering by values in a column that actually start with %.
Select *
From customer
Where last_name not like '%%'
I have some weird customer instances where the last names are %Smith, %Johnson, etc. I don't want to remove the %, I just want those instances to not be returned in my result set.
You should be able to escape this by using square brackets around the given character(s) that you want to escape :
SELECT *
FROM Customer
WHERE last_name NOT LIKE '[%]%'