Multiple Sequence in SQL Oracle / Varchar2 ID with letters and digits - sql

I am trying to 'generate' a Varchar ID that contains letters and digits too with a sequence, but I think there will be more sequences than one.
What I am looking forward to is something like this 'DJ_Digit_Digit_Letter_Letter_Letter'. An example would be DJ00WVX/DJ01HYZ/DJ99ZZZ. This is also my primary key so it would be good if I could not encounter any primary key errors.
I thought about working in ascii so I will generate numbers between 65 and 90 (A-Z) and Insert them into the column. Same for the numbers in ascii 45 to 57 or something like that. I don't know how I could use more sequences on the same column like 'letter_seq.nextvalue,digit_seq.nextvalue', i know that this example is wrong, but it was easier for me to explain it. Maybe all my thinking is wrong
In conclusion I need to get something that starts with DJ followed by 2 digits and 3 letters, all wrapped up in a primary key. It's ok to get like 20-30 unique entries, I think.
Thank you for your help!

You can create an INVISIBLE IDENTITY column and then create your letter-digit-letter sequence as a function generated from the invisible column:
CREATE TABLE table_name (
id_seq_value INT
INVISIBLE
GENERATED ALWAYS AS IDENTITY( START WITH 0 INCREMENT BY 1 MINVALUE 0 MAXVALUE 175799 )
NOT NULL
CONSTRAINT table_name__id_seq_value__u UNIQUE,
id VARCHAR2(7)
GENERATED ALWAYS AS (
CAST(
'DJ'
|| TO_CHAR( FLOOR( id_seq_value / POWER( 26, 3 ) ), 'FM00' )
|| CHR( 65 + MOD( FLOOR( id_seq_value / POWER(26, 2) ), 26 ) )
|| CHR( 65 + MOD( FLOOR( id_seq_value / POWER(26, 1) ), 26 ) )
|| CHR( 65 + MOD( id_seq_value, 26 ) )
AS VARCHAR2(7)
)
)
CONSTRAINT table_name__id__pk PRIMARY KEY,
name VARCHAR2(50)
);
Then you can do:
INSERT INTO table_name ( name ) VALUES ( 'Item1' );
INSERT INTO table_name ( name ) VALUES ( 'Item2' );
Then:
SELECT * FROM table_name;
Outputs:
ID | NAME
:------ | :----
DJ00AAA | Item1
DJ00AAB | Item2
(If you want to increment the digits and the letters together then change the IDENTITY to INCREMENT BY 17577 (263+1).)
db<>fiddle here

To me, it looks like this:
SQL> create table test (id varchar2(10));
Table created.
SQL> create sequence seq1;
Sequence created.
SQL> insert into test (id)
2 with
3 a as (select chr(64 + level) la from dual
4 connect by level <= 26),
5 b as (select chr(64 + level) lb from dual
6 connect by level <= 26),
7 c as (select chr(64 + level) lc from dual
8 connect by level <= 26)
9 select 'DJ' || lpad(seq1.nextval, 2, '0')
10 || la || lb || lc id
11 from a cross join b cross join c;
17576 rows created.
SQL>
Several sample values:
SQL> with temp as
2 (select id,
3 row_number() Over (order by id) rna,
4 row_Number() over (order by id desc) rnd
5 from test
6 )
7 select id
8 from temp
9 where rna <= 5
10 or rnd <= 5
11 order by id;
ID
----------
DJ01AAA
DJ02AAB
DJ03AAC
DJ04AAD
DJ05AAE
DJ99OUK
DJ99OUL
DJ99OUM
DJ99OUN
DJ99OUO
10 rows selected.
SQL>
However, if
It's ok to get like 20-30 unique entries, I think.
means that you need to generate at most 30 values, well, you'd easily create them manually; why would you develop any software solution for that? If you started typing, you'd be over by now.

Related

How to insert into temp table when looping through a string - Oracle - PL/SQL

