extract data sql - sql

Example: Below is the string(sentence) in field and I want to extract the specific data from the below patterns using select query in different fields:
i )
Input :
/a03/infor/current/server/infa_sh/ScriptFil/infa_common/adap_main 'FI_RE_PRJ' 'wf_RE_ACC_TIE_HIS_MNT_EN_AM'
Output :
Select query to fetch FI_RE_PRJ and wf_RE_ACC_TIE_HIS_MNT_EN_AM
Other pattern I have is :
Input :
$SCRIPTS/run_in.ksh -f FI_FLE_PRJ -wait wf_FI_SV_CNCL_RP_BAS_KF
Output :
Select query to fetch FI_FLE_PRJ and wf_FI_SV_CNCL_RP_BAS_KF from input

We can try using REGEXP_SUBSTR with a capture group:
SELECT
REGEXP_SUBSTR(col, '''(.*?)''', 1, 1, NULL, 1) AS first,
REGEXP_SUBSTR(col, '''(.*?)''', 1, 2, NULL, 1) AS second
FROM yourTable;

You can use simple string functions (which are much faster than regular expressions):
SELECT SUBSTR( value, pos1 + 1, pos2 - pos1 - 1 ) AS quote1,
SUBSTR( value, pos3 + 1, pos4 - pos3 - 1 ) AS quote2
FROM (
SELECT value,
INSTR(value, '''', 1, 1) AS pos1,
INSTR(value, '''', 1, 2) AS pos2,
INSTR(value, '''', 1, 3) AS pos3,
INSTR(value, '''', 1, 4) AS pos4
FROM table_name
)
Which, for the sample data:
CREATE TABLE table_name (value) AS
SELECT '/a03/infor/current/server/infa_sh/ScriptFil/infa_common/adap_main ''FI_RE_PRJ'' ''wf_RE_ACC_TIE_HIS_MNT_EN_AM''' FROM DUAL;
Outputs:
QUOTE1
QUOTE2
FI_RE_PRJ
wf_RE_ACC_TIE_HIS_MNT_EN_AM
fiddle

Related

I need to Update my row with english version

I have a column which includes rows like this : MER.Fiyatlandırma Müdür Yardımcısı.
SELECT name, SUBSTR(
name,
INSTR(name, '.', 1, 1),
INSTR(name, '.', 1, 2) + 1 - INSTR(name, '.', 1, 1)
) AS deneme
FROM HR_ALL_POSITIONS_F;
I used this code and my rows looks like .Fiyatlandırma Müdür Yardımcısı.
Also ı have an excel file which includes .Fiyatlandırma Müdür Yardımcısı. this row and english version.
I need to change .Fiyatlandırma Müdür Yardımcısı. this to Price Vice Manager.
How can ı do that.
UPDATE denememusa123
SET denememusa123.eklenecekkolon = (SELECT ENGLISHPOSITION FROM pozisyontanimlama)
WHERE (SELECT SUBSTR (name,
INSTR (name,
'.',
1,
1),
INSTR (name,
'.',
1,
2)
+ 1
- INSTR (name,
'.',
1,
1))
FROM HR_ALL_POSITIONS_F) = (Select TURKISHPOSITION FROM pozisyontanimlama);
(I tired this but it is not working.
ora-01427:single-row subquery returns more than one row )
Help Please.
You can use a MERGE statement:
MERGE INTO denememusa123 dst
USING (
WITH split_denememusa123 (rid, prefix, position, suffix) AS (
SELECT rowid AS rid,
SUBSTR(
eklenecekkolon,
1,
INSTR(eklenecekkolon, '.', 1, 1)
) AS prefix,
SUBSTR(
eklenecekkolon,
INSTR(eklenecekkolon, '.', 1, 1) + 1,
INSTR(eklenecekkolon, '.', 1, 2) - INSTR(eklenecekkolon, '.', 1, 1) - 1
) AS position,
SUBSTR(
eklenecekkolon,
INSTR(eklenecekkolon, '.', 1, 2)
) AS suffix
FROM denememusa123
WHERE INSTR(eklenecekkolon, '.', 1, 2) > 0
)
SELECT s.rid, s.prefix || p.englishposition || s.suffix AS position
FROM split_denememusa123 s
INNER JOIN pozisyontanimlama p
ON (p.turkishposition = s.position)
) src
ON (src.rid = dst.ROWID)
WHEN MATCHED THEN
UPDATE SET dst.eklenecekkolon = src.position;
Which, for the sample data:
CREATE TABLE denememusa123 (eklenecekkolon) AS
SELECT 'MER.Fiyatlandırma Müdür Yardımcısı.XYZ' FROM DUAL;
CREATE TABLE pozisyontanimlama (turkishposition, englishposition) AS
SELECT 'Fiyatlandırma Müdür Yardımcısı', 'Price Vice Manager' FROM DUAL
Then after the MERGE the table contains:
EKLENECEKKOLON
MER.Price Vice Manager.XYZ
fiddle

how can I get the specified content by the string

QUESTION DESCRIPTION:
My oracle query SQL result have a column, the column value is a string description like as follow.
How can I get the special substring that I want, such as I only want the pilot's name.
{"flightRiskValue":{"airportDifficulty":0.00,"airportExp":0.00,"airportExp_captain":0.00,"airportTechNos":0.20,"airportTechNos_captain":0.40,"flightCrewNum":0.00,"flightExp":0.00,"flightExp_captain":0.00,"groupExp":0.70,"ilsWorkStatus":0.00,"pilotCrewLimit":0.00,"pilotCrewLimit_captain":0.00,"pilotDutyLevel":0.05,"pilotDutyLevel_captain":0.05,"pilotFindAnyFemale":0.00,"pilotScheduleTechLevel":0.00,"pilotScheduleTechLevel_captain":0.00,"pilotWorkTimeMonth":0.00,"pilotWorkTimeMonth_captain":0.00,"pilotWorkTimeWeek":0.15,"pilotWorkTimeWeek_captain":0.10,"pilotsIsDeptTeam":0.00,"rain":0.00,"rainAndSnow":0.00,"seeing":0.00,"snow":0.00,"wind":0.00},"pilotDTOList":["副驾驶:5-2|杜佳佳|F3;","副驾驶:5-1|凯尔|F4;","第二机长/巡航机长:5-1|贺云|A1;","机长:5-2|张磊3|A1;"],"riskValue":1.95,"version":"1"}
EXPECTED:
How can I get the special substring such as follow I want substring:
|杜佳佳|,|凯尔|,|贺云|,|张三|
and I want a column become more columns according to this column
first pilot name
second pilot name
....
杜佳佳
凯尔
贺云...
You can extract each of the items in the JSON array directly into different columns (eliminating the need to PIVOT) and then find the substrings (without needing to use [slow] regular expressions):
SELECT SUBSTR(
pilot1,
INSTR(pilot1, '|', 1, 1) + 1,
INSTR(pilot1, '|', 1, 2) - INSTR(pilot1, '|', 1, 1) - 1
) AS first_pilot_name,
SUBSTR(
pilot2,
INSTR(pilot2, '|', 1, 1) + 1,
INSTR(pilot2, '|', 1, 2) - INSTR(pilot2, '|', 1, 1) - 1
) AS second_pilot_name,
SUBSTR(
pilot3,
INSTR(pilot3, '|', 1, 1) + 1,
INSTR(pilot3, '|', 1, 2) - INSTR(pilot3, '|', 1, 1) - 1
) AS third_pilot_name,
SUBSTR(
pilot4,
INSTR(pilot4, '|', 1, 1) + 1,
INSTR(pilot4, '|', 1, 2) - INSTR(pilot4, '|', 1, 1) - 1
) AS fourth_pilot_name
FROM table_name t
CROSS APPLY JSON_TABLE(
t.value,
'$'
COLUMNS (
pilot1 NVARCHAR2(200) PATH '$.pilotDTOList[0]',
pilot2 NVARCHAR2(200) PATH '$.pilotDTOList[1]',
pilot3 NVARCHAR2(200) PATH '$.pilotDTOList[2]',
pilot4 NVARCHAR2(200) PATH '$.pilotDTOList[3]'
)
) j
Which, for the sample data, outputs:
FIRST_PILOT_NAME
SECOND_PILOT_NAME
THIRD_PILOT_NAME
FOURTH_PILOT_NAME
杜佳佳
凯尔
贺云
张磊3
db<>fiddlle here

How do I get substring after a character when the occurance of the character keeps changing

Example
123\.456.578.910.ABC
123\.456.578.910
Expected result
123\.456.578
123\.456.578
For the both the inputs I should get only the first 3
I tried the regexp and substring and instr but I’m not getting the results
We can use REGEXP_SUBSTR here with a capture group:
SELECT REGEXP_SUBSTR(col, '^(\d+(\.\d+)*)', 1, 1, NULL, 1)
FROM yourTable;
Demo
Traditional, substr + instr combination is another option:
Sample data:
SQL> with test (col) as
2 (select '123\.456.578.910.ABC' from dual union all
3 select '123\.456.578.910' from dual
4 )
Query begins here:
5 select col,
6 substr(col, 1, instr(col, '.', 1, 3) - 1) result
7 from test;
COL RESULT
-------------------- --------------------
123\.456.578.910.ABC 123\.456.578
123\.456.578.910 123\.456.578
SQL>
If you value will always have at least 3 . characters then you can use:
SELECT value,
SUBSTR(value, 1, INSTR(value, '.', 1, 3) - 1) AS expected
FROM table_name;
If it may have fewer and you want the entire string in those cases then:
SELECT value,
CASE INSTR(value, '.', 1, 3)
WHEN 0
THEN value
ELSE SUBSTR(value, 1, INSTR(value, '.', 1, 3) - 1)
END AS expected
FROM table_name;
Which, for your sample data:
CREATE TABLE table_name (value) AS
SELECT '123\.456.578.910.ABC' FROM DUAL UNION ALL
SELECT '123\.456.578.910' FROM DUAL;
Both outputs:
VALUE
EXPECTED
123.456.578.910.ABC
123.456.578
123.456.578.910
123.456.578
db<>fiddle here

REGEXP_SUBSTR SPLIT Function

i want to split this into 2019/GA/0000104
select REGEXP_SUBSTR('2019/0000015,2019/GA/0000104,2cdb376e-2966-4f24-9063-f4c6f31a6f35', '[^,]+')
from dual;
Output = 2019/GA/0000104
can u guys help?
It seems that you want to extract the second substring. If that's so, then you could use
regexp_substr (result), or
substr + inenter code herestr combination (result2)
SQL> with test (col) as
2 (select '2019/0000015,2019/GA/0000104,2cdb376e-2966-4f24-9063-f4c6f31a6f35' from dual)
3 select regexp_substr(col, '[^,]+', 1, 2) result,
4 --
5 substr(col, instr(col, ',', 1, 1) + 1,
6 instr(col, ',', 1, 2) - instr(col, ',', 1, 1) - 1
7 ) result2
8 from test;
RESULT RESULT2
--------------- ---------------
2019/GA/0000104 2019/GA/0000104
SQL>
Try using REGEXP_SUBSTR with a capture group:
SELECT
REGEXP_SUBSTR(input, ',(.*),', 1, 1, NULL, 1)
FROM yourTable;
Demo
This form of the regex returns the second occurrence of a string of characters that are followed by a comma or the end of the line. It returns the correct element if the first one should ever be NULL.
with tbl(str) as (
select '2019/0000015,2019/GA/0000104,2cdb376e-2966-4f24-9063-f4c6f31a6f35' from dual
)
select regexp_substr(str, '(.*?)(,|$)', 1, 2, NULL, 1)
from tbl;

Substring between characters in Oracle 9i

In later versions I can use this regexp_substr:
SELECT
ID,
regexp_substr(ID, '[^.]+', 1, 2) DATA 1,
regexp_substr(ID, '[^.]+', 1, 3) DATA 2
FROM employees
Table: Employees
ID
--------------------------
2017.1.3001-ABC.01.01
2017.2.3002-ABCD.02.02
2017.303.3003-ABC.03.03
2017.404.3004-ABCD.04.04
Expected output:
ID DATA 1 DATA 2
------------------------ ------ ---------
2017.1.3001-ABC.01.01 1 3001-ABC
2017.2.3002-ABCD.02.02 2 3002-ABCD
2017.303.3003-ABC.03.03 303 3003-ABC
2017.404.3004-ABCD.04.04 404 3004-ABCD
Please help me to get the sub-string between . characters in ID column in SQL Oracle 9i.
You don't need regular expressions - just use SUBSTR and INSTR:
SELECT id,
SUBSTR( id, dot1 + 1, dot2 - dot1 - 1) AS data1,
SUBSTR( id, dot2 + 1, dot3 - dot2 - 1) AS data2
FROM (
SELECT id,
INSTR( id, '.', 1, 1 ) AS dot1,
INSTR( id, '.', 1, 2 ) AS dot2,
INSTR( id, '.', 1, 3 ) AS dot3
FROM employees
);