I'm facing some problems while inserting and query data from the oracle SQL table. my table name is Table_Name
and it has two column names (id number, password varchar2). I make the inserting operation finely but when I query data then it as "1.14662E+10" this format.
so whats the problem is?
In SQL*Plus, You can set the format of the column to display the content of the column. For your case, You need something like this:
column your_column_name format 9999999999
The number of 9s in the above command should be the number of digits in your column's max value.
See below example(ROWNM column):
SQL> select * from T;
ROWNM NAME TOTAL COLUMN1
---------- ---------- ---------- ----------
1.2346E+17 Tejash ########## test
SQL> column rownm format 999999999999999999
SQL>
SQL> select * from T;
ROWNM NAME TOTAL COLUMN1
------------------- ---------- ---------- ----------
123456789123456789 Tejash ########## test
SQL>
The data is correctly stored and it's only a question of the way the client program (here SQL*Plus) is displaying the data.
As already answered you can use SQL*Plus formatting commands or use TO_CHAR function:
SQL> select x from t;
X
----------
1
1234567890
1.2346E+19
SQL> select to_char(x) from t;
TO_CHAR(X)
------------------------------------------------------------------------------------------------------------------------
1
1234567890
12345678901234567890
SQL>
One more trick for SQL*Plus (and SQLcl and SQL Developer) if you have several large number columns and don't want to set them all with column. There is a default display width for numbers, and beyond that size the formatting changes to scientific notation; but you can change that:
SQL> show numwidth
numwidth 10
SQL> select 12345, 123456789123456789 from dual;
12345 123456789123456789
---------- ------------------
12345 1.2346E+17
SQL> set numwidth 20
SQL> select 12345, 123456789123456789 from dual;
12345 123456789123456789
-------------------- --------------------
12345 123456789123456789
SQL>
It affects all number columns, even for smaller numbers, as you can see - so it's a broader approach and less targeted than individual column settings, but that can be useful sometimes.
You could also use set numformat to do more involved formatting, like adding group separators. Read more about set options in the documentation.
Related
I am just trying to understand some old code without help which uses this mybatis statement. I did not find any proper answer. So in the below statement of myBatis what is dual?
SELECT mySeq.currVal FROM dual
Wrong question, I'd say. DUAL is related to Oracle, not MyBatis.
It is a single-row, single-column table, owned by SYS. It looks like this:
SQL> desc dual
Name Null? Type
----------------------------- -------- --------------------
DUMMY VARCHAR2(1)
SQL> select * from dual;
DUMMY
-----
X
SQL>
To users, it is available as a public synonym:
SQL> select object_name, owner, object_type
2 from all_objects
3 where object_name = 'DUAL';
OBJECT_NAME OWNER OBJECT_TYPE
-------------------- -------------------- -------------------
DUAL SYS TABLE
DUAL PUBLIC SYNONYM
SQL>
How was it created and why does it have that strange name, "dual"?
Charles Weiss:
I created the DUAL table as an underlying object in the Oracle Data Dictionary. It was never meant to be seen itself, but instead used inside a view that was expected to be queried. The idea was that you could do a JOIN to the DUAL table and create two rows in the result for every one row in your table. Then, by using GROUP BY, the resulting join could be summarized to show the amount of storage for the DATA extent and for the INDEX extent(s). The name, DUAL, seemed apt for the process of creating a pair of rows from just one.
Where do we use it? Everywhere!!!
In Oracle's SQL, you have to select from something, and that's frequently dual. For example, which date is it today, or who am I?
SQL> select sysdate from dual;
SYSDATE
-------------------
04.08.2022 11:43:08
SQL> select 'My name is Littlefoot' who_am_i from dual;
WHO_AM_I
---------------------
My name is Littlefoot
SQL>
Or, in your case, you're selecting sequence's current value. Here's a demo:
Create the sequence:
SQL> create sequence myseq;
Sequence created.
Your query (won't work, though - see error description):
SQL> select myseq.currval from dual;
select myseq.currval from dual
*
ERROR at line 1:
ORA-08002: sequence MYSEQ.CURRVAL is not yet defined in this session
OK, so let's first fetch nextval:
SQL> select myseq.nextval from dual;
NEXTVAL
----------
1
Now we also have the currval:
SQL> select myseq.currval from dual;
CURRVAL
----------
1
SQL>
From the Oracle documentation:
DUAL is a table automatically created by Oracle Database along with the data dictionary. DUAL is in the schema of the user SYS but is accessible by the name DUAL to all users. It has one column, DUMMY, defined to be VARCHAR2(1), and contains one row with a value X. Selecting from the DUAL table is useful for computing a constant expression with the SELECT statement. Because DUAL has only one row, the constant is returned only once.
So that statement is querying the current value of the sequence mySeq, within your session (so there must have been a call to nextval preceding it for it to work).
In Oracle SQL you always have to select from something; dual is useful when there is no actual table you want to get data from - either for a constant or literal or function (select sysadte from dual) etc.
I have a column 'creation_date' which is of type 'date', when I am querying my table for distinct records based on 'creation_date' I am getting 6 records:
select distinct creation_date from test_table;
output:
06-APR-11
06-APR-11
28-MAR-11
06-APR-11
06-APR-11
18-MAR-11
In this output 6th April is displayed 4 times even when I used distinct in my query. Also when I am trying to find out all records which are matching with creation_date of 6th April 2011 I am not getting any results. Below is my query:
select * from test_table where creation_date = to_date('06-APR-11','DD-MON-YY');
Please help me where I am doing wrong in these two queries.
The problem is twofold. Firstly the dates almost definitely have time-components. to_date('06-MAR-11','DD-MON-YY') is equivalent to 2011/03/06 00:00:00. If you use the TRUNC() function you will be able to see everything for that day:
select *
from test_table
where trunc(creation_date) = to_date('06-MAR-11','DD-MON-YY');
I would not use the MON datetime format model. As I explain here it depends on your region and settings. It's safer to use a numeric month format model instead. Equally, always specify century as part of the year.
where trunc(creation_date) = to_date('06-03-YY11','DD-MM-YYYY');
Your second problem is almost definitely your NLS_DATE_FORMAT; it appears to not take into account the time, hence why you see 4 identical dates. This only governs the manner in which data is displayed not how it is stored.
You can change this using something like:
ALTER SESSION SET NLS_DATE_FORMAT = "DD/MM/YYYY HH24:MI:SS"
If I set up a test environment using the following:
create table test_table ( creation_date date );
insert into test_table values ( sysdate );
insert into test_table values ( sysdate - 0.01 );
alter session set nls_date_format = "YYYY/MM/DD";
You can see the data returned does not include time (though SYSDATE does):
SQL> select * from test_table;
CREATION_D
----------
2013/04/12
2013/04/12
Altering the NLS_DATE_FORMAT and performing the same SELECT, you now get a time component:
SQL> alter session set nls_date_format = "YYYY/MM/DD HH24:MI:SS";
Session altered.
SQL> select * from test_table;
CREATION_DATE
-------------------
2013/04/12 12:48:41
2013/04/12 12:34:17
Lastly, when trying to select today's date alone no rows will be returned:
SQL> select *
2 from test_table
3 where creation_date = to_date('20130412','yyyymmdd');
no rows selected
But, when using TRUNC() to compare on only the date portion of the field you get all your rows again:
SQL> select *
2 from test_table
3 where trunc(creation_date) = to_date('20130412','yyyymmdd');
CREATION_DATE
-------------------
2013/04/12 12:48:41
2013/04/12 12:34:17
To actually answer your second question, if you want unique dates you can re-use the TRUNC() function:
select distinct trunc(creation_date)
from test_table
The DATE datatype stores the year (including the century), the month, the day, the hours, the minutes, and the seconds (after midnight). You must also consider this.
We know that "Dual" is a temporary table which exactly contains 1 column whose name is "dummy" which is of "varchar2(1)" type that has 1 single row. The value of that record is "X". The varchar2 has size of 1 which means it should not allow more than a single character.
Now my question is: If it is of varchar2 type, then why it can hold any datatype temporarily AND If we insert characters more than 1 (size), how this is possible for it (dual) to accept it ? Example:
SQL> desc dual
Name Null? Type
------------------------------- -------- ----
DUMMY VARCHAR2(1)
SQL> select sysdate from dual;
SYSDATE
---------
22-JAN-13
SQL> select 5*5 result from dual;
RESULT
---------
25
SQL> select 'Ankita' as "Name" from dual;
Name
------
Ankita
Clearly "SYSDATE" is of DATE type (not varchar2), "RESULT" is of NUMBER type (not varchar2). Also 'Ankita' is more than 1 single character, i.e., 6 characters. This should contradict the structure that varchar2 is holding only 1, and supporting 6 too..
The types and sizes of columns only come into play when you extract the data from them. It's true that if you select the dummy column, it will only give you what is in that column.
But, when you're selecting something unrelated to the column data, such as 5*5 or 'Ankita', they have no such restrictions.
I think your misunderstanding stems from your snippet:
why it can hold any datatype temporarily AND If we insert characters more than 1 (size), how this is possible for it (dual) to accept it ?
The truth is, the column doesn't accept it. Selecting 5*5 does not attempt to place that data into the table somewhere, it just evaluates it as-is. It's no different to having a table with:
users:
id
name
and performing:
select 42, 3.14159, id, name from users
For every row selected from, you will see the constant values 42 and 3.14159, alongside the data extracted from the two specified columns.
I'm using Oracle 10g. I want to append # to all values in 'sal' column. To acomplish this first I'm trying to change data type of 'sal' column from numeric to varchar but getting following error
What am I doing wrong ?
You should use modify keyword instead of your second alter .
alter table
emp
modify
(
sal varchar2(10)
);
When modifying a tables column datatype , you need to use modify keyword.
Of course, you must deal with existing column data. When modifying a tables column datatype you may want to export the rows, redefine the table and then re-import you data.
In this you would need to follow these steps to alter a column data type:
Create the new column at the end of the table.
Run an update to populate the new table column
Drop the old table column
Re-name the new column to the original column name
It does not seem a smart idea to take a perfectly good number and ruin in for the rest of the user by appending a piece of string.
Just add the string on the select directly or through a view?
Something like:
SQL> create view emp_horked
as
select ename, sal, sal || '#' hash, to_char(SAL,'9999.99') || '#' sal_padded
from emp;
View created.
SQL> select * from emp_horked where rownum < 5;
ENAME SAL HASH SAL_PADDE
---------- ---------- ----------------------------------------- ---------
SMITH 800 800# 800.00#
ALLEN 1600 1600# 1600.00#
WARD 1250 1250# 1250.00#
JONES 2975 2975# 2975.00#
More on format models for to_char
I have a table
TableName: MACAddresses
Columns:
- Computer
- MACAddress
I would like to create a sql script that creates a new column and correctly formats the mac address with the colon (ie with Substring) - To create a new column called CorrectMAC
How would I do this with Oracle?
Here is some test data:
SQL> select * from MACAddresses
2 /
COMPUTER MACADDRESS
---------- ------------
100 123456789abc
200 acef35dd6ecc
SQL>
Adding the new column is quite straightforward:
SQL> alter table MACAddresses
2 add corrected_MACAddress varchar2(17)
3 /
Table altered.
SQL>
Note that you cannot make it NOT NULL at this point, because you already have some records in the table. So if you want to apply such a constraint, you need to populate it first.
This is the simplest way of updating the new column.
SQL> update MACAddresses
2 set corrected_MACAddress = substr(macaddress, 1,2)||':'||
3 substr(macaddress, 3,2)||':'||
4 substr(macaddress, 5,2)||':'||
5 substr(macaddress, 7,2)||':'||
6 substr(macaddress, 9,2)||':'||
7 substr(macaddress, 11,2)
8 /
2 rows updated.
SQL> select * from MACAddresses
2 /
COMPUTER MACADDRESS CORRECTED_MACADDR
---------- ------------ -----------------
100 123456789abc 12:34:56:78:9a:bc
200 acef35dd6ecc ac:ef:35:dd:6e:cc
SQL>
Now, if you had a more complicated pattern, or if you wanted to perform this operation on a regukar basis I suggest you expend the effort to turn it into a function, and perhaps remove that repetition at the same time.
Finally, if you wanted to enforce a mandatory constraint you can:
SQL> alter table MACAddresses
2 modify corrected_MACAddress not null
3 /
Table altered.
SQL>