This question already has answers here:
Oracle REGEXP_SUBSTR | Fetch string between two delimiters
(2 answers)
Closed 2 years ago.
I want to retrieve a String between two characters.
I have whole string like "Attachments:Attachments~Attachment" and I want to take substring between : and ~ characters that is output will be Attachments.
How can be this done in SQL/Oracle select statement?
You can use the REGEXP_SUBSTR function. Starting with Oracle 11g, there is a parameter to the function, which lets you specify the capture group that you want returned:
SELECT regexp_substr('Attachments:Attachments~Attachment', '\:(.+)\~', 1,1,NULL,1) from dual;
There are workarounds for older versions (see also https://stackoverflow.com/a/7759146/14015737). You can shorten your result:
SELECT rtrim(ltrim(regexp_substr('Attachments:Attachments~Attachment', '\:(.+)\~'),':'), '~') FROM dual;
or
SELECT substr( match, 2, length(match)-2 ) from (
SELECT regexp_substr('Attachments:Attachments~Attachment', '\:(.+)\~') match FROM dual);
Related
This question already has answers here:
Split String by delimiter position using oracle SQL
(2 answers)
Closed last month.
I have a select query in Postgres with SPLIT_PART() function which works fine in postgres
SELECT SPLIT_PART(totalscore, '_', 1) highlow,
SPLIT_PART(totalscore, '_', 2) score,
score_desc
FROM student_score
WHERE (totalscore LIKE 'HN%' OR totalscore LIKE 'LN%')
http://sqlfiddle.com/#!15/877966/4
The SPLIT_PART() is not working in Oracle
Could please let me know how to achieve the same in Oracle as SPLIT_PART() is not working .
http://sqlfiddle.com/#!4/042f62/5
You can use REGEXP_SUBSTR() with [^_]+ patterns such as
SELECT REGEXP_SUBSTR(totalscore, '[^_]+') AS highlow,
REGEXP_SUBSTR(totalscore, '[^_]+$') AS score,
score_desc
FROM student_score
WHERE REGEXP_LIKE(totalscore,'^HN_|^LN_')
Demo
considering the whole dataset contains one and only one underscore per each value of the column.
This question already has answers here:
Concat function is not working - invalid number of arguments
(4 answers)
Closed 8 months ago.
I'm using a query to select all columns of a table and separate them by a comma(',') but in case if value of a column is null then I'll use string 'null' in place of a value.
The query I am trying is -
SELECT CONCAT(NVL(ID,'null'),',',NVL(NAME,'null'),',',NVL(ROLL_NO,'null'))
FROM DUAL
Expected result-
1,john,123
2,josh,null
I intend to run this query on a spark temporary table. But before that I tried running it on sql developer.
But I'm getting ORA-00909 : invalid number of arguments error. I can't find where I'm going wrong here.
Extra question (not necessary to answer) : Is there a way to concatenate all columns by not writing columns manually? I know there is a function concat_ws in spark.sql and oracle, where we can use a delimiter but it also neglects null value instead of replacing them with 'null' string but again I've to write all columns manually even in concat_ws.
Oracle's CONCAT function accepts only 2 arguments.
But there is the concat operator, ||, which does what you want:
SELECT NVL(ID,'null') || ',' || NVL(NAME,'null') || ',' || NVL(ROLL_NO,'null')
FROM DUAL
This question already has answers here:
How to get the 1st value before delimiter in sql server
(4 answers)
How to split string with delimiter and get the first value
(1 answer)
Split string on only first occurance of character/delimiter
(4 answers)
Split the string. get first value of split SQL Server 2005
(1 answer)
Closed 10 months ago.
I have value like
b10F|b3432R|b2134D
I need to get only the first value before the pipe:
b10F
How can I do that?
use mysql query with SUBSTRING_INDEX
SELECT SUBSTRING_INDEX("b10F|b3432R|b2134D", "|", 1)
Maybe use this query which uses CHARINDEX function to find the location of first pipe character and then uses LEFT to get data till that position -1
SELECT
LEFT(
'b10F|b3432R|b2134D',
CHARINDEX('|', 'b10F|b3432R|b2134D' ) -1
)AS Output;
This question already has answers here:
Remove first characters of string in Oracle Server
(3 answers)
Closed 1 year ago.
I have table in Oracle SQL Developer as below:
col1
-------
RT|12313
RT|ERT66
EG|daHH
And I would like to take from table above from "col1" only values after "|", so I would like to ignore 3 first characters in "col1" and take rest.
How can I do that in Oracle SQL Developer?
As a result I need:
col1
-----
12313
ERT66
daHH
You can use substr():
select substr(col1, 4)
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
I have to put a string after every 5 characters in a given string (varchar2).
Given string can have different length.
I already solved it by a loop using substrings.
Is there any way i could reach the goal using REGEXP in Oracle DB?
You can use REGEXP_REPLACE to replace every 5 characters with those 5 characters followed by another string. For example:
SELECT REGEXP_REPLACE('ABCDE12345FGHIJ67890KL', '(.{5})', '\1*') FROM DUAL
Output:
ABCDE*12345*FGHIJ*67890*KL
Demo on dbfiddle