select only required text in oracle without hardcoding the character position - oracle

I have a column in database which stores data like follows
{0}="[DYD666020115982-ZO]",{1}="SomeText"
I want to get the 0th value ie DYD666020115982-ZO printed in output.
I have tried SUBSTR({0}="[DYD666020115982-ZO]",{1}="SomeText",7,18) which gives me output, but I wanted to know is there any other way without hard coding the position.

The oldfashioned substr + instr combination works good, works fast (res_1).
Regexp (res_2) along with trims (because of parenthesis) is another option:
SQL> with test (col) as
2 (select '{0}="[DYD666020115982-ZO]",{1}="SomeText"' from dual)
3 select substr(col, instr(col, '[') + 1,
4 instr(col, ']') - instr(col, '[') - 1
5 ) res_1,
6 --
7 rtrim(ltrim(regexp_substr(col, '\[.+\]'), '['), ']') res_2
8 from test;
RES_1 RES_2
------------------ ------------------
DYD666020115982-ZO DYD666020115982-ZO
SQL>

You can use REGEXP_SUBSTR to match the pattern (^|,)\{0\}="(([^"]|\\")*?)"(,|$):
Oracle Setup:
CREATE TABLE test_data ( id, list ) AS
SELECT 1, '{0}="[DYD666020115982-ZO]",{1}="SomeText"' FROM DUAL UNION ALL
SELECT 2, '{1}="SomeText with a \"Quote\"",{0}="[DYD666020115982-ZO]"' FROM DUAL UNION ALL
SELECT 3, '{1}="SomeText"' FROM DUAL UNION ALL
SELECT 4, '{0}="[DYD666020115982-ZO-2]",{1}="SomeText",{0}="[DYD666020115982-ZO-1]"' FROM DUAL UNION ALL
SELECT 5, '{1}="{0}=\"[DYD666020115982-ZO]\""' FROM DUAL;
Query:
SELECT REGEXP_SUBSTR( list, '(^|,)\{0\}="(([^"]|\\")*?)"(,|$)', 1, 1, NULL, 2 ) AS value
FROM test_data
Output:
| VALUE |
| :--------------------- |
| [DYD666020115982-ZO] |
| [DYD666020115982-ZO] |
| null |
| [DYD666020115982-ZO-2] |
| null |
or, if you want to string the leading and training characters then:
SELECT SUBSTR( value, 2, LENGTH( value ) - 2 ) AS value
FROM (
SELECT REGEXP_SUBSTR( list, '(^|,)\{0\}="(([^"]|\\")*?)"(,|$)', 1, 1, NULL, 2 ) AS value
FROM test_data
)
which outputs:
| VALUE |
| :------------------- |
| DYD666020115982-ZO |
| DYD666020115982-ZO |
| null |
| DYD666020115982-ZO-2 |
| null |
If you want to get all the values then you can use a recursive sub-query and REGEXP_SUBSTR:
Query:
WITH data ( id, list, key, value, idx, max_idx ) AS (
SELECT id,
list,
TO_NUMBER( REGEXP_SUBSTR( list, '\{(\d+)\}="(([^"]|\\")*?)"(,|$)', 1, 1, NULL, 1 ) ),
REGEXP_SUBSTR( list, '\{(\d+)\}="(([^"]|\\")*?)"(,|$)', 1, 1, NULL, 2 ),
1,
REGEXP_COUNT( list, '\{(\d+)\}="(([^"]|\\")*?)"(,|$)' )
FROM test_data
UNION ALL
SELECT id,
list,
TO_NUMBER( REGEXP_SUBSTR( list, '\{(\d+)\}="(([^"]|\\")*?)"(,|$)', 1, idx + 1, NULL, 1 ) ),
REGEXP_SUBSTR( list, '\{(\d+)\}="(([^"]|\\")*?)"(,|$)', 1, idx + 1, NULL, 2 ),
idx + 1,
max_idx
FROM data
WHERE idx < max_idx
)
SELECT id, key, value
FROM data
WHERE idx <= max_idx
ORDER BY id, key
Output:
ID | KEY | VALUE
-: | --: | :---------------------------
1 | 0 | [DYD666020115982-ZO]
1 | 1 | SomeText
2 | 0 | [DYD666020115982-ZO]
2 | 1 | SomeText with a \"Quote\"
3 | 1 | SomeText
4 | 0 | [DYD666020115982-ZO-2]
4 | 0 | [DYD666020115982-ZO-1]
4 | 1 | SomeText
5 | 1 | {0}=\"[DYD666020115982-ZO]\"
If you just want the values where the key is 0 then add AND key = 0 to the end of the query.
db<>fiddle here

