For date fields just use sysdate, for varchar fileds I think you have done right, just add "default value" after select works.
Oracle Sql: How to set a default column value on insert select
-
20-06-2023 - |
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.
Solution 2
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');