how to insert date values in dbms? - oracle

I am using oracle dbms and created a table. One of the attributes of the table is of type date. Even after entering in YYYY-MM-DD format it still shows error ORA-01843.
This is my query
insert into reserves values(120,001,'2012-01-11');
This is the error
ORA-01843: not a valid month
ORA-06512: at "SYS.DBMS_SQL", line 1721
insert into reserves values(120,001,'2012-01-11');
I tried inserting month names and also rearranging the date, still no success.
I am trying to insert this set of values

You better tell the DB the format of the date string, like this
insert into reserves values(120,001,to_date('2012-01-11','yyyy-mm-dd');
You can also set the NLS_DATE_FORMAT like this
alter session set nls_date_format='yyyy-mm-dd';
(or whatever format you like) and then use the syntax you show in the question

insert into reserves values(120,001,DATE'2023-01-25');

Related

oracle Alter Session NLS FORMAT for bulk insert statements - not behaving as expected

I have exported out table information from an SQL DB in the format of insert statements. Many of the tables contain timestamp information the YYYY-MM-DD HH24:MI:SS format. Since there are hundreds of these statements it is not realistic for me to add the TO_DATE() statement with each date. I thought that altering the sessions NLS DATE format would resolve this issue, however I'm still getting an error about ORA-01843: not a valid month.
The columns data type is TIMESTAMP.
EXAMPLE:
ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS'
INSERT INTO gcGovernance (id, userID, grantAppID, grantAppUUID,
grantCommCatID, grantApprovalCmnt, grantApprovalDate) VALUES (758, 163,
408, 'iahfahfahashvai', 0, '', '2016-12-20
14:32:17');
If the receiving column is a TIMESTAMP, then you need to set NLS_TIMESTAMP_FORMAT, not NLS_DATE_FORMAT.
You can try to use the format timestamp'YYYY-MM-DD HH24:MI:SS'
within that example insert statement such as
timestamp'2016-12-20 14:32:17' for grantApprovalDate column.
Demo

Oracle- save dates with correct centuryinfo

I am trying to insert nvarchar2 values into my oracle Date field. When my source nvarchar2 value is '20701130', I want to save it as '30/11/2070' in my oracle column. And, if the source value is '21150529', I want to save it as '29/05/2115'.
I tried different formatting options to achieve this, but the year is always getting saved as two digits and is not indicating the correct century.
One of my Queries:
INSERT INTO MY_TABLE (MY_DATE_FIELD)
VALUES(TO_DATE('21061030', 'RRRR-MM-DD'));
Now, when I select after the above query, I get the result as 30/10/06 which is not we want, it should be '30/10/2106'.
You can go about it in many ways:
You alter the session date format itself to a format you want to achieve like :
ALTER SESSION SET NLS_DATE_FORMAT = 'RRRR-MM-DD';
Another approach is once you have achieved a date from a string passed using to_date then you can again add to_char to display the date in a format you want.
To_date will convert your string to a date using the format you have provided but while selecting it will still display the date as per the format provided in the session that's why we use To_char to change it to a format we want to display it into.
select to_char(TO_DATE('21061030', 'RRRR-MM-DD'),'RRRR-MM-DD') "DATE" from dual;
Thanks for the help.
I have pasted my SQL below if anyone is interested.
-- To insert into table
UPDATE MY_TABLE SET MY_DATE_FLD = TO_CHAR(TO_DATE('21061030', 'RRRR-MM-DD'),'DD-MM-YYYY') ;
-- To read from the table, this gave me the result 30/01/2106
select TO_CHAR(DATE_FLD,'DD/MM/YYY') AS START_DATE from MY_TABLE;
Appreciate the help.
Kiran.

How to save date in 'YYYY/MM/DD' format in Oracle despite NLS_DATE_FORMAT is set as 'DD-MON-YY'

Currently in my database
nls_date_format = 'DD-MON-YY'
But I want to save date in YYYY/DD/MM format .
Is it possible to save data in YYYY/DD/MM format because when I do
insert into tab1(name) values to_date(sysdate,'YYYY/DD/MM') ;
it shows below error:
Error report -
SQL Error: ORA-03001: unimplemented feature
03001. 00000 - "unimplemented feature"
*Cause: This feature is not implemented.
*Action: None.
Altering the session works ,for eg
alter session set nls_date_format = 'YYYY/DD/MM' ;
insert into tab1(name) values to_date(sysdate) ;
select * from tab1;
2017/31/05
Is there any way to save the data without altering session .so that data gets saved in table in 'YYYY/MM/DD' format instead of NLS_DATE_FORMAT ?
Thanks in Advance
you can not save date format in date column.
you can write your date like to_char(your_date,'YYYY/DD/MM') to column which data type is varchar.
when you read data you can use to_date function like to_date(your_value,'YYYY/DD/MM')
DATE and TIMESTAMP columns don't have any specific format. You can display your own format. you want to set default format means you can set NLS setting.

Oracle - How to Convert VARCHAR to DATETIME format

I am using Oracle10g database in which a table contains a Column with Date DataType. I am using the following query to get the record:
select to_char(START_TIME, 'YYMMDD HH24:MI:SS') from table;
So from above query, the result will be of type VARCHAR. I have tried to_Date() method but resulted in displaying only DATE. Can i convert VARCHAR to DATETIME format? The result should be of type DATETIME. Please help me how to resolve this problem.
an Oracle date contains both date and time so you can consider it a datetime (there is no datatype of datetime in Oracle). how is DISPLAYS when you select it is entirely up to your client. the default display setting is controlled by the NLS_DATE_FORMAT parameter. If you're just using the date in your pl/sql block then just assign it into a date datatype and select into that variable without to_char and it will work just fine and contain whatever time component is present in your table.
to control the display, for example using nls_date_format:
SQL> select a from datetest;
A
---------
19-FEB-13
SQL> alter session set nls_date_format='YYMMDD HH24:MI:SS';
Session altered.
SQL> select a from datetest;
A
---------------
130219 07:59:38
but again, this is only for display.
Oracle's Date type fields contain date/time values, therefore converting it to Datetime does not make any sense (it's already datetime)
Read more about oracle date types here
Yeah the Date datatype will meet your needs but you will have to jump through some hoops every time to get the exact time out of it. Definitely use the Timestamp datatype.

Oracle insert datetime wrong

In the oracle, I have field 'time_entered' as date type, I insert a current date time like this, it will give me error. What is the current syntax ? especially I want to insert the time under my timezone, not Oracle server timezone.
Thanks.
insert into mytbl( TIME_ENTERED)
values( SYSDATE);
error :
SQL Error: ORA-00984: column not allowed here
00984. 00000 - "column not allowed here"
Two possibilities-
You are typing the column name TIME_ENTERED wrong or there exists a column called SYSDATE in the table (which Oracle Shouldn't allow anyways).
mytbl is a PLSQL Table rather than a database Table.
You can change your timezone with something like:
ALTER SESSION SET TIME_ZONE = '-2:00';
Then you can use:
insert into mytbl( TIME_ENTERED) values( SYSDATE);
But be sure that there is no such column on mytbl named SYSDATE.

Resources