How to define default escape character in Oracle? - oracle

I have table
id name
__________
1 name1
2 name2
3 _name3
and I want to select all names, that starting with '_' character.
SELECT name FROM table1 WHERE name like '_%'
But this query returns all rows from table. Maybe anyone knows some solution for this problem (without using ESCAPE keyword)? Or, is there opportunity to set default escape character in Oracle?

Apparently, you cannot:
char1 [ NOT ] { LIKE | LIKEC | LIKE2 | LIKE4 }
char2 [ ESCAPE esc_char ]
[...]
If esc_char is not specified, then there is no default escape
character.

I want to select all names, that starting with '_' character.
Use SUBSTR.
SQL> WITH DATA AS(
2 SELECT 1 ID, 'name1' NAME FROM dual UNION ALL
3 SELECT 2, 'name2' FROM dual UNION ALL
4 SELECT 3 , '_name3' FROM dual
5 )
6 SELECT * FROM DATA
7 WHERE substr(NAME, 1, 1) = '_'
8 /
ID NAME
---------- ------
3 _name3
SQL>

Try this..
select * from table1 where regexp_like (name,'^_') ;

Related

Need to extract data from string by regexp_substr

I have a ["1101124","1101123","123456"], I need to get the end result as rows for the numbers which are in the bracket.
How can I achieve this by using regular expression in Oracle.
Don't use regular expression to try to parse JSON data; use a proper JSON parser:
SELECT value
FROM JSON_TABLE(
'["1101124","1101123","123456"]',
'$[*]'
COLUMNS(
value VARCHAR2(20) PATH '$'
)
)
Outputs:
VALUE
1101124
1101123
123456
db<>fiddle here
If ["1101124","1101123","123456"] is a string:
SQL> WITH DATA AS
2 ( SELECT '["1101124","1101123","123456"]' str FROM dual
3 )
4 SELECT trim(regexp_substr(str, '[0-9]+', 1, LEVEL)) str
5 FROM DATA
6 CONNECT BY regexp_substr(str , '[0-9]+', 1, LEVEL) IS NOT NULL
7 /
STR
----------------------------------------
1101124
1101123
123456
3 rows selected.
SQL>

Reverse of ascii function in Oracle

When I do following query
select asciistr(first_name) from person where id = 1
the result contains '\200D'.
So what is the reverse function of converting a '\200D' to character, so I can find all names with that specific character.
You want UNISTR (and, maybe, CAST it to a VARCHAR2). If you have the table:
CREATE TABLE person ( first_name, id ) AS
SELECT 'A', 1 FROM DUAL UNION ALL
SELECT CAST( UNISTR( '\200D' ) AS VARCHAR2(20) ), 1 FROM DUAL;
Then the output from your query:
select first_name,
asciistr(first_name)
from person
where id = 1
Is:
FIRST_NAME | ASCIISTR(FIRST_NAME)
:--------- | :-------------------
A | A
? | \200D
db<>fiddle here
\200D is the U+200D ZERO WIDTH JOINER
If you like to find names with this (non printable) character try
SELECT *
FROM person
WHERE first_name LIKE '%'||UNISTR('\200D')||'%'
or
WHERE asciistr(first_name) LIKE '%\200D%'

Getting data from a column only if it contains number or special characters in Oracle

I want data from a column only if it contains special characters or numbers.
My query looks as follows:
Select First_name from account where regexp_like(First_name,'[0-9]')
But I don't know how to achieve that special character thing
Well you're most of the way there already, just add the special characters you are interested in to the character class you've already defined in the regexp:
Select First_name
from account
where regexp_like(First_name,'[0-9##$_!?*]')
To also select records where First_name is null use one of the two following queries:
Select First_name
from account
where regexp_like(First_name,'[0-9##$_!?*]')
or First_name is null
or
Select First_name
from account
where regexp_like(nvl(First_name,'!'),'[0-9##$_!?*]')
The first one explicitly selects rows where First_name is null while the second query gets it by substituting a special character string for null strings.
How about upside-down approach? Select values that aren't all letters. How? Remove them from the string! Something like this:
SQL> with account (id, first_name) as
2 (select 1, 'Little' from dual union all -- valid
3 select 2, 'der Leyen' from dual union all -- valid
4 select 3, 'F00t' from dual union all -- invalid; two zeros
5 select 4, 'Sco#tt' from dual union all -- invalid; #
6 select 5, 'Me#SO' from dual union all -- invalid; #
7 select 6, 'What_is_it' from dual union all -- invalid; _
8 select 7, '12.345' from dual union all -- invalid; digits
9 select 8, 'Huh? Whoa!' from dual -- invalid; ?!
10 )
11 select id,
12 first_name,
13 regexp_replace(first_name, '[[:alpha:] ]', null) repl
14 from account
15 where regexp_replace(first_name, '[[:alpha:] ]', null) is not null
16 order by id;
ID FIRST_NAME REPL
---------- ---------- ----------
3 F00t 00
4 Sco#tt #
5 Me#SO #
6 What_is_it __
7 12.345 12.345
8 Huh? Whoa! ?!
6 rows selected.
SQL>
Column REPL is here to show what's left after replacing letters (and a space) with null; you wouldn't normally display it.

Concat a string based on condition in oracle sql developer

I want to concat to the output based on condition. Here is my query:
select 'hey',
case id =1 then 'Mary' else 'Tom' end
from names;'
I want to print 'hey tom' or 'hey mary' based on id ... any help ??
It is concatenation you need; in Oracle, double pipe || represents that operator:
SQL> with names (id) as
2 (select 1 from dual union all
3 select 2 from dual union all
4 select 3 from dual
5 )
6 select id,
7 'hey ' || case when id = 1 then 'Mary'
8 else 'Tom'
9 end result
10 from names;
ID RESULT
---------- ----------
1 hey Mary
2 hey Tom
3 hey Tom
SQL>
You can run something like this query:
SELECT CONCAT('hey ', FIRST_NAME) from names where ID = '1';

split string in oracle query

I am trying to fetch phone numbers from my Oracle database table. The phone numbers may be separated with comma or "/". Now I need to split those entries which have a "/" or comma and fetch the first part.
Follow this approach,
with t as (
select 'Test 1' name from dual
union
select 'Test 2, extra 3' from dual
union
select 'Test 3/ extra 3' from dual
union
select ',extra 4' from dual
)
select
name,
regexp_instr(name, '[/,]') pos,
case
when regexp_instr(name, '[/,]') = 0 then name
else substr(name, 1, regexp_instr(name, '[/,]')-1)
end first_part
from
t
order by first_part
;
Lookup substr and instr functions or solve the puzzle using regexp.
I added a table test with one column phone_num. And added rows similar to your description.
select *
from test;
PHONE_NUM
------------------------------
0123456789
0123456789/1234
0123456789,1234
3 rows selected.
select
case
when instr(phone_num, '/') > 0 then substr(phone_num, 0, instr(phone_num, '/')-1)
when instr(phone_num, ',') > 0 then substr(phone_num, 0, instr(phone_num, ',')-1)
else phone_num
end phone_num
from test
PHONE_NUM
------------------------------
0123456789
0123456789
0123456789
3 rows selected.
This generally works. Although it will fail if you have rows with commas and slashes.

Resources