CREATE GLOBAL TEMPORARY TABLE tt_temptable(
RowNums NUMBER(3,0),
procNums NUMBER(18,0)
) ON COMMIT PRESERVE ROWS;
inputString VARCHAR2 ;
inputString := '12,13,14,15'
SELECT REGEXP_SUBSTR (inputString,'[^,]+',1,LEVEL) ProcNums
FROM dual CONNECT BY REGEXP_SUBSTR (inputString,'[^,]+',1,LEVEL) IS NOT NULL;
INSERT INTO tt_temptable(
SELECT identity(3) RowNums,procNums
FROM
);
Want to insert 12 , 13, 14 , 15 and identity of 3 length in the temptable so total 4 rows in temptable
If you use Oracle 12c, then you may define an IDENTITY column through GENERATED ALWAYS AS IDENTITY in your table definition and follow the way below :
SQL> CREATE GLOBAL TEMPORARY TABLE tt_temptable(
2 RowNums NUMBER(3,0) GENERATED ALWAYS AS IDENTITY,
3 procNums NUMBER(18,0)
4 ) ON COMMIT PRESERVE ROWS;
Table created
SQL>
SQL> DECLARE
2 inputString VARCHAR2(50) := '12,13,14,15';
3 BEGIN
4 INSERT INTO tt_temptable(procNums)
5 SELECT REGEXP_SUBSTR (inputString,'[^,]+',1,LEVEL) ProcNums
6 FROM dual
7 CONNECT BY REGEXP_SUBSTR (inputString,'[^,]+',1,LEVEL) IS NOT NULL;
8 END;
9 /
PL/SQL procedure successfully completed
SQL> SELECT * FROM tt_temptable;
ROWNUMS PROCNUMS
------- -------------------
1 12
2 13
3 14
4 15
To reset the IDENTITY column (RowNums), use :
SQL> ALTER TABLE tt_temptable MODIFY( RowNums Generated as Identity (START WITH 1));
whenever the shared locks on the table are released.
insert
into tt_temptable
select NVL((select max(a.rownums)
from tt_temptable a
),100)+rownum
,procNums
from (SELECT REGEXP_SUBSTR ('10,20,30','[^,]+',1,LEVEL) ProcNums,level as lvl
FROM dual
CONNECT BY REGEXP_SUBSTR ('10,20,30','[^,]+',1,LEVEL) IS NOT NULL
)x

value substitution/replacement in a string

