How to add date and time in Oracle - sql

I have the following details:
date_time: 08-Dec-14 07:52:52 along with other fields.
I insert using the following:
insert into employee (DATE_TIME, NAME, EMPLOYEE_ID)
values(TO_DATE('08-Dec-14 07:52:52','DD-MON-YY HH24:MI:SS'), 'XYZ', 123);
When I query the database for a return of thedate_time, I get only 08-Dec-14 and not the time. I have set this field as DATE.
What changes need to be made so I can get the time as well?
Thanks in advance.

use select query like below
select to_char(date_time,'Required date format') from employee.
Required date format: 'DD-MON-YY HH24:MI:SS' or'DD/MM/YYYY' etc.

When I query the database for a return of the date_time, I get only 08-Dec-14 and not the time. I have set this field as DATE.
It is a display format which your client is using. it depends on the locale-specific NLS date settings.
You can override the local NLS settings using TO_CHAR at individual query level.
For example,
SQL> alter session set nls_date_format='DD-MON-YYYY';
Session altered.
SQL> SELECT sysdate FROM dual;
SYSDATE
-----------
24-JUL-2015
Using TO_CHAR to override the NLS dependent format:
SQL> SELECT to_char(sysdate, 'MM/DD/YYYY HH24:MI:SS') dt FROM dual;
DT
-------------------
07/24/2015 12:21:01

Related

Set NLS_DATE_FORMAT in oracle 11g to truncate time if it is not present

