name
------
[pratik]
[Ishaan,llc,ltd]
[sundaar,j]
[sid,h]
output should be like:
name last_name
------ ---------
[pratik] null
[Ishaan,llc,ltd] ltd
[sundaar,j] j
[sid,h] h
is there any way by which we can achieve the above in Oracle SQL
You can use regexp_substr()
:
select replace(replace(regexp_substr(name, ',([^,]+)\]$'), ',', ''), ']', '') as last_name
Or using regexp_replace()
:
select regexp_replace(name, '^(.*,|[\[a-zA-Z]+)([^,]*)\]$', '\2')
Here is the db<>fiddle.
I should note that storing multiple values in a string is a bad idea. SQL and Oracle have many better solutions than overloading the definition of a string.
You can use the regexp_substr
with regexp_count
oe instr
to achieve the desired result as following:
Select case when regexp_count(name, ',') > 0
then replace(regexp_substr(name, '[^,]+$'),']','')
end
From your_table
Cheers!!
This option uses nested regexp_substr
:
]
bracket. For example, for [sundaar,j]
, it returns ,j]
,
and ]
So:
SQL> with test (name) as
2 (select '[pratik]' from dual union all
3 select '[Ishaan,llc,ltd]' from dual union all
4 select '[sundaar,j]' from dual union all
5 select '[sid,h]' from dual
6 )
7 select name,
8 regexp_substr(regexp_substr(name, ',\w+]$'), '\w+') last_name
9 from test;
NAME LAST_NAME
---------------- --------------------
[pratik]
[Ishaan,llc,ltd] ltd
[sundaar,j] j
[sid,h] h
SQL>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.