create before insert trigger to add sysdate + 30 minutes in sql - sql

i have table with timestamp column
i want to add on each insert
to add the current timestamp + 30 minutes
how to achieve that
im using oracle 11g;

As you have a timestamp column it's probably better to use systimestamp instead of sysdate, and add an interval rather than a fraction of a day - which would lose the fractional second precision (and time zone, if your column actually stores that too).
You would either just have a date (if you use sysdate + 30/1440) or implicitly convert to a date (if you use systimestamp + 30/1440); and either way will you'll end up with a date that is then implicitly or explicitly converted to a timestamp as it's stored in your column.
As a simple example of using an interval:
create table t42 (col1 number, col2 timestamp);
create trigger tr42
before insert on t42
for each row
begin
:new.col2 := systimestamp + interval '30' minute;
end;
/
select systimestamp from dual;
SYSTIMESTAMP
---------------------------------
2019-02-13 07:17:11.971542000 GMT
insert into t42 (col1) values (42);
select col1, col2 from t42;
COL1 COL2
---------- -----------------------------
42 2019-02-13 07:47:12.253603000
You could also use a default value for the column instead of a trigger:
create table t42 (
col1 number,
col2 timestamp default systimestamp + interval '30' minute
);
select systimestamp from dual;
SYSTIMESTAMP
---------------------------------
2019-02-13 07:17:12.962268000 GMT
insert into t42 (col1) values (42);
select col1, col2 from t42;
COL1 COL2
---------- -----------------------------
42 2019-02-13 07:47:13.028670000
although that does allow the person doing the insert to provide their own value:
insert into t42 (col1, col2) values (43, timestamp '2000-01-01 00:00:00.0');
select col1, col2 from t42;
COL1 COL2
---------- -----------------------------
42 2019-02-13 07:47:13.028670000
43 2000-01-01 00:00:00.000000000
The trigger would override any user-supplied value (though it could also be modified not to.)
You could also use current_timestamp instead of systimestamp - they do slightly different things.

you can create a trigger as shown below. this will insert current timestamp+30 minutes each time you insert a row to the table.
create or replace trigger before_insert
before insert
on table_name for each row
declare
v_time date;
begin
select sysdate+30/1440 into v_time from dual;
:new.cur_time :=v_time;
end;
/

Related

convert epoch time into normal datetime format in Oracle SQL

