Select records which have two digits on the left and four digits on the right side in Oracle - sql

Table with example data:
ID ANPR_TEXT
-------------
1 16AH22551
2 DL8CM8797
In this example I' like to select the row with 16AH22551.
I tried:
select ANPR_TEXT,
to_number(regexp_substr(ANPR_TEXT,'\d+$'))
from TXN_SPEED_CAM;
But it didn't work. Could anyone help, please!

I need 16AH22551
Update OP wants at least count to match, not exact match.
From documentation on POSIX Metacharacters in Oracle Database Regular Expressions
{m,}
Interval—At Least Count
Matches at least m occurrences of the preceding subexpression.
The expression a{3,} matches the strings aaa and aaaa, but does not
match aa.
So, you need to use {m, } expression which means it will match at least m occurrences. REGEXP_LIKE(anpr_text, '[[:digit:]]{2,}[[:alpha:]]{2,}[[:digit:]]{4,}')
For example,
SQL> CREATE TABLE t(ID NUMBER, anpr_text VARCHAR2(20));
Table created.
SQL>
SQL> INSERT INTO t VALUES(1, '16AH22551');
1 row created.
SQL> INSERT INTO t VALUES(2, 'DL8CM8797');
1 row created.
SQL> INSERT INTO t VALUES(3, '123ABC8797 ');
1 row created.
SQL> INSERT INTO t VALUES(4, 'HR29AE5806 ');
1 row created.
SQL>
SQL> COMMIT;
Commit complete.
SQL>
SQL> SELECT * FROM t
2 WHERE REGEXP_LIKE(anpr_text, '[[:digit:]]{2,}[[:alpha:]]{2,}[[:digit:]]{4,}');
ID ANPR_TEXT
---------- --------------------
1 16AH22551
3 123ABC8797
4 HR29AE5806
SQL>
For an exact pattern match:
If you have a fixed pattern of first two digits, then two alphabets and then at least 4 digits, then you could do a pattern match using REGEXP_LIKE.
For example,
SQL> CREATE TABLE t(ID NUMBER, anpr_text VARCHAR2(20));
Table created.
SQL>
SQL> INSERT INTO t VALUES(1, '16AH22551');
1 row created.
SQL> INSERT INTO t VALUES(2, 'DL8CM8797');
1 row created.
SQL> INSERT INTO t VALUES(3, '123ABC8797');
1 row created.
SQL>
SQL> COMMIT;
Commit complete.
SQL>
SQL> SELECT * FROM t
2 WHERE REGEXP_LIKE(anpr_text, '[[:digit:]]{2}[[:alpha:]]{2}[[:digit:]]{4}');
ID ANPR_TEXT
---------- --------------------
1 16AH22551
SQL>

Related

Trying to insert multiple rows in my table in SQL.Always showing SQL Command not ended properly [duplicate]

This question already has answers here:
Best way to do multi-row insert in Oracle?
(9 answers)
Closed 1 year ago.
I am trying to insert multiple rows into my table using the below query : INSERT INTO TABLE2(ID) VALUES(1),(2) .It is showing SQL Command Not Ended.
Is it some syntacital error?I have rechecked but still not clear what's going wrong.
Invalid syntax, of course. But, here are 3 options that work.
SQL> create table table2(id number);
Table created.
SQL>
SQL> insert into table2(id) values(1);
1 row created.
SQL> insert into table2(id) values(2);
1 row created.
SQL>
SQL> insert all
2 into table2(id) values (1)
3 into table2(id) values (2)
4 select * from dual;
2 rows created.
SQL>
SQL> insert into table2(id)
2 select 1 from dual
3 union all
4 select 2 from dual;
2 rows created.
SQL>

Create Auto Sequence text and number in oracle 11g

