Question

In my Oracle 11 database, I have a sql script for inserting(migrating) data from one table into another, this is a problem I encounter a lot, for instance:

INSERT INTO TargetTable (eventname, eventplace, eventdate)
SELECT  name, eventplace, "How to insert time here?"   FROM SourceTable;

The SourceTable don't have the eventdate column, and it is "Not Nullable" in TargetTable, so I have to insert something there, in this case I just want to just use the current time for all rows. How can I do this?

p.s Not only for dates, This could also be a missing varchar column and I might to set a default value in target database.

Was it helpful?

Solution 2

For date fields just use sysdate, for varchar fileds I think you have done right, just add "default value" after select works.

OTHER TIPS

Try this:

INSERT INTO TargetTable (
  eventname, 
 eventplace, 
  eventdate)
 SELECT name, 
        eventplace, 
        SysDate -- <- Current Date and Time
   FROM SourceTable;

modify the table definition like this:

create table table_name
(
col1 varchar2(10),
default_date_col date default sysdate
);

this way u dont need to include the date column in the insert in case u dont need it.

If you Want date format into varchar2 column use

 TO_CHAR(SYSDATE,'YYYY.MM.DD HH24:MI:SS');

You can play with functions how you like

example

TO_DATE(TO_CHAR(2013) || TO_CHAR(SYSDATE,'.MM.DD HH24:MI:SS'), 'YYYY.MM.DD HH24:MI:SS');

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top