If I want to select strings with underscores using Oracle SQL Developer, how do I have to escape those?
I tried:
name like '%_%'
name like '%'_%'
name like '%\_%'
but none of this helped.
You need to use the explicit escape; in this way you can decide a character to use for escaping and then use it in your LIKE.
For example, here I use the '!' to escape special characters:
select str
from (
select 'a_b' str from dual union all
select 'ab' from dual
)
where str like '%!_%' escape '!'
gives
STR
---
a_b
Oracle suggests the code below which workded for me:
SELECT last_name
FROM employees
WHERE last_name LIKE '%A\_B%' ESCAPE '\'
ORDER BY last_name;
Related
I have a table with an email field this field can only have the following characters:
'abcdefghijklmnopqrstuvwxyz0123456789. # _- +'
How can you check the email field to know if I have any different characters from the ones I mentioned ('abcdefghijklmnopqrstuvwxyz0123456789. # _- +')?
This sounds like a perfect job for a regular expression - just check whether the E-Mail contains any characters that are not in your list. You can use regexp_like for this:
regexp_like(e_mail, '[^-a-z0-9.#_ +]')
(I've replaced a...z and 0..9 with the respective ranges - shorter and more readable. Note that the hyphen '-' has to be the first character after the initial caret '^' to indicate that it is a literal hyphen and not part of a character range).
Simple test case:
with v_data(e_mail) as (
select 'xyz#abc.com' from dual union all
select 'xyz(#def.com' from dual union all
select 'ab123-def#gmail.com' from dual
)
select
e_mail,
(case
when regexp_like(e_mail, '[^-a-z0-9.#_ +]') then 'NO'
else 'YES'
end) as is_valid_email
from v_data
However, a valid E-Mail adresse can contain tons of additional characters - uppercase letters for example.
I have this value '45465,6464,654' And I want to remove second comma and string after it. So basically I want ''45465,6464' . I found a solution but its for Mssql. How can I make this query for Oracle I couldnt do it even with substring. Can you help me?
This for MSSQL;
`declare #S varchar(20) = '45465#6464#654';
select left(#S, charindex('#', #S, charindex('#', #S)+1)-1);`
You can use something very similar:
WITH s AS (SELECT '45465#6464#654' s FROM dual)
SELECT SUBSTR(s,1,INSTR(s,'#',1,2)-1) FROM s
or you can use regular Expression:
SELECT regexp_substr('45465#6464#654','([^#]*#)?[^#]*') from dual
In my table, I have data like PAT5DSA-(ALRP), LAR6DAOP-(RAH) etc..But I want to remove the strings like -(xxxx) or -(xxx) which can be any alphabets inside braces. Tried using the below:
select replace(:code,'-(Aa-Zz)',null) from employee;
But this didn't work..Can anyone please help?
We can do a regex replacement using REGEXP_REPLACE:
SELECT REGEXP_REPLACE('PAT5DSA-(ALRP)', '-\(.*?\)', '')
FROM dual;
PAT5DSA
The plain replace() doesn't understand patterns. You could use a regular expression replace, e.g.:
-- CTE for sample data
with cte (code) as (
select 'PAT5DSA-(ALRP)' from dual
union all
select 'LAR6DAOP-(RAH)' from dual
)
select code, regexp_replace(code, '-\(.*?\)$') as result
from cte;
CODE RESULT
-------------- --------------
PAT5DSA-(ALRP) PAT5DSA
LAR6DAOP-(RAH) LAR6DAOP
This will remove anything inside a pair of parentheses which is preceded by a dash, at the end of the original string. If the parentheses to be removed could be anywhere in the string then remove the $.
Use INSTR and SUBSTR:
WITH cteVals AS (SELECT 'PAT5DSA-(ALRP)' AS COL_VAL FROM DUAL UNION ALL
SELECT 'LAR6DAOP-(RAH)' AS COL_VAL FROM DUAL)
SELECT SUBSTR(COL_VAL, 1, INSTR(COL_VAL, '-')-1)
FROM cteVals;
Best of luck.
What are those exclamation(!) marks in the following query?
l_sql := q'!
SELECT CASE WHEN FILTER_CNT = 0 THEN NULL ELSE FILTER_LIST END AS FILTER_LIST
FROM
(
SELECT 'TABLE_NAME NOT LIKE ' || LISTAGG(''''||EXCLUSION_CRITERIA||'''', ' AND TABLE_NAME NOT LIKE ') WITHIN GROUP (ORDER BY ROWNUM) AS FILTER_LIST, COUNT(*) AS FILTER_CNT
FROM !'|| p_after_owner ||q'!.UT_TABLE_EXCLUSION
WHERE EXCLUSION_TYPE = 'P'
)
!';
The q method of quoting strings means that you don't have to escape single quotes in the string.
As per the documentation, it works like this:
q'<single character delimiter><text><single character delimiter>'
Therefore, the !s in your sample string are acting as the quote delimiter.
For example, if you want to use the string Don't worry, be happy, in the old style quoting, that would become:
'Don''t worry, be happy'
but the new quoting mechanism would be:
q'!Don't worry, be happy!'
or
q'{Don't worry, be happy}'
etc.
I have a PLSQL string which contains chr() special characters like chr(10), chr(13). I want to replace these special characters from the string. I tried the following ways
select regexp_replace('Hello chr(10)Goodchr(13)Morning','CHR(10)|chr(13)','') from dual;
This do not works since regexp_replace do not replace chr() function. Then I tried
select translate('Hello chr(10)Goodchr(13)Morning', 'chr(10)'||'chr(13)', ' ') from dual;
It works partially, ie; I am forced to replace chr() with white space(third parameter). No option if I do not want to replace chr() with white spaces . If I pass third character as null then above query returns null result.
Anybody have any other methods?
The problem with your replacement isn't the logic, it's the syntax. Parentheses are regex metacharacters, and as such, if you want to replace a literal parenthesis, you need to escape it. So your pattern should be this:
chr\(13\)|chr\(10\)
Here is a working query:
select
regexp_replace('Hello chr(10)Goodchr(13)Morning','chr\(13\)|chr\(10\)','', 1, 0, 'i')
from dual
The fifth parameters in the above call to regexp_replace is 'i' and indicates that we want to do a case insensitive replacement.
Demo
The above logic removes the literal text chr(13) and chr(10) from your text. If instead you want to remove the actual control characters chr(13) and chr(10), then you may add those control characters to the alternation, e.g.
select
regexp_replace('Hello chr(10)Goodchr(13)Morning','chr\(13\)|chr\(10\)|chr(10)|chr(13)','', 1, 0, 'i')
from dual
Since regular expression functions are relatively expensive in Oracle I think it's worth showing the alternate method which just uses REPLACE for the same effect.
This replaces occurrences of the each control characters with a space;
SELECT REPLACE(REPLACE('ABC'||CHR(10)||'DEF'||CHR(13)||'GHI'||CHR(10)||'JKL',CHR(13),' '),CHR(10),' ') from dual
And this replaces occurrences of the strings 'CHR(13)' and 'CHR(10)';
select REPLACE(REPLACE('ABCCHR(10)DEFCHR(13)GHICHR(10)JKL','CHR(13)',' '),'CHR(10)',' ') from dual