Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting system date in oracle

Tags:

oracle

How to insert system date in dd/mm/yyyy to table using oracle 10g?

While using the following query, it inserts system date as 03/04/0013. I need 03/04/2013. Can you help me to solve this problem?

insert into GtTable 
values('111','Name',300,'Tour',to_date('02/04/2012','DD/MM/YYYY'),to_date(sysdate,'DD/MM/YYYY'));

But when inserting '02/04/2012' directly, it accepts as same as '02/04/2012'.

like image 466
user2238881 Avatar asked Apr 03 '13 05:04

user2238881


People also ask

How do I insert date in Oracle?

The TO_DATE function allows you to define the format of the date/time value. For example, we could insert the '3-may-03 21:02:44' value as follows: insert into table_name (date_field) values (TO_DATE('2003/05/03 21:02:44', 'yyyy/mm/dd hh24:mi:ss')); Learn more about the TO_DATE function.

Can we use Sysdate in insert query?

How Can I Insert SYSDATE In Oracle? To insert a SYSDATE value in Oracle, you can just insert the SYSDATE function into a DATE column. This is the ideal method because both the SYSDATE and the DATE column are in a date datatype. There's no need to convert when inserting into the table.

How do I create a system date in SQL?

To insert only date value, use curdate() in MySQL. With that, if you want to get the entire datetime, then you can use now() method. Insert both date and time with the help of now().

How do I display a date in YYYY-MM-DD format in Oracle?

Just use: select to_date(date_value, 'yyyy-mm-dd') as date_value from table; To convert to a date. That's it!


3 Answers

If the last field on your insert is a date type field, you should not need any conversion on SYSDATE, so, the following should be OK:

insert into GtTable 
values('111', 'Name', 300, 'Tour', to_date('02/04/2012','DD/MM/YYYY'), sysdate);

But if it is a varchar field, the following should work:

insert into GtTable 
values('111', 'Name', 300, 'Tour', to_date('02/04/2012','DD/MM/YYYY'), to_char(sysdate, 'dd/mm/yyyy'));
like image 86
warantesbr Avatar answered Oct 21 '22 22:10

warantesbr


You shouldn't activate TO_DATE on a date

sysdate is already a date, when you run TO_DATE with it as the first parameter, you make Oracle implicitly convert it to a string according to NLS_DATE_FORMAT which in your case probably contains YY and not YYYY.

A date in oracle is a number representing a date and time, it doesn't have a "format", if you want to insert sysdate without the time value you need to truncate it like this:

insert into GtTable 
values('111','Name',300,'Tour',to_date('02/04/2012','DD/MM/YYYY'),trunc(sysdate))
like image 45
A.B.Cade Avatar answered Oct 21 '22 22:10

A.B.Cade


I think the format of displaying is due to the environment parameter。 TO_CHAR(SYSDATE,'DD/MM/YYYY') CAN LET YOU inserts system date as DD/MM/YYYY ,but, it is not date type anymore.

like image 39
Gentlezerg Avatar answered Oct 21 '22 20:10

Gentlezerg