PL Sql Parsing # Character - sql

I have a records in table which contains only one column,and its column contains data like under string.
D#1001111068#112B#0010040130022013012111505444##20130121110800#20130121115054#01#-##240#username#username#20130124171831#20130130#6
Between # chracters every string show one column in other table like that.
D#
1001111068# --Customer Number
112B# --Procut Id
0010040130022013012111505444# --Serial Number
# --Order Number(empty record)
20130121110800# --X Columns
I want to parse these items and insert to other table.
How to write this.

You can extract n-th substring by the following expression:
regexp_substr(source_string, '([^#]*)(#|$)', 1, n, '', 1)
Full query:
create table your_table(
source_string varchar2(4000)
);
insert into your_table
values('D#1001111068#112B#0010040130022013012111505444##20130121110800#20130121115054#01#-##240#username#username#20130124171831#20130130#6');
select
n,
regexp_substr(source_string, '([^#]*)(#|$)', 1, n, '', 1)
from your_table,
(
select level as n from dual
connect by level <= (
select max(regexp_count(source_string, '#')) + 1
from your_table
)
)
where n <= regexp_count(source_string, '#') + 1;
Output:
1 D
2 1001111068
3 112B
4 0010040130022013012111505444
5 (null)
6 20130121110800
7 20130121115054
8 01
9 -
10 (null)
11 240
12 username
13 username
14 20130124171831
15 20130130
16 6
fiddle

The function you are looking for is regexp_substr.

Related

find all comma delimited values from a varchar2 datatype column

I am not sure whether this is possible or not to find all the comma delimited values from a varchar2 datatype column.
id test
----------------------
1 1
2 3,10,15,20
3 2
4 3,15
If my table is as above then i want to return only row 2 and 4 as below i.e. result of query should look like below:
rownum test
----------------------
1 3,10,15,20
2 3,15
find all the comma delimited values from a number datatype column.
It is impossible to store comma-delimited values in a NUMBER data type column. It has to be character type.
Simply use INSTR in the filter predicate:
SELECT ID, TEST FROM your_table WHERE instr(TEST, ',') > 0;
For example,
SQL> WITH DATA AS
2 ( SELECT 1 ID, '1' TEST FROM dual
3 UNION ALL
4 SELECT 2 , '3,10,15,20' FROM dual
5 UNION ALL
6 SELECT 3 , '2' FROM dual
7 UNION ALL
8 SELECT 4 , '3,15' FROM dual
9 )
10 SELECT ID, TEST FROM DATA WHERE instr(TEST, ',') > 0;
ID TEST
---------- ----------
2 3,10,15,20
4 3,15
INSTR(test, ',') looks for the occurrence of comma , in the delimited-string. > 0 will filter those rows.
NOTE : The WITH clause is only to build the sample data for demonstration.
You can find it by using REGEXP function. A regular expression is a powerful way of specifying a pattern for a complex search.
So query to get result specific to is as :
select id, test from Table_name where test REGEXP '[,]'
For more details regarding REGEX : Go to dev.Mysql

SQL Oracle, next free number from numeric column data

I have a table with an numeric column. There are data records, lets take for Example { 1,7, 10, 11, 12, 19, 20}. I want to use SQL to get the next "free" number from a specific x:
>8 for x=7
>8 for x=8
>13 for x=10
>21 for x=20
Does anybody have an idea?
OK: I want to insert something with an 'x'. The column is unique, so I cannot put something with x=7 in the table when there already is a '7' in there. So I want a routine that returns me '8' if there is already a record with '7'. Or '9' if there already is an '8'.
IN Pseudo SQL:
x = 7 // for example
select COL from myTable where COL= (x or if x does not exist, the y : y > x, y - x smallest possible)
SQL Fiddle
Oracle 11g R2 Schema Setup:
CREATE TABLE NUMBERS
("NUM" int)
;
INSERT ALL
INTO NUMBERS ("NUM")
VALUES (1)
INTO NUMBERS ("NUM")
VALUES (7)
INTO NUMBERS ("NUM")
VALUES (10)
INTO NUMBERS ("NUM")
VALUES (11)
INTO NUMBERS ("NUM")
VALUES (12)
INTO NUMBERS ("NUM")
VALUES (19)
INTO NUMBERS ("NUM")
VALUES (20)
SELECT * FROM dual
;
Query 1:
select min(n.VAL) as NextFree
from (
SELECT LEVEL as VAL
FROM DUAL
CONNECT BY LEVEL <= 100000
ORDER BY LEVEL
) n
left outer join NUMBERS d on n.VAL = d.NUM
where d.NUM is null
and n.VAL >= 10
Results:
| NEXTFREE |
|----------|
| 13 |
Similar to the answer above but there's no need for a left join:
select nvl(nextfree, 10) as nextfree from (
select max(d.NUM) + 1 as nextfree
from NUMBERS d
CONNECT BY PRIOR NUM + 1 = NUM
START WITH NUM = 10
)
| NEXTFREE |
|----------|
| 13 |

