How to remove specific initial characters from a column in oracle? - oracle

I have column by the name of phone_number and it consists different types of numbers for example:
phone_number
078912354
93784385483
009378248448
776868886
So I want to remove all the initial numbers which starts with(0,93,0093). The expected result which I want is:
phone_number
78912354
784385483
78248448
776868886

Here's one option:
Sample data:
SQL> select * from test order by phone_number;
PHONE_NUMBER NEW_PHONE_NUMBER
------------ --------------------
009378248448
078912354
776868886
93784385483
Remove leading characters you mentioned:
SQL> update test set
2 new_phone_number = regexp_replace(phone_number, '^(0093|093|93|0)');
4 rows updated.
Result:
SQL> select * from test order by phone_number;
PHONE_NUMBER NEW_PHONE_NUMBER
------------ --------------------
009378248448 78248448
078912354 78912354
776868886 776868886
93784385483 784385483
SQL>

Related

adding a sequence to an existing table

i created a table but i forgot to add a sequence to one of the PK, its a sequence on a form page, i just cant find anything about it, is it possible or do i have to do the form all over again.
i tried to replace the PK but it doesnt give me the option to add the sequence when creating a new one.
i searched everywhere and asked the support in chat (didn't really help since its not their job).
all i could find was this and this.
I'd suggest you to skip Apex in this matter and do the following: presume this is your table:
SQL> create table test
2 (id number constraint pk_test primary key,
3 name varchar2(20)
4 );
Table created.
This is the sequence:
SQL> create sequence myseq;
Sequence created.
As you forgot to specify PK source while creating Apex Form page, never mind - let the database handle it. How? Create a BEFORE INSERT trigger:
SQL> create or replace trigger trg_bi_test
2 before insert on test
3 for each row
4 when (new.id is null)
5 begin
6 :new.id := myseq.nextval;
7 end trg_bi_test;
8 /
Trigger created.
Let's test it: I'm inserting only the NAME (which is what your Apex Form will be doing):
SQL> insert into test (name) values ('Littlefoot');
1 row created.
What is table's contents?
SQL> select * from test;
ID NAME
---------- --------------------
1 Littlefoot
SQL>
See? Trigger automatically inserted ID (primary key) column value.
If it were an Interactive Grid (which lets you insert several records at a time):
SQL> insert into test (name)
2 select 'Bigfoot' from dual union all
3 select 'FAD' from dual;
2 rows created.
SQL> select * from test;
ID NAME
---------- --------------------
1 Littlefoot
2 Bigfoot
3 FAD
SQL>
Works just fine.
And what's another benefit: you don't have to modify Apex application at all.

Query select on varchar2 works without apex

I have a query on field type varchar2 that works also without apex.
E.g.
SELECT * FROM table WHERE field = 123456
where field is varchar2, and it returns the result and not the
ORA-01722: invalid number.
There is some setting on DB that make it possible? Thankx.
If that column's datatype is varchar2, then enclose its value with single quotes:
SELECT * FROM table WHERE field = '123456'
There's no setting that would "remove" invalid number error, as far as I can tell.
Query you posted works when there are only "numbers" in a table; though, when "something else" (it is a varchar2 after all) is stored in the column, it won't work any more:
Sample table:
SQL> create table test (id number, field varchar2(10));
Table created.
Numbers only:
SQL> insert into test (id, field) values (1, 123456);
1 row created.
SQL> select * from test where field = 123456;
ID FIELD
---------- ----------
1 123456
Enter value that contains e.g. letters in field:
SQL> insert into test (id, field) values (2, 'ABC');
1 row created.
SQL> select * from test where field = 123456;
ERROR:
ORA-01722: invalid number
no rows selected
SQL>
So, it is not "Apex" to blame, but you who tried to misuse what you have.

How to update value with special character in oracle 11g

I want to update the password having special characters ''?# #C $4 ABC (starting two characters are two single quotes) in Xyz table.
I am trying the following query
UPDATE Xyz set password="''?# #C $4" where user_no like '%123%';
But I am getting error as
ORA-00911: invalid charachter
The q-quoting mechanism helps in such situations, when you have to work with multiple single quotes within the string.
SQL> desc xyz
Name Null? Type
----------------------------------------- -------- ----------------------------
USER_NO NUMBER
PASSWORD VARCHAR2(20)
SQL> select * From xyz;
USER_NO PASSWORD
---------- --------------------
123 a
SQL> update xyz set password = q'[''?# #C $3]' where user_no = 123;
1 row updated.
SQL> select * From xyz;
USER_NO PASSWORD
---------- --------------------
123 ''?# #C $3
SQL>
Are you pasting the query from a different editor or IDE ? or Maybe copying from windows applications to Linux? In that case, there may be non-printable characters present.
If so, you could retype (not copy-paste) the SQL statement and try.
Also, double quotes aren't commonly used in SQL. You may want to replace them with single quotes.

How should I use an object type in an insert DML statement?

I have created two TYPE objects to try out OOP processing in PL/SQL.
I tried to use my type o_customers in my INSERT statement, but I could not do it.
There is a Customers table. It has same columns as o_customers.
create or replace type o_customers as object (
id number,
name varchar2(40),
age number,
address o_addressC,
salary number
);
create or replace type o_addressC as object (
mahalle varchar(30),
apartman varchar(15),
ilce varchar(15),
apt_no number
);
declare
adres o_addressC;
musteri o_customers;
begin
adres := o_addressC('selami ali mah','çınar apt',' üsküdar',19);
musteri:= o_customers(10,'UĞUR SİNAN SAĞIROĞLU',26,adres,1000);
insert into customers values (musteri);
end;
" There is a customers table. it has same columns with o_customers"
In OOP it is not enough for objects to have the same structure to be compatible in a programming context: they must be the same type, or related to each other through inheritance.
So you need to create the table using that type:
SQL> create table customers of o_customers
2 /
Table created.
SQL> desc customers
Name Null? Type
---------------------- -------- -------------
ID NUMBER
NAME VARCHAR2(40)
AGE NUMBER
ADDRESS O_ADDRESSC
SALARY NUMBER
SQL>
Now your insert statement will work:
SQL> declare
2 adres o_addressC;
3 musteri o_customers;
4 begin
5 adres := o_addressC('selami ali mah','cınar apt','uskudar',19);
6 musteri:= o_customers(10,'UĞUR SİNAN SAĞIROĞLU',26,adres,1000);
7 insert into customers values(musteri);
8 end;
9 /
PL/SQL procedure successfully completed.
SQL> select * from customers;
ID NAME AGE
---------- ---------------------------------------- ----------
ADDRESS(MAHALLE, APARTMAN, ILCE, APT_NO)
------------------------------------------------------------------------------------------------------------------------------------------------------
SALARY
----------
10 UĞUR SİNAN SAĞIROĞLU 26
O_ADDRESSC('selami ali mah', 'c??nar apt', ' uskudar', 19)
1000
SQL>
Incidentally I had to make minor changes to the inserted values because the posted statement hurled
declare
*
ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error: character string buffer too small
ORA-06512: at line 6
This is because your o_addressC type attributes are too small for strings with multi-byte characters.
Unless customers is an object table (create table customers of o_customers), you'll need to refer to the object's properties explicitly:
insert into customers
( id, name, age, address, salary)
values
( musteri.id, musteri.name, musteri.age, musteri.address, musteri.salary );
By the way, o_customer (no 's') would make more sense than o_customers for an object name.

Oracle Precision

I want a number to take values between -7.2E-75 to +7.2E+75. How should I specify precision and scale for the NUMBER. (The scale range is 1 to 38 and precision is -84 to 127).
If there is any other way to accomplish this please suggest.
The numeric values you propose are well within the limits of the Oracle NUMBER datatype.
Do you need to specify a precision? Defining a column as just NUMBER is allowed:
SQL> desc t1
Name Null? Type
----------------------------------------- -------- ----------------------------
COL1 NUMBER
COL2 NUMBER
COL3 NUMBER
COL4 NUMBER
SQL> insert into t1 (col1, col2) values ( -7.2E-75, +7.2E+75);
1 row created.
SQL>
If it's the range bounds which are bothering you, you need to define a CHECK constraint to enforce the rule.
"Suppose i want COL1 to take 123456766675544.344546567676, with NUMBER as datatype its failing"
Failing? How? That value works fine for me (same table as before):
SQL> insert into t1 (col1) values ( 123456766675544.344546567676);
1 row created.
SQL>
So, please provide more details, such as the error message.
"When i fire the above query it is showing just 123456766675544,"
That sounds like a display issue. The right values are being inserted. Check this out:
SQL> set numwidth 50
SQL> select col1 from t1
/
2
COL1
--------------------------------------------------
7.20000000000000000000000000000000000000000000E+75
123456766675544.344546567676
SQL>
In this case, setting the NUMWIDTH to its maximum allowed value ( in SQL*plus) allows us to display one value but the other is still too big, and so we have to use scientific notation.

Resources