how can I get the specified content by the string - sql

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

Related

extract data 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

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

In code_subject there are values in the following formats string1#string2#integer#string3. How to extract integer?

In the table code_subject there is a row with a following format string1#string2#integer#string3. How to extract a number (integer) from it?
IS IT LIKE THAT?:
SELECT REGEXP_SUBSTR('integer') "REGEXPR_SUBSTR" FROM code_subject
One way could be using regexp_replace:
select regexp_replace('stringA#stringB#1234#stringC', '[^0-9]', '')
from dual;
Another possibility is:
select substr('string1#string2#1234#string3', instr('string1#string2#1234#string3', '#', 1, 2) + 1,
instr('string1#string2#1234#string3', '#', 1, 3) - instr('string1#string2#1234#string3', '#', 1, 2) - 1)
from dual;

Convert multiple row from one row based on specific character

I am trying to convert from one row to multiple row based on specific character
I copied a script from someone which was working with his/her example but not working for me which is as below
;WITH tmp(NOTEINDX, DataItem, TXTFIELD) AS
(
SELECT
NOTEINDX,
LEFT(TXTFIELD, CHARINDEX('#', TXTFIELD + '#') - 1),
STUFF(TXTFIELD, 1, CHARINDEX('#', TXTFIELD + '#'), '')
FROM SY03900
UNION all
SELECT
NOTEINDX,
LEFT(TXTFIELD, CHARINDEX('#', TXTFIELD + '#') - 1),
STUFF(TXTFIELD, 1, CHARINDEX(',', TXTFIELD + '#'), '')
FROM tmp
WHERE
TXTFIELD > ''
)
SELECT
NOTEINDX,
DataItem
FROM tmp
ORDER BY NOTEINDX
However I am getting below error
Msg 402, Level 16, State 1, Line 6
The data types text and varchar are incompatible in the add operator.
I'm using SQL Server 2008
Thanks,
Hatim

extract the value between two parameters oracle

I am trying to extract the string 01-DEC-17 from '123|01-DEC-17|-123'. But I am not getting desired output:
SELECT SUBSTR('123|01-DEC-17|-123', INSTR('123|01-DEC-17|-123','|', 1)+1, INSTR('123|01-DEC-17|-123', '|',-1)+1)
FROM DUAL;
Output is coming as 01-DEC-17|-123. but the desired output is 01-DEC-17. Any help would be appreciated
Use regexp_substr(). Here is one method:
select trim(both '|' from regexp_substr(col, '|[^|]+|'))
Or, if you know that the positions are fixed, you can just use substr():
select substr(col, 5, 9)
Try this:
with t (col)
as (
select '123|01-DEC-17|-123'
from dual
)
select substr(
col,
instr(col, '|') + 1,
instr(col, '|', 1, 2) - instr(col, '|') - 1
)
from t;
instr(col, '|') - Location of first occurrence of |
instr(col, '|', 1, 2) - Location of second occurrence of |
For substr(column, start_position, length), We need length of subtring as third parameter to substr, so,
instr(col, '|', 1, 2) - instr(col, '|') - 1 will give us the required length.