Well, I am going to convert the epoch into a normal datetime in oracle sqldeveloper, I wrote the below code, but it says "missing expression"
My code:
SELECT to_date(CreationDate, 'yyyymmdd','nls_calendar=persian')+ EpochDate/24/60/60
from table1
My table1:
ID
EpochDate
100
16811048
101
16810904
102
12924715
103
15667117
I don not know what is wrong!
If the CreationDate is a Date and EpochDate is a Varchar you can try this:
SELECT to_date(to_char(CreationDate, 'yyyymmdd','nls_calendar=persian'),'yyyymmdd') +
EpochDate/24/60/60 as newDate
from table1
or:
select to_date(to_char(CreationDate, 'yyyymmdd','nls_calendar=persian'),'yyyymmdd') +
numtodsinterval(EpochDate,'SECOND') as newDate
from dual
Let's show you how in an example
Demo data
SQL> create table c1 ( id number generated always as identity, date_test date) ;
Table created.
SQL> insert into c1 ( date_test ) values ( sysdate ) ;
1 row created.
SQL> insert into c1 ( date_test ) values ( sysdate-365 ) ;
1 row created.
SQL> insert into c1 ( date_test ) values ( sysdate-4000 ) ;
1 row created.
SQL> insert into c1 ( date_test ) values ( sysdate-7200 ) ;
1 row created.
SQL> commit ;
Commit complete.
Now, let's add a column called epoch, and a small function to make easier to update the column.
SQL> alter table c1 add epoch number ;
Table altered.
SQL> create or replace function date_to_unix_ts( PDate in date ) return number is
l_unix_ts number;
begin
l_unix_ts := ( PDate - date '1970-01-01' ) * 60 * 60 * 24;
return l_unix_ts;
end;
/
Function created
We update the column epoch with the real epoch date based on the timestamp field
SQL> update c1 set epoch=date_to_unix_ts (date_test) ;
4 rows updated.
SQL> select * from c1 ;
ID DATE_TEST EPOCH
---------- ---------------------------------------- -----------------
1 2021-09-15 12:25:25 1631708725
2 2020-09-15 12:25:25 1600172725
3 2010-10-03 12:25:25 1286108725
4 2001-12-29 12:25:26 1009628726
SQL> select to_char(to_date('1970-01-01','YYYY-MM-DD') + numtodsinterval(EPOCH,'SECOND'),'YYYY-MM-DD HH24:MI:SS') from c1 ;
TO_CHAR(TO_DATE('19
-------------------
2021-09-15 12:25:25
2020-09-15 12:25:25
2010-10-03 12:25:25
2001-12-29 12:25:26

Oracle date format while using TO_CHAR()

The standard date format in any oracle table is DD-MON-YY, but I still wonder for the below query if the date will get stored into reg_date column in 'DD-MON-YY' format as it is the Oracle standard or will it get stored as per the 'FMmonth DD, YYYY' format?
insert into table (id,name,reg_date)
values (1, 'abc', TO_CHAR(SYSDATE,'FMmonth DD, YYYY') );
To directly answer your question, if the column REG_DATE is (as it should be) defined as a DATE, then it will, as all of the comments have said, be stored in oracle's internal binary format for DATEs.
And you supplied vaue of
TO_CHAR(SYSDATE,'FMmonth DD, YYYY')
will simply force in implied TO_DATE() function on the string that results from your use of TO_CHAR. That implied TO_DATE will use the format mask defined by the controlling setting of NLS_DATE_FORMAT. And if that mask does not match what you used in your TO_CHAR (and that is very unlikely) you will get an error.
SQL> create table my_table (id number,
2 fname varchar2(10),
3 reg_date date
4 );
Table created.
SQL>
SQL> insert into my_table (id,fname,reg_date)
2 values (1, 'abc', TO_CHAR(SYSDATE,'FMmonth DD, YYYY') );
values (1, 'abc', TO_CHAR(SYSDATE,'FMmonth DD, YYYY') )
*
ERROR at line 2:
ORA-01858: a non-numeric character was found where a numeric was expected
SQL> --
SQL> insert into my_table (id,fname,reg_date)
2 values (1, 'abc', SYSDATE);
1 row created.
SQL> --
SQL> select id,
2 fname,
3 reg_date,
4 to_char(reg_date,'FMmonth DD, YYYY') date1,
5 to_char(reg_date,'dd-MON-yyyy') date2,
6 to_char(reg_date,'yyyy-mm-dd hh24:mi:ss') date3
7 from my_table
8 ;
ID FNAME REG_DATE DATE1 DATE2 DATE3
--- ----- --------- ----------------- ----------- -------------------
1 abc 18-FEB-21 february 18, 2021 18-FEB-2021 2021-02-18 12:08:45
1 row selected.
Lets see what Oracle actually does when you insert into a date column using various formats: ISO Standard, Oracle's NLS_DATE_FORMAT specification, and a format I just made up. Then a couple queries, and finally, with the DUMP function, a peek inside. (Also see here for slightly different set.)
create table date_examples( date_1 date, date_2 date, date_3 date);
alter session set nls_date_format = 'dd-Mon-yyyy'; -- set default
-- set the same date in verious formats: ISO Standare, Declared Standard, a strange format
insert into date_examples( date_1, date_2, date_3)
select date '2021-02-18' -- standard iso format
, to_date('18-Feb-2021') -- defaulr see alter session
, to_date('18 2021 02', 'dd yyyy mm') -- specified
from dual;
-- what are the various dates without specifying how
select * from date_examples;
-- now change the default
alter session set nls_date_format = 'Month dd, yyyy hh24:mi:ss'; -- set default
select * from date_examples;
-- take a peek at the inside.
select dump(date_1), dump(date_2), dump(date_3)
from date_examples;

Time Stamp value not inserting in oracle

I have following table in Oracle:
DESC TIME_PERIOD
Name Null Type
---------- -------- ------------
TIME_ID NOT NULL NUMBER(2)
START_TIME NOT NULL TIMESTAMP(6)
END_TIME NOT NULL TIMESTAMP(6)
I am inserting values, but values are not inserted. I am using the following query.
INSERT INTO TIME_PERIOD (TIME_ID,START_TIME,END_TIME)
VALUES (1, TO_DSINTERVAL('0 23:59:59'), TO_DSINTERVAL('0 23:59:59'));
How can I insert value in Oracle?
I want time like this
10:00 Am
11:00Am
1:00pm
Not quite sure what you're trying to do, but to insert a TIMESTAMP you need to use the SYSTIMESTAMP() function:
e.g.
INSERT INTO TIME_PERIOD (START_TIME)
VALUES (SYSTIMESTAMP);

Update with after insert trigger on same table

I have a to write a insert trigger on a tableA. which will perform update with same table but different column. I am getting error while doing this. My trigger is
create or replace trigger trigger_A
after insert on table_A
begin
update table_A set col1=1 where col1 is null;
end;
I have an application will perform col2 alone will be inserted and col1 will be kept null. so my trigger will give value for col1 once the row is inserted. But i am getting error saying "Trigger is failed and invalid" when a row is inserted.
How to do this. TIA.
If you want to assign a simple default value, the easiest way is to declare it on the table, using the DEFAULT clause.
SQL> create table t42
2 ( col1 number default 1 not null
3 , col2 date)
4 /
Table created.
SQL> insert into t42 (col2) values (sysdate)
2 /
1 row created.
SQL> select * from t42
2 /
COL1 COL2
---------- ---------
1 03-AUG-11
SQL>
This works with literals or pseudocolumns such as SYSDATE or USER. If you want to derive a more complicated value with a user-defined function or a sequence, you will need to use
a trigger.
Here is a new version of the table...
SQL> create table t42
2 ( col1 number default 1 not null
3 , col2 date default sysdate
4 , col3 varchar2(30) default user
5 , col4 number )
6 /
Table created.
SQL>
... with a trigger:
SQL> create or replace trigger t42_trg
2 before insert or update
3 on t42
4 for each row
5 begin
6 if :new.col4 is null
7 then
8 :new.col4 := my_seq.nextval;
9 end if;
10 end;
11 /
Trigger created.
SQL> insert into t42 (col1, col2, col3)
2 values (99, sysdate, 'MR KNOX')
3 /
1 row created.
SQL> select * from t42
2 /
COL1 COL2 COL3 COL4
---------- --------- ------------------------------ ----------
99 03-AUG-11 MR KNOX 161
SQL>
Note that although every column on the table is defaultable, I have to populate at least one column to make the SQL valid:
SQL> insert into t42 values ()
2 /
insert into t42 values ()
*
ERROR at line 1:
ORA-00936: missing expression
SQL>
But I can pass in NULL to COL4 to get a completely defaulted record:
SQL> insert into t42 (col4) values (null)
2 /
1 row created.
SQL> select * from t42
2 /
COL1 COL2 COL3 COL4
---------- --------- ------------------------------ ----------
99 03-AUG-11 MR KNOX 161
1 03-AUG-11 APC 162
SQL>
Caveat lector: my trigger uses the new 11g syntax. In previous versions we have to assign the sequence value using a SELECT statement:
select my_seq.nextval
into :new.col4
from dual;
You cannot update a table where the trigger is invoked:
Within a stored function or trigger, it is not permitted to modify a
table that is already being used (for reading or writing) by the
statement that invoked the function or trigger.
Doing so will generate Error 1442:
Error Code: 1442
Can't update table 'MyTable' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
In short, we are not allowed to update the table in use - but your case is simple, only want to update the field if it's NULL, for this choose BEFORE INSERT ON trigger, this way you can update all the fields of the new/current entry/row (as it has not been entered yet):
DELIMITER //
DROP TRIGGER IF EXISTS trigger_A//
CREATE TRIGGER trigger_A BEFORE INSERT ON table_A
FOR EACH ROW BEGIN
IF NEW.col1 IS NULL THEN
set NEW.col1 = <some-value>;
ENF IF;
END;
//
DELIMITER ;

How To Format Timestamp

I have col1 in myTable which is varchar, and I have to insert here timestamp eg:- 09-MAY-11 10.23.12.0000 AM.
Now please tell me:
How to insert into myTable with taking sysdate in above format...
How to retrieve data from col1 in tha same timestamp format..
INSERT:
insert into myTable (col1) VALUES (to_char(systimestamp, 'dd-mon-yyyy hh.mi.ss.ff4 AM') );
SELECT:
select to_timestamp(col1, 'dd-mon-yyyy hh.mi.ss.ff4 AM') from myTable ;
But it is much better to store the data directly as a timestamp.
Then you can compare the values ​​or modify them directly.
create table myTable1( col1 timestamp default systimestamp);