I have created a table in an Oracle database as shown below:
create table employee(eid int, enameemp varchar(1), emgrid int);
insert into employee values(101,'A',103);
insert into employee values(102,'B',103);
insert into employee values(103,'C',104);
insert into employee values(104,'D',101);
//Displaying the table contents
select * from employee;
EID E EMGRID
--- - ------
101 A 103
102 B 103
103 C 104
104 D 101
The column name is not displayed completely unlike MySQL. Is there any way I can go about it without increasing the column size of enameemp i.e enameemp varchar(10)?
If you use SQL*Plus, By default the output column size would be colummn size itself. If you wish have a custom size you can always.
Try the below command.
COL COLUMN_NAME FORMAT A<ur column size>
EXAMPLE: COL ENAMEEMP FORMAT A10
More SQL plus options from Oracle Docs
Related
Suppose if the table name is ABC_XYZ_123. I want to extract the integer values after _.
The output should be integer values after _.
In the above case, the output should be 123.
I have used the below sql query.
select from table_name like 'XXX_%';
But I am not getting required output. Can anyone help me with this query.
Thanks
Using REGEXP_SUBSTR with a capture group we can try:
SELECT REGEXP_SUBSTR(name, '_(\d+)$', 1, 1, NULL, 1)
FROM yourTable;
The question is somewhat unclear:
it looks as if you're looking for table names that contain number at the end, while
query you posted suggests that you're trying to select those numbers from one of table's columns
I'll stick to
Suppose if the table name is ABC_XYZ_123
If that's so, it is the data dictionary you'll query. USER_TABLES contains that information.
Let's create that table:
SQL> create table abc_xyz_123 (id number);
Table created.
Query selects numbers at the end of table names, for all my tables that end with numbers.
SQL> select table_name,
2 regexp_substr(table_name, '\d+$') result
3 from user_tables
4 where regexp_like(table_name, '\d+$');
TABLE_NAME RESULT
-------------------- ----------
TABLE1 1
TABLE2 2
restore_point-001 001
ABC_XYZ_123 123 --> here's your table
SQL>
Apparently, I have a few of them.
I was declared one column in oracle datatype is number ex:cust_acc_no NUMBER (9) DEFAULT (0),.
After creating table that column take double datatype why?
but that column is account number so when i select that particular field it shows account numbers with decimal.
If you didn't put any decimal numbers into it, then they aren't decimal numbers. If you think they are, please, post an example - copy/paste your SQL*Plus session which shows what you're saying. I suspect that it is matter of formatting, not data storage.
By the way, you could have used the INT datatype, e.g.
SQL> create table test (cust_acc_no int default 0);
Table created.
SQL> insert into test
2 select 100 from dual union
3 select 0.5 from dual union
4 select 20.6 from dual;
3 rows created.
SQL> select * from test;
CUST_ACC_NO
-----------
1
21
100
SQL>
One of my field has Data Type as Numeric and the size is (16,8). If I give the value as 0.000000012 will it take and load it in the table? As there are 9 places after the decimal.
It will accept and round the values (at least in 11g):
SQL> CREATE TABLE tst (c1 NUMBER(10,2));
Table created
SQL> INSERT INTO tst VALUES (9.123);
1 row inserted
SQL> INSERT INTO tst VALUES (9.129);
1 row inserted
SQL> SELECT * FROM tst;
C1
------------
9.12
9.13
I am currently building a database system using oracle 10g. I have developed a query that shows an instance of an event, the customer who requested the event and the staff assigned to the event. My query returns all the correct results but I wanted to know if there is a way to delete the repeating data. For each staff member assigned the result returns the event and customer details. I have attached a picture. Is there a way to return the customer and event details only once in their columns and the corresponding records of staff assigned in that columns?
This my code
COLUMN E_ID FORMAT A7
COLUMN E_NAME FORMAT A20
COLUMN E_STIME FORMAT A30
COLUMN E_FTIME FORMAT A30
COLUMN E_COST FORMAT 9999999.99
COLUMN ET_ET_ID FORMAT A5
COLUMN V_V_ID FORMAT A5
COLUMN C_C_ID FORMAT A5
COLUMN C_FNAME FORMAT A10
COLUMN C_LNAME FORMAT A10
COLUMN S_S_ID FORMAT A5
COLUMN S_FNAME FORMAT A10
COLUMN S_LNAME FORMAT A10
COLUMN S_TASK FORMAT A20
COLUMN S_CNUM FORMAT 9999999
COLUMN ST_DESC FORMAT A20
SELECT E_ID, E_NAME, EVENT.C_C_ID ,C_FNAME, C_LNAME, E_STIME, E_FTIME, E_COST, EVENT.ET_ET_ID, EVENT.V_V_ID, EVENT_STAFF.S_S_ID, S_FNAME, S_LNAME, S_TASK, S_CNUM, STAFFTYPE.ST_DESC
FROM CUSTOMER, EVENT, EVENT_STAFF, STAFF, STAFFTYPE
WHERE EVENT.E_ID = 'E000004'
AND EVENT.C_C_ID = CUSTOMER.C_ID
AND EVENT.E_ID = EVENT_STAFF.E_E_ID
AND EVENT_STAFF.S_S_ID = STAFF.S_ID
AND STAFF.ST_ST_ID = STAFFTYPE.ST_ID;
The results in SQL plus is as shown
That's what the BREAK ON directive in SQL*Plus is for. Add to your COLUMN formatting directives:
BREAK ON E_ID ON E_NAME ...
listing all columns where you want to omit duplicates. Don't forget to use ORDER BY to ensure that records for each event and customer return in the right sequence.
I found many examples showing how to create table with nested table column and how to update them “manually” like detailed at steps 1-5.
What I want to do:
update the nested table column (“cust_info” at the example) directly/automatically from other table in the database (NEW_TBL- that has the same structure) and not entering the values one after another manually.
(1) Create Object:
CREATE TYPE TEST_OBJECT_T AS OBJECT
(
x NUMBER,
y NUMBER
);
(2) CREATE collection:
CREATE TYPE TEST_OBJ_TBL IS TABLE OF TEST_OBJECT_T;
(3) Create table with nested table column
create table aaaTable
(
CUSTID number,
cust_info TEST_OBJ_TBL
)
NESTED TABLE cust_info STORE AS xx_tbl
;
(4) --insert data
insert into AAATABLE
VALUES (1,TEST_OBJ_TBL(
TEST_OBJECT_T(33,77),
TEST_OBJECT_T(66,67),
TEST_OBJECT_T(320,999)
)
);
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
insert into AAATABLE
VALUES (2,TEST_OBJ_TBL(
TEST_OBJECT_T(444,555),
TEST_OBJECT_T(333,67),
TEST_OBJECT_T(111,978)
)
);
(5) query 2 see results
select T1.custid ,T2.*
from AAATABLE T1 , table(T1. CUST_INFO) T2;
results :
CUSTID X Y
---------- ---------- ----------
1 33 77
1 66 67
1 320 999
2 444 555
2 333 67
2 111 978
(6) Create new table, which will be the data source for updating the nested table column)
create table new_tbl
(
X1 NUMBER,
Y1 NUMBER
);
(7) insert data into new table
insert into NEW_TBL values (222,444);
insert into NEW_TBL values (345,777);
insert into NEW_TBL values (867,222);
+++++++++++++++++++++++++++++++++
select * from NEW_TBL
X 1 Y1
---------- ----------
222 444
345 777
867 222
The question again: can I insert “new_tbl” data into nested table column (“cust_info “) at bulk (meaning - replace the data at stage 4
TEST_OBJECT_T(33,77),
TEST_OBJECT_T(66,67),
TEST_OBJECT_T(320,999)
)
I tried to use bulk collect but didn’t successes .
Thanks
keren
You can use the COLLECT function (with CAST)
update aaatable d
set d.cust_info = (select cast ( collect(TEST_OBJECT_T(x1,y1)) as TEST_OBJ_TBL ) from new_tbl)
where d.custid = 1;
Here is a sqlfiddle demo