How to create increment field in select statement [duplicate]

Is there any way to select the numbers (integers) that are included between two numbers with SQL in Oracle; I don't want to create PL/SQL procedure or function.
For example I need to get the numbers between 3 and 10. The result will be the values 3,4,5,6,7,8,9,10.
Thx.
This trick with Oracle's DUAL table also works:
SQL> select n from
2 ( select rownum n from dual connect by level <= 10)
3 where n >= 3;
N
----------
3
4
5
6
7
8
9
10
The first thing I do when I create a new database is to create and populate some basic tables.
One is a list of all integers between -N and N, another is a list of dates 5 years in the past through 10 years in the future (a scheduled job can continue creating these as needed, going forward) and the last is a list of all hours throughout the day. For example, the inetgers:
create table numbers (n integer primary key);
insert into numbers values (0);
insert into numbers select n+1 from numbers; commit;
insert into numbers select n+2 from numbers; commit;
insert into numbers select n+4 from numbers; commit;
insert into numbers select n+8 from numbers; commit;
insert into numbers select n+16 from numbers; commit;
insert into numbers select n+32 from numbers; commit;
insert into numbers select n+64 from numbers; commit;
insert into numbers select n+128 from numbers; commit;
insert into numbers select n+256 from numbers; commit;
insert into numbers select n+512 from numbers; commit;
insert into numbers select n+1024 from numbers; commit;
insert into numbers select n+2048 from numbers; commit;
insert into numbers select n+4096 from numbers; commit;
insert into numbers select n+8192 from numbers; commit;
insert into numbers select -n from numbers where n > 0; commit;
This is for DB2/z which has automatic transaction start which is why it seems to have naked commits.
Yes, it takes up a (minimal) space but it makes queries much easier to write, simply by selecting values from those tables. It's also very portable across pretty much any SQL-based DBMS.
Your particular query would then be a simple:
select n from numbers where n >=3 and n <= 10;
The hour figures and date ranges are quite useful for the sort of reporting applications we work on. It allows us to create zero entries for those hours of the day (or dates) which don't have any real data so that, instead of (where there's no data on the second of the month):
Date | Quantity
-----------+---------
2009-01-01 | 7
2009-01-03 | 27
2009-01-04 | 6
we can instead get:
Date | Quantity
-----------+---------
2009-01-01 | 7
2009-01-02 | 0
2009-01-03 | 27
2009-01-04 | 6
SQL> var N_BEGIN number
SQL> var N_END number
SQL> exec :N_BEGIN := 3; :N_END := 10
PL/SQL procedure successfully completed.
SQL> select :N_BEGIN + level - 1 n
2 from dual
3 connect by level <= :N_END - :N_BEGIN + 1
4 /
N
----------
3
4
5
6
7
8
9
10
8 rows selected.
This uses the same trick as Tony's. Note that when you are using SQL*Plus 9, you have to make this query an inline view as Tony showed you. In SQL*Plus 10 or higher, the above is sufficient.
Regards,
Rob.
You can use the MODEL clause for this.
SELECT c1 from dual
MODEL DIMENSION BY (1 as rn) MEASURES (1 as c1)
RULES ITERATE (7)
(c1[ITERATION_NUMBER]=ITERATION_NUMBER+7)
this single line query will help you,
select level lvl from dual where level<:upperbound and
level >:lowerbound connect by level<:limt
For your case:
select level lvl from dual where level<10 and level >3 connect by level<11
let me know if any clarification.
One way to generate numbers from range is to use XMLTABLE('start to end'):
SELECT TO_NUMBER(column_value) integer_value
FROM XMLTABLE('3 to 10');
I added TO_NUMBER because COLUMN_VALUE is a string
DBFiddle Demo
Or you can use Between
Select Column1 from dummy_table where Column2 Between 3 and 10
Gary, to show the result that he explained, the model query will be:
SELECT c1
FROM DUAL
MODEL DIMENSION BY (1 as rn)
MEASURES (1 as c1)
RULES ITERATE (8)
(c1[ITERATION_NUMBER]=ITERATION_NUMBER+3)
ORDER BY rn
;)
I always use:
SELECT (LEVEL - 1) + 3 as result
FROM Dual
CONNECT BY Level <= 8
Where 3 is the start number and 8 is the number of "iterations".
This is a late addition. But the solution seems to be more elegant and easier to use.
It uses a pipelined function that has to be installed once:
CREATE TYPE number_row_type AS OBJECT
(
num NUMBER
);
CREATE TYPE number_set_type AS TABLE OF number_row_type;
CREATE OR REPLACE FUNCTION number_range(p_start IN PLS_INTEGER, p_end IN PLS_INTEGER)
RETURN number_set_type
PIPELINED
IS
out_rec number_row_type := number_row_type(NULL);
BEGIN
FOR i IN p_start .. p_end LOOP
out_rec.num := i;
pipe row(out_rec);
END LOOP;
END number_range;
/
Then you can use it like this:
select * from table(number_range(1, 10));
NUM
---
1
2
3
4
5
6
7
8
9
10
The solution is Oracle specific.
I just did a table valued function to do this in SQL server, if anyone is interested, this works flawlessly.
CREATE FUNCTION [dbo].[NumbersBetween]
(
#StartN int,
#EndN int
)
RETURNS
#NumberList table
(
Number int
)
AS
BEGIN
WHILE #StartN <= #EndN
BEGIN
insert into #NumberList
VALUES (#StartN)
set #StartN = #StartN + 1
END
Return
END
GO
If you run the query: "select * from dbo.NumbersBetween(1,5)" (w/o the quotes of course) the result will be
Number
-------
1
2
3
4
5
I want to share an usefull query that converts a string of comma and '-' separated list of numbers into a the equivalent expanded list of numbers:
An example that converts '1,2,3,50-60' into
1
2
3
50
51
...
60
select distinct * from (SELECT (LEVEL - 1) + mini as result FROM (select REGEXP_SUBSTR (value, '[^-]+', 1, 1)mini ,nvl(REGEXP_SUBSTR (value, '[^-]+', 1, 2),0) maxi from (select REGEXP_SUBSTR (value, '[^,]+', 1, level) as value from (select '1,2,3,50-60' value from dual) connect by level <= length(regexp_replace(value,'[^,]*'))+1)) CONNECT BY Level <= (maxi-mini+1)) order by 1 asc;
You may use it as a view and parametrize the '1,2,3,50-60' string
create table numbers (value number);
declare
x number;
begin
for x in 7 .. 25
loop
insert into numbers values (x);
end loop;
end;
/
In addition to the answers already provided, it is possible to combine the listagg function with connect by to obtain the result in the format mentioned in the question. See a code example below:
SELECT
DBMS_LOB.SUBSTR(LISTAGG(S.INTEGERS,',' ) WITHIN GROUP (ORDER BY S.INTEGERS), 300,1) RESULT
FROM
(SELECT
INTEGERS
FROM
( SELECT ROWNUM INTEGERS FROM DUAL CONNECT BY LEVEL <= 10)
WHERE
INTEGERS >= 3
) S;
OutPut:
SQL>
RESULT
----------------
3,4,5,6,7,8,9,10

Pivot values of a column based on a search string

Note: I would like to do this in a single SQL statement. not pl/sql, cursor loop, etc.
I have data that looks like this:
ID String
-- ------
01 2~3~1~4
02 0~3~4~6
03 1~4~5~1
I want to provide a report that somehow pivots the values of the String column into distinct rows such as:
Value "Total number in table"
----- -----------------------
1 3
2 1
3 2
4 3
5 1
6 1
How do I go about doing this? It's like a pivot table but I am trying to pivot the data in a column, rather than pivoting the columns in the table.
Note that in real application, I do not actually know what the values of the String column are; I only know that the separation between values is '~'
Given this test data:
CREATE TABLE tt (ID INTEGER, VALUE VARCHAR2(100));
INSERT INTO tt VALUES (1,'2~3~1~4');
INSERT INTO tt VALUES (2,'0~3~4~6');
INSERT INTO tt VALUES (3,'1~4~5~1');
This query:
SELECT VALUE, COUNT(*) "Total number in table"
FROM (SELECT tt.ID, SUBSTR(qq.value, sp, ep-sp) VALUE
FROM (SELECT id, value
, INSTR('~'||value, '~', 1, L) sp -- 1st posn of substr at this level
, INSTR(value||'~', '~', 1, L) ep -- posn of delimiter at this level
FROM tt JOIN (SELECT LEVEL L FROM dual CONNECT BY LEVEL < 20) q -- 20 is max #substrings
ON LENGTH(value)-LENGTH(REPLACE(value,'~'))+1 >= L
) qq JOIN tt on qq.id = tt.id)
GROUP BY VALUE
ORDER BY VALUE;
Results in:
VALUE Total number in table
---------- ---------------------
0 1
1 3
2 1
3 2
4 3
5 1
6 1
7 rows selected
SQL>
You can adjust the maximum number of items in your search string by adjusting the "LEVEL < 20" to "LEVEL < your_max_items".

ORACLE SQL:Get all integers between two numbers

Is there any way to select the numbers (integers) that are included between two numbers with SQL in Oracle; I don't want to create PL/SQL procedure or function.
For example I need to get the numbers between 3 and 10. The result will be the values 3,4,5,6,7,8,9,10.
Thx.
This trick with Oracle's DUAL table also works:
SQL> select n from
2 ( select rownum n from dual connect by level <= 10)
3 where n >= 3;
N
----------
3
4
5
6
7
8
9
10
The first thing I do when I create a new database is to create and populate some basic tables.
One is a list of all integers between -N and N, another is a list of dates 5 years in the past through 10 years in the future (a scheduled job can continue creating these as needed, going forward) and the last is a list of all hours throughout the day. For example, the inetgers:
create table numbers (n integer primary key);
insert into numbers values (0);
insert into numbers select n+1 from numbers; commit;
insert into numbers select n+2 from numbers; commit;
insert into numbers select n+4 from numbers; commit;
insert into numbers select n+8 from numbers; commit;
insert into numbers select n+16 from numbers; commit;
insert into numbers select n+32 from numbers; commit;
insert into numbers select n+64 from numbers; commit;
insert into numbers select n+128 from numbers; commit;
insert into numbers select n+256 from numbers; commit;
insert into numbers select n+512 from numbers; commit;
insert into numbers select n+1024 from numbers; commit;
insert into numbers select n+2048 from numbers; commit;
insert into numbers select n+4096 from numbers; commit;
insert into numbers select n+8192 from numbers; commit;
insert into numbers select -n from numbers where n > 0; commit;
This is for DB2/z which has automatic transaction start which is why it seems to have naked commits.
Yes, it takes up a (minimal) space but it makes queries much easier to write, simply by selecting values from those tables. It's also very portable across pretty much any SQL-based DBMS.
Your particular query would then be a simple:
select n from numbers where n >=3 and n <= 10;
The hour figures and date ranges are quite useful for the sort of reporting applications we work on. It allows us to create zero entries for those hours of the day (or dates) which don't have any real data so that, instead of (where there's no data on the second of the month):
Date | Quantity
-----------+---------
2009-01-01 | 7
2009-01-03 | 27
2009-01-04 | 6
we can instead get:
Date | Quantity
-----------+---------
2009-01-01 | 7
2009-01-02 | 0
2009-01-03 | 27
2009-01-04 | 6
SQL> var N_BEGIN number
SQL> var N_END number
SQL> exec :N_BEGIN := 3; :N_END := 10
PL/SQL procedure successfully completed.
SQL> select :N_BEGIN + level - 1 n
2 from dual
3 connect by level <= :N_END - :N_BEGIN + 1
4 /
N
----------
3
4
5
6
7
8
9
10
8 rows selected.
This uses the same trick as Tony's. Note that when you are using SQL*Plus 9, you have to make this query an inline view as Tony showed you. In SQL*Plus 10 or higher, the above is sufficient.
Regards,
Rob.
You can use the MODEL clause for this.
SELECT c1 from dual
MODEL DIMENSION BY (1 as rn) MEASURES (1 as c1)
RULES ITERATE (7)
(c1[ITERATION_NUMBER]=ITERATION_NUMBER+7)
this single line query will help you,
select level lvl from dual where level<:upperbound and
level >:lowerbound connect by level<:limt
For your case:
select level lvl from dual where level<10 and level >3 connect by level<11
let me know if any clarification.
One way to generate numbers from range is to use XMLTABLE('start to end'):
SELECT TO_NUMBER(column_value) integer_value
FROM XMLTABLE('3 to 10');
I added TO_NUMBER because COLUMN_VALUE is a string
DBFiddle Demo
Or you can use Between
Select Column1 from dummy_table where Column2 Between 3 and 10
Gary, to show the result that he explained, the model query will be:
SELECT c1
FROM DUAL
MODEL DIMENSION BY (1 as rn)
MEASURES (1 as c1)
RULES ITERATE (8)
(c1[ITERATION_NUMBER]=ITERATION_NUMBER+3)
ORDER BY rn
;)
I always use:
SELECT (LEVEL - 1) + 3 as result
FROM Dual
CONNECT BY Level <= 8
Where 3 is the start number and 8 is the number of "iterations".
This is a late addition. But the solution seems to be more elegant and easier to use.
It uses a pipelined function that has to be installed once:
CREATE TYPE number_row_type AS OBJECT
(
num NUMBER
);
CREATE TYPE number_set_type AS TABLE OF number_row_type;
CREATE OR REPLACE FUNCTION number_range(p_start IN PLS_INTEGER, p_end IN PLS_INTEGER)
RETURN number_set_type
PIPELINED
IS
out_rec number_row_type := number_row_type(NULL);
BEGIN
FOR i IN p_start .. p_end LOOP
out_rec.num := i;
pipe row(out_rec);
END LOOP;
END number_range;
/
Then you can use it like this:
select * from table(number_range(1, 10));
NUM
---
1
2
3
4
5
6
7
8
9
10
The solution is Oracle specific.
I just did a table valued function to do this in SQL server, if anyone is interested, this works flawlessly.
CREATE FUNCTION [dbo].[NumbersBetween]
(
#StartN int,
#EndN int
)
RETURNS
#NumberList table
(
Number int
)
AS
BEGIN
WHILE #StartN <= #EndN
BEGIN
insert into #NumberList
VALUES (#StartN)
set #StartN = #StartN + 1
END
Return
END
GO
If you run the query: "select * from dbo.NumbersBetween(1,5)" (w/o the quotes of course) the result will be
Number
-------
1
2
3
4
5
I want to share an usefull query that converts a string of comma and '-' separated list of numbers into a the equivalent expanded list of numbers:
An example that converts '1,2,3,50-60' into
1
2
3
50
51
...
60
select distinct * from (SELECT (LEVEL - 1) + mini as result FROM (select REGEXP_SUBSTR (value, '[^-]+', 1, 1)mini ,nvl(REGEXP_SUBSTR (value, '[^-]+', 1, 2),0) maxi from (select REGEXP_SUBSTR (value, '[^,]+', 1, level) as value from (select '1,2,3,50-60' value from dual) connect by level <= length(regexp_replace(value,'[^,]*'))+1)) CONNECT BY Level <= (maxi-mini+1)) order by 1 asc;
You may use it as a view and parametrize the '1,2,3,50-60' string
create table numbers (value number);
declare
x number;
begin
for x in 7 .. 25
loop
insert into numbers values (x);
end loop;
end;
/
In addition to the answers already provided, it is possible to combine the listagg function with connect by to obtain the result in the format mentioned in the question. See a code example below:
SELECT
DBMS_LOB.SUBSTR(LISTAGG(S.INTEGERS,',' ) WITHIN GROUP (ORDER BY S.INTEGERS), 300,1) RESULT
FROM
(SELECT
INTEGERS
FROM
( SELECT ROWNUM INTEGERS FROM DUAL CONNECT BY LEVEL <= 10)
WHERE
INTEGERS >= 3
) S;
OutPut:
SQL>
RESULT
----------------
3,4,5,6,7,8,9,10