I have a string x-y+z. The values for x, y and z will be stored in a table. Say
x 10
y 15
z 20
This string needs to be changed like 10-15+20.
Anyway I can achieve this using plsql or sql?
using simple Pivot we can do
DECLARE #Table1 TABLE
( name varchar(1), amount int )
;
INSERT INTO #Table1
( name , amount )
VALUES
('x', 10),
('y', 15),
('Z', 25);
Script
Select CAST([X] AS VARCHAR) +'-'+CAST([Y] AS VARCHAR)+'+'+CAST([Z] AS VARCHAR) from (
select * from #Table1)T
PIVOT (MAX(amount) FOR name in ([X],[y],[z]))p
An approach could be the following, assuming a table like this:
create table stringToNumbers(str varchar2(16), num number);
insert into stringToNumbers values ('x', 10);
insert into stringToNumbers values ('y', 20);
insert into stringToNumbers values ('zz', 30);
insert into stringToNumbers values ('w', 40);
First tokenize your input string with something like this:
SQL> with test as (select 'x+y-zz+w' as string from dual)
2 SELECT 'operand' as typ, level as lev, regexp_substr(string, '[+-]+', 1, level) as token
3 FROM test
4 CONNECT BY regexp_instr(string, '[a-z]+', 1, level+1) > 0
5 UNION ALL
6 SELECT 'value', level, regexp_substr(string, '[^+-]+', 1, level) as token
7 FROM test
8 CONNECT BY regexp_instr(string, '[+-]', 1, level - 1) > 0
9 order by lev asc, typ desc;
TYP LEV TOKEN
------- ---------- --------------------------------
value 1 x
operand 1 +
value 2 y
operand 2 -
value 3 zz
operand 3 +
value 4 w
In the example I used lowercase literals and only +/- signs; you can easily edit it to handle something more complex; also, I assume the input string is well-formed.
Then you can join your decoding table to the tokenized string, building the concatenation:
SQL> select listagg(nvl(to_char(num), token)) within group (order by lev asc, typ desc)
2 from (
3 with test as (select 'x+y-zz+w' as string from dual)
4 SELECT 'operand' as typ, level as lev, regexp_substr(string, '[+-]+', 1, level) as token
5 FROM test
6 CONNECT BY regexp_instr(string, '[a-z]+', 1, level+1) > 0
7 UNION ALL
8 SELECT 'value', level, regexp_substr(string, '[^+-]+', 1, level) as token
9 FROM test
10 CONNECT BY regexp_instr(string, '[+-]', 1, level - 1) > 0
11 order by lev asc, typ desc
12 ) tokens
13 LEFT OUTER JOIN stringToNumbers on (str = token);
LISTAGG(NVL(TO_CHAR(NUM),TOKEN))WITHINGROUP(ORDERBYLEVASC,TYPDESC)
--------------------------------------------------------------------------------
10+20-30+40
This assumes that every literal in you input string has a corrensponding value in table. You can even handle the case of strings with no corrensponding number, for example assigning 0:
SQL> select listagg(
2 case
3 when typ = 'operand' then token
4 else to_char(nvl(num, 0))
5 end
6 ) within group (order by lev asc, typ desc)
7 from (
8 with test as (select 'x+y-zz+w-UNKNOWN' as string from dual)
9 SELECT
.. ...
16 ) tokens
17 LEFT OUTER JOIN stringToNumbers on (str = token);
LISTAGG(CASEWHENTYP='OPERAND'THENTOKENELSETO_CHAR(NVL(NUM,0))END)WITHINGROUP(ORD
--------------------------------------------------------------------------------
10+20-30+40-0
Create a function like this:
create table ttt1
( name varchar(1), amount int )
;
INSERT INTO ttt1 VALUES ('x', 10);
INSERT INTO ttt1 VALUES ('y', 15);
INSERT INTO ttt1 VALUES ('z', 25);
CREATE OR REPLACE FUNCTION replace_vars (in_formula VARCHAR2)
RETURN VARCHAR2
IS
f VARCHAR2 (2000) := UPPER (in_formula);
BEGIN
FOR c1 IN ( SELECT UPPER (name) name, amount
FROM ttt1
ORDER BY name DESC)
LOOP
f := REPLACE (f, c1.name, c1.amount);
END LOOP;
return f;
END;
select replace_vars('x-y+z') from dual
Here's another way to approach the problem that attempts to do it all in SQL. While not necessarily the most flexible or fastest, maybe you can get some ideas from another way to approach the problem. It also shows a way to execute the final formula to get the answer. See the comments below.
Assumes all variables are present in the variable table.
-- First build the table that holds the values. You won't need to do
-- this if you already have them in a table.
with val_tbl(x, y, z) as (
select '10', '15', '20' from dual
),
-- Table to hold the formula.
formula_tbl(formula) as (
select 'x-y+z' from dual
),
-- This table is built from a query that reads the formula a character at a time.
-- When a variable is found using the case statement, it is queried in the value
-- table and it's value is returned. Otherwise the operator is returned. This
-- results in a row for each character in the formula.
new_formula_tbl(id, new_formula) as (
select level, case regexp_substr(formula, '(.|$)', 1, level, NULL, 1)
when 'x' then
(select x from val_tbl)
when 'y' then
(select y from val_tbl)
when 'z' then
(select z from val_tbl)
else regexp_substr(formula, '(.|$)', 1, level, NULL, 1)
end
from formula_tbl
connect by level <= regexp_count(formula, '.')
)
-- select id, new_formula from new_formula_tbl;
-- This puts the rows back into a single string. Order by id (level) to keep operands
-- and operators in the right order.
select listagg(new_formula) within group (order by id) formula
from new_formula_tbl;
FORMULA
----------
10-15+20
Additionally you can get the result of the formula by passing the listagg() call to the following xmlquery() function:
select xmlquery(replace( listagg(new_formula) within group (order by id), '/', ' div ')
returning content).getNumberVal() as result
from new_formula_tbl;
RESULT
----------
15

Oracle Query to select separate alphabet+numbers and numbers after special character

I am writing a query in oracle where I have requirement to separate alphabet+numbers and numbers after special character as 2 different columns
Eg.
Colum Value is
ABC 123#78800,XYZ#4666,PQR 444#9900
Output Required
Column 1 : ABC 123,XYZ,PQR 444
Column 2 : 78800,4666, 9900
I tried following query:
select TRANSLATE('ABC 123#78800,XYZ#4666,PQR 444#9900 ','ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789#','ABCDEFGHIJKLMNOPQRSTUVWXYZ') from dual.
Output is "ABC ,XYZ,PQR " numbers are missing
What you need is regex
with table_name as
(
select 'ABC 123#78800,XYZ#4666,PQR 444#9900' col_name from dual
)
select
REGEXP_REPLACE(col_name,'#([^#,])*', null) alphabet_num
,REGEXP_REPLACE(col_name,'([^#,])*#', null) num_value
from
table_name;
Editted: Remove some redundant character as suggested by MTO
Oracle Setup:
CREATE TABLE table_name ( col VARCHAR2(100) );
INSERT INTO table_name VALUES ( 'ABC 123#78800,XYZ#4666,PQR 444#9900' );
CREATE TYPE key_value_pair AS OBJECT(
key VARCHAR2(20),
value NUMBER(10)
);
/
CREATE TYPE key_value_pair_table AS TABLE of key_value_pair;
/
Query:
SELECT *
FROM table_name t,
TABLE(
CAST(
MULTISET(
SELECT REGEXP_SUBSTR( t.col, '(^|,)([^,#]+)#(\d+)', 1, LEVEL, NULL, 2 ),
TO_NUMBER(
REGEXP_SUBSTR( t.col, '(^|,)([^,#]+)#(\d+)', 1, LEVEL, NULL, 3 )
)
FROM DUAL
CONNECT BY LEVEL <= REGEXP_COUNT( t.col, '(^|,)([^,#]+)#(\d+)' )
)
AS key_value_pair_table
)
);
Output:
COL KEY VALUE
----------------------------------- ------- -----
ABC 123#78800,XYZ#4666,PQR 444#9900 ABC 123 78800
ABC 123#78800,XYZ#4666,PQR 444#9900 XYZ 4666
ABC 123#78800,XYZ#4666,PQR 444#9900 PQR 444 9900

Oracle connect by level complication

I'm having difficulties with a query that's purpose is to split a row into two.
My sample data:
ID OLD NEW
---------- ------------------------ ------------------------
12 L-D / T-E L-E / T-E
13 L-D / T-E L-D / T-D
14 L-D / T-E L-E / T-D
Where the new and old values differ, I'd like to split them so my output should be:
id | OLD | NEW
12 | L-D | L-E //in this instance T hasn't changed so I will ignore it.
13 | T-E | T-D //similar to the above but this time only add T
14 | L-D | L-E //both values have changed, two rows required
14 | T-E | T-D
I've put together a 'working' example but the results are duplicated. I think the issue is with the connect by level, I'm quite new to Oracle so debugging is a touch difficult.
Any help is much appreciated.
This is the query I'm using to drop, create and run the query:
It runs fine in sqldeveloper but unsure if sqlplus will be impressed with it.
drop table t;
CREATE TABLE T
(id int,
old varchar2(24),
new varchar2(24))
;
INSERT ALL
INTO T (id, old, new)
VALUES (12, 'L-D / T-E', 'L-E / T-E')
INTO T (id, old, new)
VALUES (13, 'L-D / T-E', 'L-D / T-D')
INTO T (id, old, new)
VALUES (14, 'L-D / T-E', 'L-E / T-D')
SELECT * FROM dual
;
SELECT * FROM T;
--insert into t (id, old, new) values (1,'dasdsad', 'asdasd');
BEGIN
INSERT INTO t (id,old, new)
WITH DATA AS
(SELECT id,OLD, new
FROM t
WHERE
--multiple changes
(
(SUBSTR(OLD,3,1) <> SUBSTR(NEW, 3,1)
AND SUBSTR(OLD, 9) <> SUBSTR(NEW, 9)
)
OR
--row 2 old/new L are same, old/new T are not
(SUBSTR(OLD,3,1) = SUBSTR(NEW, 3,1)
AND SUBSTR(OLD, 9) <> SUBSTR(NEW, 9)
)
OR
--row 1
(SUBSTR(OLD,3,1) <> SUBSTR(NEW, 3,1)
AND SUBSTR(OLD, 9) = SUBSTR(NEW, 9)
)
)
)
SELECT id, trim(regexp_substr(OLD, '[^/]+', 1, LEVEL)) OLD,
trim(regexp_substr(NEW, '[^/]+', 1, LEVEL)) NEW
FROM DATA
CONNECT BY (LEVEL <= regexp_count(new, '/')+1);
END;
/
I personally wouldn't use a recursive syntax.
First I would create virtual columns (or a view) to eliminate cumbersome substring functions from the query, in this way:
ALTER TABLE t ADD ( OLD_LEFT AS ( Substr( OLD, 1, 3 )));
ALTER TABLE t ADD ( OLD_RIGHT AS ( Substr( OLD, 7, 3 )));
ALTER TABLE t ADD ( NEW_LEFT AS ( Substr( NEW, 1, 3 )));
ALTER TABLE t ADD ( NEW_RIGHT AS ( Substr( NEW, 7, 3 )));
or:
CREATE VIEW tt AS
SELECT ID, OLD, NEW,
Substr( OLD, 1, 3 ) As OLD_LEFT,
Substr( OLD, 7, 3 ) As OLD_RIGHT,
Substr( NEW, 1, 3 ) As NEW_LEFT,
Substr( NEW, 7, 3 ) As NEW_RIGHT
FROM t;
After the above simplification, the query is very straightforward, just:
SELECT id, OLD_LEFT , NEW_LEFT
FROM T
WHERE OLD_LEFT <> NEW_LEFT
UNION ALL
SELECT id, OLD_RIGHT , NEW_RIGHT
FROM T
WHERE OLD_RIGHT <> NEW_RIGHT
ORDER BY ID
Demo: http://sqlfiddle.com/#!4/2fdc4/2

Generate 6 numbers without repetition from the set 1 ... 49

I need to generate 6 numbers without repetition from 1 to 49.
I have something like that:
create table zadanie1(
NAZWISKO varchar2(30),
PUNKTY number,
ID number,
constraint stud1_nazw primary key(NAZWISKO)
);
create or replace procedure "LOTTO" IS
pkt number;
begin
loop
BEGIN
pkt := round(dbms_random.value(1,14),0);
INSERT INTO ZADANIE1(PUNKTY) VALUES (pkt);
exit;
Exception When DUP_VAL_ON_INDEX Then 0;
END;
end loop;
END;
but this doesn't seem to work. Can somebody help me ?
SQL> select num
2 from (select rownum num
3 from dual
4 connect by level <= 49
5 order by dbms_random.value)
6 where rownum <= 6;
NUM
----------
10
6
9
12
22
14
Just put the 49 numbers in a table and use
SELECT SOME_NUMBER FROM
(
SELECT SOME_NUMBER
FROM TABLE_OF_NUMBERS
ORDER BY DBMS_RANDOM.VALUE()
)
WHERE ROWNUM < 7
ok, thx for answer. I need to add column for this table. and for every row(user) add 6 number like in lottery.
create table zadanie1(
user varchar2(30),
PUNKTY number,
ID number,
constraint stud1_nazw primary key(NAZWISKO)
);