Skip to Main Content

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Karthik.

Asked: December 05, 2016 - 9:13 pm UTC

Last updated: December 06, 2016 - 9:30 am UTC

Version: 11

Viewed 10K+ times! This question is

You Asked

I have a table in which one of the column is as below

create_date TIMESTAMP(6) default SYSDATE,

When the record is inserted from the GUI(URL) , it doesn't insert any value for create_Date .. But when inserted from other than URL it is inserted with default sysdate

Why is this happening ?How to resolve it

and Connor said...

Defaults are used when you do NOT specify a value.

If the application is providing a NULL value, then we will *use* that, eg

SQL> create table t ( y int, x date default sysdate );

Table created.

SQL>
SQL> insert into t (y) values (1);

1 row created.

SQL> insert into t values (2, date '2000-01-01');

1 row created.

SQL> insert into t values (3,null);

1 row created.

SQL> insert into t values (4,default);

1 row created.

SQL>
SQL> select * from t;

         Y X
---------- ---------
         1 06-DEC-16
         2 01-JAN-00
         3
         4 06-DEC-16


Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Joseph Charpak, December 06, 2016 - 4:38 pm UTC

On 12c when creating or adding the column you can use the "ON NULL" construct:

create table t (
   y varchar2(100),
   z number default ON NULL 42
);

insert into t (y,z)
values ( 'inserting null but expecting to see 42', null );

select * from t;
Y                                       Z
—————————————————————                   ————
inserting null but expecting to see 42  42


Adapted from:
http://www.oracle.com/technetwork/issue-archive/2013/13-sep/o53asktom-1999186.html