I have a SELECT query in PLSQL that contains multiple DATE columns and some of them also include TIME in it. The date and time must be displayed in standard format i.e; DD/MM/YYYY HH:MI:SS AM So before executing the query I set NLS_DATE_FORMAT parameter:
ALTER SESSION SET NLS_DATE_FORMAT = 'DD/MM/YYYY HH:MI:SS AM';
The problem here is that those columns which only have a DATE in it (without TIME) also shows a default time in result set i.e; 12:00:00 AM
Is there a way in oracle to setup NLS_DATE_FORMAT param in way that it truncates default time if it isn't present in DATE filed?
Note that I am aware of methods like TO_CHAR and TRUNC to achieve the desired results but in my case I can't use these because it will affect data sorting in my application.
There is no way to achieve this by NLS_SETTINGS. NLS_DATE_FORMAT specifies the default date format to use with the TO_CHAR and TO_DATE functions and applies to each and every single column. The default value of this parameter is determined by NLS_TERRITORY.
The value of this parameter can be any valid date format mask, and the value must be surrounded by double quotation marks.
Oracle always stores the time, but it will show it as long as the date format specified provides a date/time mask
Example
SQL> create table mytest ( c1 date ) ;
Table created.
SQL> insert into mytest values ( to_date ( '22/07/2020 11:25:00 AM' , 'DD/MM/YYYY HH:MI:SS AM' ) ) ;
1 row created.
SQL> insert into mytest values ( to_date ( '22/07/2020' , 'DD/MM/YYYY' ) ) ;
1 row created.
SQL> select * from mytest ;
C1
---------
22-JUL-20
22-JUL-20
SQL> select value from nls_database_parameters where parameter = 'NLS_DATE_FORMAT' ;
VALUE
--------------------------------------------------------------------------------
DD-MON-RR
SQL> alter session set nls_date_format = 'DD/MM/YYYY HH:MI:SS AM';
Session altered.
SQL> select * from mytest ;
C1
----------------------
22/07/2020 11:25:00 AM
22/07/2020 12:00:00 AM
SQL>
You have to apply some function ( TRUNC or TO_CHAR ) in order to achieve what you want. Oracle always store the time.
Oracle dates (as long as we're talking about actual dates and not just text) always have a time component.
I suspect the confusion arouses because you can feed DATE columns with texts that contain dates but not time. What really happens here is that ANSI date literals and the TO_DATE() function will just store a default value for missing components. In this case, midnight:
create table test (
id int not null,
value date not null,
primary key (id)
);
insert into test (id, value) values (1, date'2020-07-22');
insert into test (id, value) values (2, to_date('2020-07-22', 'YYYY-MM-DD'));
select id, value as default_format, to_char(value, 'YYYY-MM-DD HH24:MI:SS') as custom_format
from test;
(Demo)
If you really need it you can write a custom formatting function that checks if time is midnight and omits it, but of course there's just no way to tell whether the value was the default one or it's part of actual data explicitly entered that way.
This is easily obtained by specifying the the desired date format and NOT relying on nls_date_format. Further you can conditionally format the result yes sort date/time correctly.
with x (date_col) as
( select trunc(sysdate) from dual union all
select sysdate - 1 from dual
)
select
case when to_char(date_col, 'hh24:mi:ss') = '00:00:00'
then to_char(date_col, 'yyyy-mm-dd')
else to_char(date_col, 'yyyy-mm-dd hh:mi:ss am')
end date_col
from x
order by date_col;

How do I fix ORA-01843: not a valid month?

So at the query level, I have it:
to_char(
(
to_date(
substr(TIMESTAMP, 1, 19),
'yyyy-mm-dd hh24:mi:ss'
)
),
'dd-mon-yyyy hh24:mi:ss'
) as DateTime,
And I've tried looking at a few articles with one most notable:
How to change the date format in Oracle BI Publisher?
I have also tried using:
and trunc(TIMESTAMP) between :FROM_DATE AND :TO_DATE
--and also
and trunc(TIMESTAMP) between to_date(:FROM_DATE, 'yyyy-MM-dd') AND to_date(:TO_DATE, 'yyyy-MM-dd')
While going through structure and XML I noticed my date is in string format:
element name="DATETIME" value="DATETIME" label="DATETIME" dataType="xsd:string" breakOrder="ascending" fieldOrder="3"
So I removed the to_char to get the date format
The error I've been getting is:
java.sql.SQLDataException: ORA-01843: not a valid month
How do I fix this issue?
EDIT:
Format for the column, TIMESTAMP, the format is CHAR(14)
Example of values is like 20200701103038
It runs perfectly in SQL Developer
Well, it is quite a bad and extended practice to store DATES as strings, either using varchar2 or char. Anyway, having say that, I think you have a problem with your settings or the way you are constructing your query:
SQL> alter session set nls_date_format='YYYYMMDDHH24MISS' ;
Session altered.
SQL> select to_date('20200726123722') from dual ;
TO_DATE('20200
--------------
20200726123722
SQL> select sysdate from dual ;
SYSDATE
--------------
20200726124622
Besides, as you said, if your data is stored as YYYYMMDDHHMISS, you are applying the wrong date mask YYYY-MM-DD HH24:MI:SS to that char. I would use CAST to define the field as DATE.
Example
SQL> create table my_test ( c1 char(20) ) ;
Table created.
SQL> insert into my_test values ('20200726123722') ;
1 row created.
SQL> insert into my_test values ('20200725123722') ;
1 row created.
SQL> commit ;
Commit complete.
SQL> alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss';
Session altered.
SQL> select cast(c1 as date) from my_test ;
CAST(C1ASDATE)
-------------------
2020-07-26 12:37:22
2020-07-25 12:37:22
SQL>
Update
If you can't change your NLS Session settings, then you must apply a TO_CHAR to the resulting output. But in your case, you want to operate with dates, so as long as it is a date value you want to operate with, you can forget about the mask.
SQL> col value for a20
SQL> select value from nls_database_parameters where parameter = 'NLS_DATE_FORMAT' ;
VALUE
--------------------
DD-MON-RR
SQL> select cast(to_date('20200725123722','YYYYMMDDHH24MISS') as date) from dual ;
CAST(TO_D
---------
25-JUL-20
SQL> select to_char( cast(to_date('20200725123722','YYYYMMDDHH24MISS') as date) , 'YYYYMMDDHHMISS' ) from dual ;
TO_CHAR(CAST(T
--------------
20200725123722
SQL> select case when cast(to_date('20200725123722','YYYYMMDDHH24MISS') as date) > sysdate
2 then 'FALSE'
3 else
4 'TRUE'
5 end as result from dual ;
RESUL
-----
TRUE
SQL>
So, if you want to compare the date to another date, don't use to_char. If you want to show the value in a specific format, when you have no option to change the settings, then use to_char.
Just to make sure what SYSDATE (I'm going to select) represents:
SQL> alter session set nls_Date_format = 'dd.mm.yyyy';
Session altered.
Today is:
SQL> select sysdate from dual;
SYSDATE
----------
26.07.2020
This is the way to get the error you got: apply wrong format mask to a string which represents a DATE value:
SQL> select to_Date('2020-27-07', 'yyyy-mm-dd') from dual;
select to_Date('2020-27-07', 'yyyy-mm-dd') from dual
*
ERROR at line 1:
ORA-01843: not a valid month
SQL>
How to fix it? Usually, it is hard to fix it if dates are represented as strings. They (strings that represent dates) are like a box of chocolates, you never know what you're gonna get. If there's at least one wrong value, query will fail.
How to find wrong values? You could create a function which returns TRUE (or 1 or whatever you want) if a string you pass to it represents a valid date format. But, if you pass 01/02/03, which is which? Different formats match (e.g. dd/mm/yy, yy/mm/dd, mm/yy/dd ...). Worse cases are 84/25/32 or AB/23/2f. They are all strings, they "match" two characters separated by slash but certainly aren't valid dates, so you can't rely on a simple regular expression.
Shortly, there's no easy nor fast way out of it.

Inserting a TIMESTAMP value in SQL Developer

Hello I'm having a trouble inserting this value in Oracle SQL Developer Version 19.4.0.354.
'2013-01-01 00:00:00.0000'
Here is the value that I want to insert in one of my tables.
I tried DATE and TIMESTAMP data types but they don't work. I also tried altering the sessions and other possible solutions all over the internet.
Column datatype should be TIMESTAMP. Use appropriate format mask in TO_TIMESTAMP function.
SQL> create table test (col timestamp);
Table created.
SQL> insert into test (col) values (to_timestamp('2013-01-01 00:00:00.0000', 'yyyy-mm-dd hh24:mi:ss:ff6'));
1 row created.
What's in there? (alter session is here just to display the result in desired format; it doesn't affect the value stored in that column):
SQL> alter session set nls_timestamp_format = 'dd.mm.yyyy hh24:mi:ss.ff6';
Session altered.
SQL> select * From test;
COL
---------------------------------------------------------------------------
01.01.2013 00:00:00.000000
SQL>
The simplest approach is to use a literal timestamp, so you don't rely on implicit conversion:
select timestamp '2013-01-01 00:00:00.0000' from dual;
You can also use to_timestamp():
select to_timestamp('2013-01-01 00:00:00.0000', 'yyyy-mm-dd hh24:mi:ss:ff4') from dual;
On the other hand, if you really want to work with a string, then you would need to change the nls setting to your own format before:
alter session set nls_timestamp_format = 'yyyy-mm-dd hh24:mi:ss:ff4';
You can then use pass your string value directly in the query, and Oracle will implicitly convert it to the target datatype while inserting.
You have no time component, so I would just use:
date '2013-01-01'
This is the safest syntax (and clearest and standard too!) for providing a date value.
If Oracle wants a timestamp, it will convert this correctly.

how i can format a column

i want to format a column in oracl 10g
its type is date
I want to show me always only hours and minutes
any solution with alter ..
thanks
If you want to show only hours / minutes from a timestamp field, you can use:
select to_char( sysdate, 'HH-MI' ) as sysdate_format from dual;
Date/Time Formatting in Oracle
Suppose this is what you have now:
SQL> alter session set nls_date_format = 'ddmmyy';
Session altered.
SQL> select sysdate from dual;
SYSDAT
------
131019
One option is to use TO_CHAR:
SQL> select to_char(sysdate, 'dd.mm.yyyy hh24:mi:ss') from dual;
TO_CHAR(SYSDATE,'DD
-------------------
13.10.2019 08:46:16
SQL>
but it requires you to always use it; another is to alter session and set desired format mask (which is probably what you want):
SQL> alter session set nls_date_format = 'dd.mm.yyyy hh24:mi:ss';
Session altered.
SQL> select sysdate from dual;
SYSDATE
-------------------
13.10.2019 08:45:50
SQL> select min(hiredate) from emp;
MIN(HIREDATE)
-------------------
17.12.1980 00:00:00
SQL>
To show just the time component of a date, you need to convert the value to a string.
For the hours in a 24-hour format, you can use:
select to_char(sysdate, 'HH24:MI')
from dual;
Of course, sysdate is a built-in date value. You would use whatever value or column you desire.

How to get time string to Time in oracle sql

I have insert time in form of string in oracle VARCHAR2 column. But when I try to retrieve it in form of time it's not giving me right time, it's only giving me a date which I have not saved.
INSERT INTO table1
(timestr) Select substr(numtodsinterval(MAX(date1)-MIN(date2),'day'),
12,8) from table2 where ....; // stored timestr column value: 00:00:00
Retrieve ...
select TO_DATE(timestr,'hh24:mi:ss') from table1;
... is only giving 10/01/2015
You should use to_char to see the time
select to_char(timestr,'hh24:mi:ss') from table1;
That might be because of your client setting. If you are using SQL Developer, then go to Tools->Preference->Database->NLS and change setting to view timestamp also.
If you are using SQLPlus, the change nls_date_format or see below solution.
https://community.oracle.com/thread/312115?start=0&tstart=0
In Oracle there is no concept of a TIME datatype. There are only DATE and TIMESTAMP, both of which comprise date and time elements.
Your retrieval casts your time column to a DATE: TO_DATE(timestr,'hh24:mi:ss'). Oracle does what we ask it to do, so it displays a date. As there is no date element in timestr it assigns one for us: today's date.
You are only seeing 10/01/2015 because your client's NLS settings are configured to display only the date element. Change the setting to show the time element, in the client or by altering the session as here:
SQL> select to_date('10:08:23', 'hh24:mi:ss') ts from dual
2 /
TS
---------
01-OCT-15
SQL> alter session set nls_date_format='DD-MON-YYYY HH24:MI:SS'
2 /
Session altered.
SQL> select to_date('10:08:23', 'hh24:mi:ss') ts from dual
2 /
TS
--------------------
01-OCT-2015 10:08:23
SQL>
If you only want to see the time you can change the NLS settings ...
SQL> alter session set nls_date_format='HH24:MI:SS'
2 /
Session altered.
SQL> select to_date('10:08:23', 'hh24:mi:ss') ts from dual
2 /
TS
--------
10:08:23
SQL>
...but that will get annoying ....
SQL> select sysdate from dual
2 /
SYSDATE
--------
23:59:52
SQL>
So why not just display the column as a string all without casting to a DATE?
select timestr from table1;