Escape '%' while using Oracle's REGEXP_LIKE function [duplicate] - sql

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+%,');

Related

Splitting a string by CRLF in SQL [duplicate]

This question already has answers here:
SQL Server 2005 Weird varchar Behavior
(2 answers)
Closed 6 months ago.
SO I know that there are a few post like this already here, but those post don't seem to work for me. My string contains '^' in it, and replace does not seem to like that character. Because of this, SELECT * FROM STRING_SPLIT(REPLACE(#InbMsg,#CLRF,'|'),'|')
does not seem to work The example message is this:
'W^1^Wave1102^2^11
H^12345678900987654321^OD1128263^MLO^7^Bill’s Order^98712391^N^A2^3^11
D^OGMens77162^123456789009^Y^4^Medium^Red^006134^000101^11728492'
When I run it through the previous statment, I just get 'W'. Anyone know why?
You want to replace the newline between 11 and H and also between 11 and D. You can replace the newline by looking for CHAR(10) then replace it with '^'. Then you can now split the rows using '^' as delimiter.
select * from STRING_SPLIT(REPLACE('W^1^Wave1102^2^11
H^12345678900987654321^OD1128263^MLO^7^Bill’s Order^98712391^N^A2^3^11
D^OGMens77162^123456789009^Y^4^Medium^Red^006134^000101^11728492',CHAR(10),'^'),'^');
Sample result:
https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=0dc110c889af42a8b02096eedee3c393
value
W
1
Wave1102
2
11
H
12345678900987654321
OD1128263
MLO
7
…
10 rows of 26
CHAR(10) is ascii value for New Line / Line Break for SQL Server
It was a situation of not using varchar correctly. Make sure to declare variables correctly! ```SELECT * FROM STRING-SPLIT(REPLACE(#msg, #CLRF, ','),',') works in this situation!

How to search the underscore in a column for SQL? [duplicate]

This question already has answers here:
SQL Server Escape an Underscore
(8 answers)
Closed last year.
How to search for the underscore in a column for SQL?
I tried:
select * from xxx where xxx like '%_%';
and
select * from xxx where xxx like '%$_%';
But that doesn't work as expected. How do I escape underscore in SQL LIKE Operator?
Try this:
SELECT * FROM xxx WHERE xxx LIKE '%[_]d'

How to Search for % using SQL and Wildcards? [duplicate]

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 '%[%]%'

NOT LIKE IN statement in SQL [duplicate]

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.

How to replace characters in SQL [duplicate]

This question already has an answer here:
Converting special character to plain text in oracle
(1 answer)
Closed 5 years ago.
I'm a new SQL user, and I'd like to know if there is a simple way of replacing non-English characters in Oracle SQl-developer. Let's Say, I want Hernán Nuñez to show up as Hernan Nunez, but without having to actually replace "Hernán" with "Hernan". What I need is to replace everything that contains "á" to the same thing, but with "a" instead, for example. Any suggestions?
US7ASCII will give you the right result. Example:
SELECT CONVERT('BESANÇON','US7ASCII')
FROM table;
CONVERT(
--------
BESANCON
1 row selected.