On Oracle 11g, I ran the following code first:
alter session set nls_date_format = 'YYYY-MM-DD HH24:MI:SS';
create table table1(mytime timestamp);
Then I wanted to add a row to my table:
insert into table1(mytime)
values(TO_TIMESTAMP('2014-12-24 07:16:11'));
I also tried without TO_TIMESTAMP:
insert into table1(mytime)
values('2014-12-24 07:16:11');
both gave me the same error:
--A * here: ...values('2014...
-- *
ORA-01843: not a valid month
Why?
TO_TIMESTAMP( string1 [, fmt ] ['nlsparam'] ) The optional fmt
specifies the format of char. If you omit fmt, then char must be in
the default format of the TIMESTAMP datatype, which is determined by
the NLS_TIMESTAMP_FORMAT initialization parameter.
Here you can see that it is not nls_date_format but NLS_TIMESTAMP_FORMAT that you have to change.
Use;
alter session set NLS_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH24:MI:SS';
Then try to insert the data.
NOTE
Its always better to use TO_DATE and TO_TIMESTAMP with proper format like
TO_TIMESTAMP ('10-Sep-02 14:10:10.123000', 'DD-Mon-RR HH24:MI:SS.FF')
Or you can use the Date literals
like
TIMESTAMP '1997-01-31 09:26:50.124'
This should've been easily google-able.
But here you go:
insert into table1(mytime)
values(TO_TIMESTAMP('2014-12-24 07:16:11', 'YYYY-MM-DD HH:MI:SS'));
Related
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;
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.
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.
After years of using MySQL, having to move a table over to Oracle SQL (am using SQL Developer). Created the table and now just wanted to check it with a single INSERT statement, getting this:
INSERT INTO table_name
VALUES ('1001','LAWRENCE-INDIANAPOLIS','01-06-02','I8112NP','05DX8105408','2013-06-03','2016-03-11','2018-04-29','2038-01-01','yes','yes','yes','2012-10-25','CCE','7360D33','R8NR6N0','70F63951959F9','2016-03-11')
Error report -
SQL Error: ORA-01861: literal does not match format string
01861. 00000 - "literal does not match format string"
*Cause: Literals in the input must be the same length as literals in
the format string (with the exception of leading whitespace).
If the "FX" modifier has been toggled on, the literal must match exactly,
with no extra whitespace.
*Action: Correct the format string to match the literal.
Do I really have to put a TO_DATE format in front of every single DATE field in the INSERT statement, even though they're already in the proper YYYY-MM-DD format?
Well, you don't have to do it every time, but good practice says that you should instruct Oracle what you have and what you expect of it to do.
It is about NLS settings. If date format is different from one you use, you'll get an error (as you already know it):
SQL> create table test (datum date);
Table created.
SQL> insert into test values ('2013-06-03');
insert into test values ('2013-06-03')
*
ERROR at line 1:
ORA-01861: literal does not match format string
But, if you modify date format so that it matches format you use, everything will be OK:
SQL> alter session set nls_date_format = 'yyyy-mm-dd';
Session altered.
SQL> insert into test values ('2013-06-03');
1 row created.
SQL>
Another option, which doesn't depend on NLS settings, is to use DATE literal. It is always in format YYYY-MM-DD and has to be preceded by the DATE keyword:
SQL> alter session set nls_date_format = 'dd.mm.yyyy';
Session altered.
SQL> insert into test values ('2013-06-03');
insert into test values ('2013-06-03')
*
ERROR at line 1:
ORA-01861: literal does not match format string
SQL> insert into test values (date '2013-06-03');
1 row created.
SQL>
Or, as you found out, use TO_DATE function with the appropriate format mask.
In Oracle, use the date keyword:
INSERT INTO table_name
VALUES ('1001', 'LAWRENCE-INDIANAPOLIS', '01-06-02', 'I8112NP', '05DX8105408',
DATE '2013-06-03', DATE '2016-03-11', DATE '2018-04-29', DATE '2038-01-01', 'yes', 'yes', 'yes', DATE '2012-10-25', 'CCE', '7360D33', 'R8NR6N0', '70F63951959F9', DATE '2016-03-11'
);
The default DATE format used depends on the setting of the NLS_DATE_FORMAT environment var. To find out what this is, use the following query:
SELECT *
FROM V$NLS_PARAMETERS
ORDER BY PARAMETER;
This will dump all of the NLS parameters as they are currently set in your database. As an example, in the database I'm working in right now the query above returns:
NLS_CALENDAR GREGORIAN
NLS_CHARACTERSET US7ASCII
NLS_COMP BINARY
NLS_CURRENCY $
NLS_DATE_FORMAT DD-MON-RR
NLS_DATE_LANGUAGE AMERICAN
NLS_DUAL_CURRENCY $
NLS_ISO_CURRENCY AMERICA
NLS_LANGUAGE AMERICAN
NLS_LENGTH_SEMANTICS BYTE
NLS_NCHAR_CHARACTERSET AL16UTF16
NLS_NCHAR_CONV_EXCP FALSE
NLS_NUMERIC_CHARACTERS .,
NLS_SORT BINARY
NLS_TERRITORY AMERICA
NLS_TIMESTAMP_FORMAT DD-MON-RR HH.MI.SSXFF AM
NLS_TIMESTAMP_TZ_FORMAT DD-MON-RR HH.MI.SSXFF AM TZR
NLS_TIME_FORMAT HH.MI.SSXFF AM
NLS_TIME_TZ_FORMAT HH.MI.SSXFF AM TZR
So on my system, if I wanted to enter a date-as-character string and have Oracle translate it correctly I'd have to enter is as e.g. 24-Apr-18.
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