Home » SQL & PL/SQL » SQL & PL/SQL » how to retreive the second word using substring
how to retreive the second word using substring [message #635735] |
Tue, 07 April 2015 16:00 |
|
Info_dev
Messages: 1 Registered: April 2015 Location: VIRGINIA
|
Junior Member |
|
|
Can you please let me know how to do this
JOB_TITLE_1 will have the first word of the JOB_TITLE.
(Example For the record with JOB_ID="AD_VP" this value will be "Administration").
JOB_TITLE_2 will have the second word of the JOB_TITLE. If the value is not there, then populate a hardcoded value "NOT-AVAILABLE".
(Example For the record with JOB_ID=AD_VP this value will be "Vice", whereas the record with JOB_ID=AD_PRES, the value will be "NOT-AVAILABLE").
JOB_TITLE_3 This field will have the third word of the JOB_TITLE. If the value is not there, then populate a hardcoded value "NOT-AVAILABLE".
|
|
|
|
Re: how to retreive the second word using substring [message #635737 is a reply to message #635735] |
Tue, 07 April 2015 21:03 |
|
Barbara Boehmer
Messages: 9088 Registered: November 2002 Location: California, USA
|
Senior Member |
|
|
In response to your title question,
"how to retreive the second word using substring"
where words are apparently delimited by underscores:
SCOTT@orcl12c> SELECT SUBSTR
2 ('AD_VP',
3 INSTR ('AD_VP' || '_', '_', 1, 1) + 1,
4 (INSTR ('AD_VP' || '_', '_', 1, 2)
5 - INSTR ('AD_VP' || '_', '_', 1, 1)) + 1)
6 FROM DUAL
7 /
SU
--
VP
1 row selected.
As to the rest, your requirements are not clear. I am guessing that you have some lookup table containing abbreviations and full names to compare to. The following simulates that using subquery factoring clauses for demonstration purposes. This just produces a select.
SCOTT@orcl12c> WITH
2 sample_data AS
3 (SELECT 'AD_VP' AS job_id FROM DUAL UNION ALL
4 SELECT 'AD_PRES' AS job_id FROM DUAL),
5 lookup_data AS
6 (SELECT 'AD' AS abbrev, 'Administration' AS name FROM DUAL UNION ALL
7 SELECT 'VP' AS abbrev, 'Vice' AS name FROM DUAL)
8 SELECT job_id,
9 NVL (l1.name, 'NOT-AVAILABLE') AS job_title_1,
10 NVL (l2.name, 'NOT-AVAILABLE') AS job_title_2,
11 NVL (l3.name, 'NOT-AVAILABLE') AS job_title_3
12 FROM (SELECT job_id,
13 SUBSTR
14 (job_id,
15 1,
16 INSTR (job_id || '_', '_', 1, 1) - 1) AS job_title_1,
17 SUBSTR
18 (job_id,
19 INSTR (job_id || '_', '_', 1, 1) + 1,
20 (INSTR (job_id || '_', '_', 1, 2)
21 - INSTR (job_id || '_', '_', 1, 1)) + 1) AS job_title_2,
22 SUBSTR
23 (job_id,
24 INSTR (job_id || '_', '_', 1, 2) + 1,
25 (INSTR (job_id || '_', '_', 1, 3)
26 - INSTR (job_id || '_', '_', 1, 2)) + 1) AS job_title_3
27 FROM sample_data) s,
28 lookup_data l1,
29 lookup_data l2,
30 lookup_data l3
31 WHERE s.job_title_1 = l1.abbrev (+)
32 AND s.job_title_2 = l2.abbrev (+)
33 AND s.job_title_3 = l3.abbrev (+)
34 /
JOB_ID JOB_TITLE_1 JOB_TITLE_2 JOB_TITLE_3
------- -------------- -------------- --------------
AD_PRES Administration NOT-AVAILABLE NOT-AVAILABLE
AD_VP Administration Vice NOT-AVAILABLE
2 rows selected.
If you are trying to automatically populate certain columns in a table when data is inserted in another column, then you are either looking for an update statement or a trigger.
|
|
|
Re: how to retreive the second word using substring [message #635743 is a reply to message #635735] |
Wed, 08 April 2015 01:36 |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
Info_dev wrote on Wed, 08 April 2015 02:30
JOB_TITLE_2 will have the second word of the JOB_TITLE. If the value is not there, then populate a hardcoded value "NOT-AVAILABLE".
(Example For the record with JOB_ID=AD_VP this value will be "Vice", whereas the record with JOB_ID=AD_PRES, the value will be "NOT-AVAILABLE").
It is unclear what you are saying. If JOB_ID=AD_VP, then the extracted value should be transformed from VP to "Vice". Ok, fine. But, why not the same logic for AD_PRES? Why this would be NOT-AVAILABLE? What if it is AD_XYZ? What would be the RULE for it?
|
|
|
Goto Forum:
Current Time: Fri Apr 19 23:50:13 CDT 2024
|