Related

How to merge consecutive column values in Oracle into one and save initial order?

I have the following table.
Table 1
+---------+-----------+
| col_key | col_value |
+---------+-----------+
| key1 | value1 |
| key1 | value2 |
| key1 | value3 |
| key1 | value2 |
| key1 | value2 |
| key1 | value2 |
| key1 | value1 |
| key1 | value1 |
| key1 | value3 |
| key1 | value3 |
| key1 | value3 |
| key1 | value2 |
+---------+-----------+
I want to get the following output:
Table 2
+---------+-------------------------------------------------------------------+
| col_key | col_value |
+---------+-------------------------------------------------------------------+
| key1 | value1 | value2 | value 3 | value 2 | value 1 | value 3 | value 2 |
+---------+-------------------------------------------------------------------+
Algorithm - if consecutive values same - merge them into one.
I am using Oracle and listagg function:
select
col_key,
listagg(distinct col_value, ' | ') within group (order by col_key) as col_value
from
sample_table
group by col_key
order by col_key
But, listagg function with distinct keyword removes duplicates.
So, is it possible to make this in Oracle (like in table 2)?
Oracle versions (12c and 18c)
From Oracle 12, you can use MATCH_RECOGNIZE to perform row-by-row comparisons and aggregate the adjacent duplicates and then you can use LISTAGG to aggregate the unique values:
SELECT col_key,
LISTAGG(value, ' | ') WITHIN GROUP (ORDER BY mno) AS col_value
FROM (SELECT t.*,
ROWNUM AS rn -- You need to provide a way of getting this order!
FROM table1 t)
MATCH_RECOGNIZE(
PARTITION BY col_key
ORDER BY rn
MEASURES
MATCH_NUMBER() AS mno,
FIRST(col_value) AS value
PATTERN (same_value+)
DEFINE
same_value AS FIRST(col_value) = col_value
)
GROUP BY col_key;
Which, for the sample data:
CREATE TABLE Table1 (col_key, col_value) AS
SELECT 'key1', 'value1' FROM DUAL UNION ALL
SELECT 'key1', 'value2' FROM DUAL UNION ALL
SELECT 'key1', 'value3' FROM DUAL UNION ALL
SELECT 'key1', 'value2' FROM DUAL UNION ALL
SELECT 'key1', 'value2' FROM DUAL UNION ALL
SELECT 'key1', 'value2' FROM DUAL UNION ALL
SELECT 'key1', 'value1' FROM DUAL UNION ALL
SELECT 'key1', 'value1' FROM DUAL UNION ALL
SELECT 'key1', 'value3' FROM DUAL UNION ALL
SELECT 'key1', 'value3' FROM DUAL UNION ALL
SELECT 'key1', 'value3' FROM DUAL UNION ALL
SELECT 'key1', 'value2' FROM DUAL;
Outputs:
COL_KEY
COL_VALUE
key1
value1 | value2 | value3 | value2 | value1 | value3 | value2
db<>fiddle here
You said you'll edit the question and provide sorting column; so far, you didn't do that so I used ROWID instead. Change it with your own column (once you find out which one to use).
Read comments within code.
SQL> with
2 temp as
3 -- find COL_VALUE and the value that follows it (NEXT_VALUE in this query.
4 -- As I already said, I used ROWID for sorting purposes)
5 (select col_key,
6 col_value,
7 rowid rid,
8 lead(col_value) over (partition by col_key order by rowid) next_value
9 from sample_table
10 ),
11 temp2 as
12 -- "new" COL_VALUE will be the "original" COL_VALUE if it is different from its
13 -- next value (or - for the last row - if there's no next value)
14 (select col_key,
15 rid,
16 case when col_value <> next_value or next_value is null then col_value
17 else null
18 end col_value
19 from temp
20 )
21 -- finally, aggregate the result
22 select col_key,
23 listagg(col_value, ' | ') within group (order by rid) col_value
24 from temp2
25 group by col_key;
COL_KEY COL_VALUE
---------- ------------------------------------------------------------
1 val1 | val2 | val3 | val2 | val1 | val3 | val2
SQL>
Here's another take on it:
WITH
cte1 AS (SELECT COL_KEY,
COL_VALUE,
LAG(COL_VALUE) OVER (ORDER BY ROWNUM) AS PREV_COL_VALUE
FROM TEST_TAB),
cte2 AS (SELECT COL_KEY,
COL_VALUE,
CASE
WHEN COL_VALUE = PREV_COL_VALUE THEN 0
ELSE 1
END AS FLAG
FROM cte1),
cte3 AS (SELECT COL_KEY,
COL_VALUE
FROM cte2
WHERE FLAG = 1)
SELECT COL_KEY,
LISTAGG(COL_VALUE, ' | ') WITHIN GROUP (ORDER BY COL_KEY, ROWNUM) AS COL_VALUES
FROM cte3
GROUP BY COL_KEY
This produces:
COL_KEY COL_VALUES
------- ------------------------------------------------------------
key1 value1 | value2 | value3 | value2 | value1 | value3 | value2
which matches your desired output, but without an ordering column in your data you're at the mercy of the database and how it chooses to return the data. Remember - a basic rule of relational databases is that tables are unordered collections of rows, and there must an ORDER BY clause to impose an order on those rows when they are returned by a query.
db<>fiddle here

Sets From a Single Table, Grouped By a Column

I have a table:
+-------+-------+----------+
| GROUP | State | Priority |
+-------+-------+----------+
| 1 | MI | 1 |
| 1 | IA | 2 |
| 1 | CA | 3 |
| 1 | ND | 4 |
| 1 | AZ | 5 |
| 2 | IA | 2 |
| 2 | NJ | 1 |
| 2 | NH | 3 |
And so on...
How do I write a query that makes all the sets of the states by group, in priority order? Like so:
+-------+--------------------+
| GROUP | SET |
+-------+--------------------+
| 1 | MI |
| 1 | MI, IA |
| 1 | MI, IA, CA |
| 1 | MI, IA, CA, ND |
| 1 | MI, IA, CA, ND, AZ |
| 2 | NJ |
| 2 | NJ, IA |
| 2 | NJ, IA, NH |
+-------+--------------------+
This is similar to my question here and I've tried to modify that solution but, I'm just a forty watt bulb and it's a sixty watt problem...
This problem actually looks simpler than the answer to the question you linked, which is an excellent solution to that problem. Nevertheless, this uses the same hierarchical queries, with connect by
If it is the case that priority is always a continuous sequence of numbers, this will work
SELECT t.grp, level, ltrim(SYS_CONNECT_BY_PATH(state,','),',') as "set"
from t
start with priority = 1
connect by priority = prior priority + 1
and grp = prior grp
However, if that's not always true, we would require row_number() to define the sequence based on the order of priority ( which need not be consecutive integer)
with t2 AS
(
select t.*, row_number()
over ( partition by grp order by priority) as rn from t
)
SELECT t2.grp, ltrim(SYS_CONNECT_BY_PATH(state,','),',') as "set"
from t2
start with priority = 1
connect by rn = prior rn + 1
and grp = prior grp
DEMO
I realize this has already been answered, but I wanted to see if I could do this using ANSI standard syntax. "connect by" is an Oracle only feature, the following will work on multiple databases:
WITH
-- ASET is just setting up the sample dataset
aset AS
(SELECT 1 AS grp, 'MI' AS state, 1 AS priority FROM DUAL
UNION ALL
SELECT 1 AS grp, 'IA', 2 FROM DUAL
UNION ALL
SELECT 1 AS grp, 'CA', 3 FROM DUAL
UNION ALL
SELECT 1 AS grp, 'ND', 4 FROM DUAL
UNION ALL
SELECT 1 AS grp, 'AZ', 5 FROM DUAL
UNION ALL
SELECT 2 AS grp, 'IA', 2 FROM DUAL
UNION ALL
SELECT 2 AS grp, 'NJ', 1 FROM DUAL
UNION ALL
SELECT 2 AS grp, 'NH', 3 FROM DUAL),
bset AS
-- In BSET we convert the ASET records into comma separated values
( SELECT grp, LISTAGG( state, ',' ) WITHIN GROUP (ORDER BY priority) AS set1
FROM aset
GROUP BY grp),
cset ( grp
, set1
, set2
, pos ) AS
-- CSET breaks our comma separated values up into multiple rows
-- Each row adding the next CSV value
(SELECT grp AS grp
, set1 AS set1
, SUBSTR( set1 || ',', 1, INSTR( set1 || ',', ',' ) - 1 ) AS set2
, 1 AS pos
FROM bset
UNION ALL
SELECT grp AS grp
, set1 AS set1
, SUBSTR( set1 || ','
, 1
, INSTR( set1 || ','
, ','
, 1
, pos + 1 )
- 1 ) AS set2
, pos + 1 AS pos
FROM cset
WHERE INSTR( set1 || ','
, ','
, 1
, pos + 1 ) > 0)
SELECT grp, set2
FROM cset
ORDER BY grp, pos;

Convert delimiter data to rows and columns

i have RAW data in Results column like below with semicolon separated.
Case ID;HPMN;VPMN;IMSI;Case Creation Time;Case Reporting Time;Case Closure Time;Last Call Time;Fraud Type;Reporting Remarks;SDR Value - Created (TAP);SDR Value - Created (NRT);SDR Value - Reported (TAP);SDR Value - Reported (NRT);SDR Value - Closed (TAP);SDR Value - Closed (NRT);SDR Value - Total (TAP);SDR Value - Total (NRT);SDR Value - Fraud Loss (TAP);SDR Value - Fraud Loss (NRT);Case Status ID;Case Status;Analyst Name - Assigned;Analyst Name - Closure;Group;Flag;7014472;NLXLT;ESPAT;204046205787443;17.05.2018 00:00;17.05.2018 05:57;;18.05.2018 06:45:09;High Usage Request ;;10.35;0; 10.35;0;0;0;20.72;0;10.37;0;512;Pending;Gloriana;Lokesh;AllRoamingGroup P1;non-hosted;7014473;OMNNT;ARETC;422030204010256;17.05.2018 00:01;17.05.2018 05:57;;15.05.2018 19:47:09;OMNNT 10MB & 50 SDRs;;51.35 ;0;51.35;0;0;0;51.35 ;0;0;0;512;Pending;Gloriana;Jordan;AllRoamingGroup P1;non-hosted;7014474;NLXLT;CZECM;204046868687429;17.05.2018 00:02;;17.05.2018 00:24;17.05.2018 15:36:23;Call third country;;0;0;0;0;0;0;.44;0;.44;0;2048;Closed Not Fraud;Gloriana;Gloriana;AllRoamingGroup P1;non-hosted;
7014475;DOMCL;PRICL;370021204427525;17.05.2018 00:03;;17.05.2018 00:26;17.05.2018 22:44:19;Call third country;;0;230.41;0;0;0;230.41;1.94;1200.07;1.94;969.66;2048;Closed Not Fraud;Gloriana;Gloriana;AllRoamingGroup P1;non-hosted;
But i need output in this way
I tried regular expression but not able to get the desired output
This will get the first 16 columns.... but you can just add extra columns as needed following the same pattern:
SQL Fiddle
Oracle 11g R2 Schema Setup:
CREATE TABLE table_name ( value ) AS
SELECT 'Case ID;HPMN;VPMN;IMSI;Case Creation Time;Case Reporting Time;Case Closure Time;Last Call Time;Fraud Type;Reporting Remarks;SDR Value - Created (TAP);SDR Value - Created (NRT);SDR Value - Reported (TAP);SDR Value - Reported (NRT);SDR Value - Closed (TAP);SDR Value - Closed (NRT);SDR Value - Total (TAP);SDR Value - Total (NRT);SDR Value - Fraud Loss (TAP);SDR Value - Fraud Loss (NRT);Case Status ID;Case Status;Analyst Name - Assigned;Analyst Name - Closure;Group;Flag;7014472;NLXLT;ESPAT;204046205787443;17.05.2018 00:00;17.05.2018 05:57;;18.05.2018 06:45:09;High Usage Request ;;10.35;0; 10.35;0;0;0;20.72;0;10.37;0;512;Pending;Gloriana;Lokesh;AllRoamingGroup P1;non-hosted;7014473;OMNNT;ARETC;422030204010256;17.05.2018 00:01;17.05.2018 05:57;;15.05.2018 19:47:09;OMNNT 10MB & 50 SDRs;;51.35 ;0;51.35;0;0;0;51.35 ;0;0;0;512;Pending;Gloriana;Jordan;AllRoamingGroup P1;non-hosted;7014474;NLXLT;CZECM;204046868687429;17.05.2018 00:02;;17.05.2018 00:24;17.05.2018 15:36:23;Call third country;;0;0;0;0;0;0;.44;0;.44;0;2048;Closed Not Fraud;Gloriana;Gloriana;AllRoamingGroup P1;non-hosted; 7014475;DOMCL;PRICL;370021204427525;17.05.2018 00:03;;17.05.2018 00:26;17.05.2018 22:44:19;Call third country;;0;230.41;0;0;0;230.41;1.94;1200.07;1.94;969.66;2048;Closed Not Fraud;Gloriana;Gloriana;AllRoamingGroup P1;non-hosted;' FROM DUAL;
Query 1:
WITH data (
value,
lvl,
maxlvl,
Case_ID,
HPMN,
VPMN,
IMSI,
Case_Creation_Time,
Case_Reporting_Time,
Case_Closure_Time,
Last_Call_Time,
Fraud_Type,
Reporting_Remarks,
SDR_Value_Created_TAP,
SDR_Value_Created_NRT,
SDR_Value_Reported_TAP,
SDR_Value_Reported_NRT,
SDR_Value_Closed_TAP,
SDR_Value_Closed_NRT
) AS (
SELECT value,
1,
REGEXP_COUNT( value, '([^;]*);' ) / 26,
REGEXP_SUBSTR( value, '([^;]*);', 1, 0*26 + 1, NULL, 1 ),
REGEXP_SUBSTR( value, '([^;]*);', 1, 0*26 + 2, NULL, 1 ),
REGEXP_SUBSTR( value, '([^;]*);', 1, 0*26 + 3, NULL, 1 ),
REGEXP_SUBSTR( value, '([^;]*);', 1, 0*26 + 4, NULL, 1 ),
REGEXP_SUBSTR( value, '([^;]*);', 1, 0*26 + 5, NULL, 1 ),
REGEXP_SUBSTR( value, '([^;]*);', 1, 0*26 + 6, NULL, 1 ),
REGEXP_SUBSTR( value, '([^;]*);', 1, 0*26 + 7, NULL, 1 ),
REGEXP_SUBSTR( value, '([^;]*);', 1, 0*26 + 8, NULL, 1 ),
REGEXP_SUBSTR( value, '([^;]*);', 1, 0*26 + 9, NULL, 1 ),
REGEXP_SUBSTR( value, '([^;]*);', 1, 0*26 + 10, NULL, 1 ),
REGEXP_SUBSTR( value, '([^;]*);', 1, 0*26 + 11, NULL, 1 ),
REGEXP_SUBSTR( value, '([^;]*);', 1, 0*26 + 12, NULL, 1 ),
REGEXP_SUBSTR( value, '([^;]*);', 1, 0*26 + 13, NULL, 1 ),
REGEXP_SUBSTR( value, '([^;]*);', 1, 0*26 + 14, NULL, 1 ),
REGEXP_SUBSTR( value, '([^;]*);', 1, 0*26 + 15, NULL, 1 ),
REGEXP_SUBSTR( value, '([^;]*);', 1, 0*26 + 16, NULL, 1 )
FROM table_name
UNION ALL
SELECT value,
lvl+1,
maxlvl,
REGEXP_SUBSTR( value, '([^;]*);', 1, lvl*26 + 1, NULL, 1 ),
REGEXP_SUBSTR( value, '([^;]*);', 1, lvl*26 + 2, NULL, 1 ),
REGEXP_SUBSTR( value, '([^;]*);', 1, lvl*26 + 3, NULL, 1 ),
REGEXP_SUBSTR( value, '([^;]*);', 1, lvl*26 + 4, NULL, 1 ),
REGEXP_SUBSTR( value, '([^;]*);', 1, lvl*26 + 5, NULL, 1 ),
REGEXP_SUBSTR( value, '([^;]*);', 1, lvl*26 + 6, NULL, 1 ),
REGEXP_SUBSTR( value, '([^;]*);', 1, lvl*26 + 7, NULL, 1 ),
REGEXP_SUBSTR( value, '([^;]*);', 1, lvl*26 + 8, NULL, 1 ),
REGEXP_SUBSTR( value, '([^;]*);', 1, lvl*26 + 9, NULL, 1 ),
REGEXP_SUBSTR( value, '([^;]*);', 1, lvl*26 + 10, NULL, 1 ),
REGEXP_SUBSTR( value, '([^;]*);', 1, lvl*26 + 11, NULL, 1 ),
REGEXP_SUBSTR( value, '([^;]*);', 1, lvl*26 + 12, NULL, 1 ),
REGEXP_SUBSTR( value, '([^;]*);', 1, lvl*26 + 13, NULL, 1 ),
REGEXP_SUBSTR( value, '([^;]*);', 1, lvl*26 + 14, NULL, 1 ),
REGEXP_SUBSTR( value, '([^;]*);', 1, lvl*26 + 15, NULL, 1 ),
REGEXP_SUBSTR( value, '([^;]*);', 1, lvl*26 + 16, NULL, 1 )
FROM data
WHERE lvl < maxlvl
)
SELECT Case_ID,
HPMN,
VPMN,
IMSI,
Case_Creation_Time,
Case_Reporting_Time,
Case_Closure_Time,
Last_Call_Time,
Fraud_Type,
Reporting_Remarks,
SDR_Value_Created_TAP,
SDR_Value_Created_NRT,
SDR_Value_Reported_TAP,
SDR_Value_Reported_NRT,
SDR_Value_Closed_TAP,
SDR_Value_Closed_NRT
FROM data
WHERE lvl > 1
Results:
| CASE_ID | HPMN | VPMN | IMSI | CASE_CREATION_TIME | CASE_REPORTING_TIME | CASE_CLOSURE_TIME | LAST_CALL_TIME | FRAUD_TYPE | REPORTING_REMARKS | SDR_VALUE_CREATED_TAP | SDR_VALUE_CREATED_NRT | SDR_VALUE_REPORTED_TAP | SDR_VALUE_REPORTED_NRT | SDR_VALUE_CLOSED_TAP | SDR_VALUE_CLOSED_NRT |
|----------|-------|-------|-----------------|--------------------|---------------------|-------------------|---------------------|----------------------|-------------------|-----------------------|-----------------------|------------------------|------------------------|----------------------|----------------------|
| 7014472 | NLXLT | ESPAT | 204046205787443 | 17.05.2018 00:00 | 17.05.2018 05:57 | (null) | 18.05.2018 06:45:09 | High Usage Request | (null) | 10.35 | 0 | 10.35 | 0 | 0 | 0 |
| 7014473 | OMNNT | ARETC | 422030204010256 | 17.05.2018 00:01 | 17.05.2018 05:57 | (null) | 15.05.2018 19:47:09 | OMNNT 10MB & 50 SDRs | (null) | 51.35 | 0 | 51.35 | 0 | 0 | 0 |
| 7014474 | NLXLT | CZECM | 204046868687429 | 17.05.2018 00:02 | (null) | 17.05.2018 00:24 | 17.05.2018 15:36:23 | Call third country | (null) | 0 | 0 | 0 | 0 | 0 | 0 |
| 7014475 | DOMCL | PRICL | 370021204427525 | 17.05.2018 00:03 | (null) | 17.05.2018 00:26 | 17.05.2018 22:44:19 | Call third country | (null) | 0 | 230.41 | 0 | 0 | 0 | 230.41 |
Query 2:
SELECT RTRIM( REGEXP_SUBSTR( value, '.*?;', 1, l.COLUMN_VALUE*26 + 1 ), ';' ) AS Case_ID,
RTRIM( REGEXP_SUBSTR( value, '.*?;', 1, l.COLUMN_VALUE*26 + 2 ), ';' ) AS HPMN,
RTRIM( REGEXP_SUBSTR( value, '.*?;', 1, l.COLUMN_VALUE*26 + 3 ), ';' ) AS VPMN,
RTRIM( REGEXP_SUBSTR( value, '.*?;', 1, l.COLUMN_VALUE*26 + 4 ), ';' ) AS IMSI,
RTRIM( REGEXP_SUBSTR( value, '.*?;', 1, l.COLUMN_VALUE*26 + 5 ), ';' ) AS Case_Creation_Time,
RTRIM( REGEXP_SUBSTR( value, '.*?;', 1, l.COLUMN_VALUE*26 + 6 ), ';' ) AS Case_Reporting_Time,
RTRIM( REGEXP_SUBSTR( value, '.*?;', 1, l.COLUMN_VALUE*26 + 7 ), ';' ) AS Case_Closure_Time,
RTRIM( REGEXP_SUBSTR( value, '.*?;', 1, l.COLUMN_VALUE*26 + 8 ), ';' ) AS Last_Call_Time,
RTRIM( REGEXP_SUBSTR( value, '.*?;', 1, l.COLUMN_VALUE*26 + 9 ), ';' ) AS Fraud_Type,
RTRIM( REGEXP_SUBSTR( value, '.*?;', 1, l.COLUMN_VALUE*26 + 10 ), ';' ) AS Reporting_Remarks,
RTRIM( REGEXP_SUBSTR( value, '.*?;', 1, l.COLUMN_VALUE*26 + 11 ), ';' ) AS SDR_Value_Created_TAP,
RTRIM( REGEXP_SUBSTR( value, '.*?;', 1, l.COLUMN_VALUE*26 + 12 ), ';' ) AS SDR_Value_Created_NRT,
RTRIM( REGEXP_SUBSTR( value, '.*?;', 1, l.COLUMN_VALUE*26 + 13 ), ';' ) AS SDR_Value_Reported_TAP,
RTRIM( REGEXP_SUBSTR( value, '.*?;', 1, l.COLUMN_VALUE*26 + 14 ), ';' ) AS SDR_Value_Reported_NRT,
RTRIM( REGEXP_SUBSTR( value, '.*?;', 1, l.COLUMN_VALUE*26 + 15 ), ';' ) AS SDR_Value_Closed_TAP,
RTRIM( REGEXP_SUBSTR( value, '.*?;', 1, l.COLUMN_VALUE*26 + 16 ), ';' ) AS SDR_Value_Closed_NRT
FROM table_name t
CROSS JOIN
TABLE(
CAST(
MULTISET(
SELECT LEVEL
FROM DUAL
CONNECT BY REGEXP_SUBSTR( t.value, '.*?;', 1, LEVEL*26 + 1 ) IS NOT NULL
)
AS SYS.ODCINUMBERLIST
)
) l
Results:
| CASE_ID | HPMN | VPMN | IMSI | CASE_CREATION_TIME | CASE_REPORTING_TIME | CASE_CLOSURE_TIME | LAST_CALL_TIME | FRAUD_TYPE | REPORTING_REMARKS | SDR_VALUE_CREATED_TAP | SDR_VALUE_CREATED_NRT | SDR_VALUE_REPORTED_TAP | SDR_VALUE_REPORTED_NRT | SDR_VALUE_CLOSED_TAP | SDR_VALUE_CLOSED_NRT |
|----------|-------|-------|-----------------|--------------------|---------------------|-------------------|---------------------|----------------------|-------------------|-----------------------|-----------------------|------------------------|------------------------|----------------------|----------------------|
| 7014472 | NLXLT | ESPAT | 204046205787443 | 17.05.2018 00:00 | 17.05.2018 05:57 | (null) | 18.05.2018 06:45:09 | High Usage Request | (null) | 10.35 | 0 | 10.35 | 0 | 0 | 0 |
| 7014473 | OMNNT | ARETC | 422030204010256 | 17.05.2018 00:01 | 17.05.2018 05:57 | (null) | 15.05.2018 19:47:09 | OMNNT 10MB & 50 SDRs | (null) | 51.35 | 0 | 51.35 | 0 | 0 | 0 |
| 7014474 | NLXLT | CZECM | 204046868687429 | 17.05.2018 00:02 | (null) | 17.05.2018 00:24 | 17.05.2018 15:36:23 | Call third country | (null) | 0 | 0 | 0 | 0 | 0 | 0 |
| 7014475 | DOMCL | PRICL | 370021204427525 | 17.05.2018 00:03 | (null) | 17.05.2018 00:26 | 17.05.2018 22:44:19 | Call third country | (null) | 0 | 230.41 | 0 | 0 | 0 | 230.41 |

xsd format number , 5 decimal places

I have xsd code which is printing LINE_AMOUNT value as 14,952.59 , now i want to display this as 14,952.59000(5 decimal places).
How to achieve this?
Thank you
SQL Fiddle
Query 1 Either (if you want to use the current NLS values for decimal and thousands characters):
SELECT TO_CHAR(
14952.59,
'FM9G999G999G999G990D00000'
)
FROM DUAL
Results:
| TO_CHAR(14952.59,'FM9G999G999G999G990D00000') |
|-----------------------------------------------|
| 14,952.59000 |
Query 2 or:
SELECT TO_CHAR(
14952.59,
'FM9,999,999,999,990.00000'
)
FROM DUAL
Results:
| TO_CHAR(14952.59,'FM9,999,999,999,990.00000') |
|-----------------------------------------------|
| 14,952.59000 |
Update: SQL Fiddle
Query 3:
SELECT TO_CHAR(
TO_NUMBER(
'13,214,952.59',
'FM9G999G999G999G990D99999'
),
'FM9G999G999G999G990D00000'
) AS formatted_value
FROM DUAL
Results:
| FORMATTED_VALUE |
|------------------|
| 13,214,952.59000 |
Query 4:
SELECT TO_CHAR(
TO_NUMBER(
'13,214,952.59',
'FM9,999,999,999,990.99999'
),
'FM9,999,999,999,990.00000'
) AS formatted_value
FROM DUAL
Results:
| FORMATTED_VALUE |
|------------------|
| 13,214,952.59000 |
Try this, it will work for you I think.
select trim(to_char(14952.59,9999999999.99999)) from dual
OUTPUT
14952.59000

oracle - querying NULL values in unpivot query

I want to fetch records from oracle DB where column value is NULL. Also i am using unpivot in my query. Somehow NULL values are not getting selected because of unpivot keyword. Can you please help me about how to get rows for the same when using unpivot.
EDIT:
SELECT a.emp_id, a.emp_dept, b.emp_location
FROM employee a,
location b UNPIVOT (emp_id
FOR dummy_id
IN (emp_id AS 'EMP_ID', last_date AS 'LAST_DATE'))
WHERE emp_id = 123 AND b.emp_loc_id = 'india' AND b.location IS NULL;
Use UNPIVOT INCLUDE NULLS:
SQL Fiddle
Oracle 11g R2 Schema Setup:
CREATE TABLE test ( id, a, b, c, d ) AS
SELECT 1, 1, 2, 3, 4 FROM DUAL UNION ALL
SELECT 2, 1, NULL, 3, NULL FROM DUAL;
Query 1:
SELECT *
FROM test
UNPIVOT INCLUDE NULLS ( value FOR name IN ( a, b, c, d ) )
Results:
| ID | NAME | VALUE |
|----|------|--------|
| 1 | A | 1 |
| 1 | B | 2 |
| 1 | C | 3 |
| 1 | D | 4 |
| 2 | A | 1 |
| 2 | B | (null) |
| 2 | C | 3 |
| 2 | D | (null) |

Resources