How I do create column ID with value JASG1?
I am only find example like this :
select 'JASG'||to_char(mtj_id_seq.nextval) from talend_job
Although what you wrote probably works (if there's a sequence named MTJ_ID_SEQ, you have a privilege to select from it; the same goes for the TALEND_JOB table), I'd say that it isn't what you should use.
Here's why: I'll create a table and a sequence. Table will be pre-populated with some IDs (just to put something in there).
SQL> create sequence mtj_id_seq;
Sequence created.
SQL> create table talend_job as
2 select rownum id from dept;
Table created.
SQL> select * from talend_job;
ID
----------
1
2
3
4
OK; 4 rows so far. Now, run your SELECT:
SQL> select 'JASG'||to_char(mtj_id_seq.nextval) from talend_job;
'JASG'||TO_CHAR(MTJ_ID_SEQ.NEXTVAL)
--------------------------------------------
JASG1
JASG2
JASG3
JASG4
SQL> select 'JASG'||to_char(mtj_id_seq.nextval) from talend_job;
'JASG'||TO_CHAR(MTJ_ID_SEQ.NEXTVAL)
--------------------------------------------
JASG5
JASG6
JASG7
JASG8
SQL>
See? You didn't get only 1 JASGx value, but as many as number of rows in the TALEND_JOB table. If there was a million rows, you'd get a million JASGx rows as well.
Therefore, maybe you meant to use DUAL table instead? E.g.
SQL> select 'JASG'||to_char(mtj_id_seq.nextval) from dual;
'JASG'||TO_CHAR(MTJ_ID_SEQ.NEXTVAL)
--------------------------------------------
JASG9
SQL> select 'JASG'||to_char(mtj_id_seq.nextval) from dual;
'JASG'||TO_CHAR(MTJ_ID_SEQ.NEXTVAL)
--------------------------------------------
JASG10
SQL>
See? Only one value.
Also, notice that sequences will provide unique values, but you can't rely on them being gapless.
As you mentioned "how to create column ID" - one option is to use a trigger. Here's an example:
SQL> create table talend_job (id varchar2(20), name varchar2(20)
Table created.
SQL> create or replace trigger trg_bi_tj
2 before insert on talend_job
3 for each row
4 begin
5 :new.id := 'JASG' || mtj_id_seq.nextval;
6 end;
7 /
Trigger created.
Let's insert some names; IDs should be auto-populated by the trigger:
SQL> insert into talend_job (name) values ('littlefoot');
1 row created.
SQL> insert into talend_job (name) values ('Ishak');
1 row created.
SQL> select * From talend_job;
ID NAME
-------------------- --------------------
JASG11 littlefoot
JASG12 Ishak
SQL>
OK then; now you have some more info - read and think about it.
By the way, what is the "compiler-errors" tag used for? Did you write any code and it failed? Perhaps you'd want to share it with us.

Insert All with Sequence.nextVal Generates Unique Constraint violation [duplicate]

This question already has answers here:
Inserting multiple rows with sequence in Oracle
(5 answers)
Closed 4 years ago.
Why does this query throw an error:
Error report -
ORA-00001: unique constraint (ON24MASTER.LANGUAGE_CODE_PK) violated
Query
INSERT ALL
INTO LANGUAGE_CODE(language_code_id,label,language_cd,country_cd,is_elite)
VALUES(SEQ_LANGUAGE_CODE_ID.NEXTVAL,'Abkhazian','ab',NULL,'N')
INTO LANGUAGE_CODE(language_code_id,label,language_cd,country_cd,is_elite)
VALUES(SEQ_LANGUAGE_CODE_ID.NEXTVAL,'Afar','aa',NULL,'N')
INTO LANGUAGE_CODE(language_code_id,label,language_cd,country_cd,is_elite)
VALUES(SEQ_LANGUAGE_CODE_ID.NEXTVAL,'Afrikaans','af',NULL,'N')
INTO LANGUAGE_CODE(language_code_id,label,language_cd,country_cd,is_elite)
VALUES(SEQ_LANGUAGE_CODE_ID.NEXTVAL,'Akan','ak',NULL,'N')
SELECT 1 FROM DUAL;
Considering select seq_language_code_id.nextval from dual; = 198 and the latest id in db is 180;
What is wrong ? can I not use insert all with .nextVal ?
Nope, you can't do it with INSERT ALL. Have a look at what's going on:
SQL> create sequence seqa;
Sequence created.
SQL> create table test (id number);
Table created.
SQL> insert all
2 into test values (seqa.nextval)
3 into test values (seqa.nextval)
4 into test values (seqa.nextval)
5 into test values (seqa.nextval)
6 select * from dual;
4 rows created.
SQL> select * From test;
ID
----------
1
1
1
1
SQL>
See? All NEXTVALs are just the same, which - in your case - leads to a primary key violation, which means that you'll have to run separate INSERT INTO statements:
SQL> insert into test values (seqa.nextval);
1 row created.
SQL> insert into test values (seqa.nextval);
1 row created.
SQL> insert into test values (seqa.nextval);
1 row created.
SQL> select * From test;
ID
----------
1
1
1
1
2
3
4
7 rows selected.
SQL>

Multi line comment in oracle sqlplus

I have ran the below statements in sqlplus. It inserts duplicate of row 2. In log I have found that i row created after the comment line as well.
So, here I am asking the multi line comment should have a space between /* and comment and */ ?
insert into table values (1);
insert into table values (2);
/*comments here*/
insert into table values (3);
commit;
Log:
SQL> insert into table values (1);
1 row created.
SQL> insert into table values (2);
1 row created.
**SQL> /*comments here*/
1 row created.**
SQL> insert into table values (3);
1 row created.
select A from table;
A
------------
1
2
2
3
All,
I have tried in sqlplus. We need to give the space or new line between /* and following characters. So it treated as multi line comment.
correct syntax:
/* comments here */
or
/*
comments here
*/
Wrong syntax:
/*comments here*/
I presume that your code contains an extra forward slash. Have a look:
SQL> create table test (id number);
Table created.
SQL> insert into test values (1);
1 row created.
SQL> insert into test values (2);
1 row created.
SQL> /* comments
SQL> here
SQL> */
SQL> / --> this one; it executes the last command in buffer ...
1 row created. --> ... and results with yet another "2" begin inserted
SQL> insert into test values (3);
1 row created.
SQL> select * from test;
ID
----------
1
2
2
3
SQL>
Without it, everything is OK:
SQL> truncate table test;
Table truncated.
SQL> insert into test values (1);
1 row created.
SQL> insert into test values (2);
1 row created.
SQL> /* comments
SQL> here
SQL> */
SQL> insert into test values (3);
1 row created.
SQL> select * from test;
ID
----------
1
2
3
SQL>

How to allow oracle table column to have multiple rows in column but sames values only. Using Constraints

How to allow oracle table column to have multiple rows in column but same value only.
Create Table test ( col int);
case - I
insert into test values (1);
--should work
insert into test values (1);
--should work
insert into test values (2);
--Should throw error !!!!
case - II
truncate table test;
insert into test values (2);
--should work
insert into test values (2);
--Should work
insert into test values (1);
--Should throw error !!!!!!
If I have understood your requirement correctly, you can use before insert trigger. Check the value present in table, if new value matches with table value or table is having no data then only allow insert.
Here's the link about trigger
http://docs.oracle.com/cd/B19306_01/appdev.102/b14251/adfns_triggers.htm
UPDATE
Based on OP's two cases, I think a BEFORE INSERT TRIGGER would do the job:
Test case:
SQL> DROP TABLE TEST PURGE;
Table dropped.
SQL>
SQL> CREATE TABLE test
2 ( col INT
3 );
Table created.
SQL>
SQL> CREATE OR REPLACE TRIGGER trg BEFORE
2 INSERT ON TEST FOR EACH ROW DECLARE ID NUMBER;
3 BEGIN
4 BEGIN
5 SELECT DISTINCT col INTO ID FROM TEST;
6 EXCEPTION
7 WHEN no_data_found THEN
8 NULL;
9 END;
10 IF :NEW.col <> id THEN
11 RAISE_APPLICATION_ERROR(-20001, 'Cannot Insert different value in the table');
12 END IF;
13 END;
14 /
Trigger created.
SQL>
The NO_DATA_FOUND exception is to ignore the first insert, since there would be no rows before that.
Now, let's test the INSERT statements:
CASE 1
SQL> INSERT INTO TEST VALUES (1);
1 row created.
SQL>
SQL> INSERT INTO TEST VALUES (1);
1 row created.
SQL>
SQL> INSERT INTO TEST VALUES (2);
INSERT INTO TEST VALUES (2)
*
ERROR at line 1:
ORA-20001: Cannot Insert different value in the table
ORA-06512: at "LALIT.TRG", line 10
ORA-04088: error during execution of trigger 'LALIT.TRG'
SQL>
CASE 2
SQL> TRUNCATE TABLE TEST;
Table truncated.
SQL>
SQL> INSERT INTO TEST VALUES (2);
1 row created.
SQL>
SQL> INSERT INTO TEST VALUES (2);
1 row created.
SQL>
SQL> INSERT INTO TEST VALUES (1);
INSERT INTO TEST VALUES (1)
*
ERROR at line 1:
ORA-20001: Cannot Insert different value in the table
ORA-06512: at "LALIT.TRG", line 10
ORA-04088: error during execution of trigger 'LALIT.TRG'
SQL>