Home » SQL & PL/SQL » SQL & PL/SQL » Convert a string to a date using TO_DATE Function in after insert trigger?
Convert a string to a date using TO_DATE Function in after insert trigger? [message #625727] |
Mon, 13 October 2014 12:00 |
|
mattfriend
Messages: 12 Registered: September 2014
|
Junior Member |
|
|
Hi Folks:
I have a table that has a column (called DATE_FED) that must be saved in string format.
After all the data is saved an after insert trigger is fired that copies certain columns to another table.
The problem is that the after insert trigger will not work because the table being copied to stores the DATE_FED data in a Date format.
I am trying to figure out how to use the TO_DATE function to convert the NEW.DATE_FED data to a Date format so the trigger will work.
Here is the trigger as it currently reads:
create or replace TRIGGER "FARM"."BALES_FED_PAST_TRIG" AFTER INSERT ON "FARM"."BALES_FED_PASTURE" REFERENCING OLD AS "OLD" NEW AS "NEW" FOR EACH ROW Declare
BEGIN
insert into BALES_STORAGE (BALES_LOST_SUBTRACTED, CROP, CUTTING, STACKYARD, DATE_MOVED, FED_ID, BALE_YEAR) values (:new.BALES_FED, :new.CROP, :new.CUTTING, :new.STACKYARD, :new.DATE_FED, :new.FED_ID, :new.BALE_YEAR);
END;
This trigger worked before I had to change to column type of DATE_FED from Date to String.
I have tried things like:
TO_DATE(DATE_FED) := NEW.DATE_FED; in a declare statement but I'm not getting it.
Summary:
I need an after insert trigger to convert NEW.DATE_FED (a string) to NEW.DATE_FED (a Date).
Tables:
BALES_FED_PASTURE is the table the trigger is running on.
BALES_STORAGE is the table the trigger is copying certain columns to.
Thank you very much for any help.
Matthew
|
|
|
|
|
Re: Convert a string to a date using TO_DATE Function in after insert trigger? [message #625733 is a reply to message #625727] |
Mon, 13 October 2014 12:39 |
joy_division
Messages: 4963 Registered: February 2005 Location: East Coast USA
|
Senior Member |
|
|
mattfriend wrote on Mon, 13 October 2014 13:00
I have tried things like:
TO_DATE(DATE_FED) := NEW.DATE_FED; in a declare statement but I'm not getting it.
Summary:
I need an after insert trigger to convert NEW.DATE_FED (a string) to NEW.DATE_FED (a Date).
TO_DATE requires a format string. You have none.
How do you distinguish "NEW.DATE_FED (a string) to NEW.DATE_FED (a Date)?"
You would use TO_DATE on the string, not the DATE.
NEW values are referred to with a colon.
|
|
|
Re: Convert a string to a date using TO_DATE Function in after insert trigger? [message #625744 is a reply to message #625733] |
Mon, 13 October 2014 14:00 |
|
mattfriend
Messages: 12 Registered: September 2014
|
Junior Member |
|
|
Hi Joy:
Thank you for trying to help, I appreciate it.
I tried a few variations of the following with what you said:
create or replace TRIGGER "FARM"."BALES_FED_PAST_TRIG" AFTER INSERT ON "FARM"."BALES_FED_PASTURE" REFERENCING OLD AS "OLD" NEW AS "NEW" FOR EACH ROW Declare
:NEW.DATE_FED := TO_DATE(:NEW.DATE_FED, 'MM/DD/YYYY');
BEGIN
insert into BALES_STORAGE (BALES_LOST_SUBTRACTED, CROP, CUTTING, STACKYARD, DATE_MOVED, FED_ID, BALE_YEAR) values (:new.BALES_FED, :new.CROP, :new.CUTTING, :new.STACKYARD, :new.DATE_FED, :new.FED_ID, :new.BALE_YEAR);
END;
The trigger still won't compile.
I'm probably doing something dumb....
Matthew
|
|
|
|
|
Re: Convert a string to a date using TO_DATE Function in after insert trigger? [message #625748 is a reply to message #625746] |
Mon, 13 October 2014 15:13 |
Bill B
Messages: 1971 Registered: December 2004
|
Senior Member |
|
|
you can convert in the insert command. The following code assumes that the layout of :new.date_fed is mm/dd/yyyy. If it isn't you have to change the format mask.
create or replace TRIGGER "FARM"."BALES_FED_PAST_TRIG"
AFTER INSERT ON "FARM"."BALES_FED_PASTURE"
REFERENCING OLD AS "OLD" NEW AS "NEW"
FOR EACH ROW
BEGIN
insert into BALES_STORAGE (BALES_LOST_SUBTRACTED, CROP, CUTTING, STACKYARD, DATE_MOVED, FED_ID, BALE_YEAR) values (:new.BALES_FED, :new.CROP, :new.CUTTING, :new.STACKYARD, TO_DATE(:new.DATE_FED,'mm/dd/yyyy'), :new.FED_ID, :new.BALE_YEAR);
END;
|
|
|
Re: Convert a string to a date using TO_DATE Function in after insert trigger? [message #625749 is a reply to message #625746] |
Mon, 13 October 2014 15:15 |
|
mattfriend
Messages: 12 Registered: September 2014
|
Junior Member |
|
|
Hi Michel:
The BALES_FED_PASTURE (the table the data is going to be copied from with the trigger) is:
"DATE_FED" VARCHAR2(18), (so it is saved in string format as MM/DD/YYYY)
The BALES_STORAGE table is the table the data is being copied too.
"DATE_MOVED" DATE, is the column I need to copy the DATE_FED data too.
The actual Table sql is:
CREATE TABLE "BALES_STORAGE"
( "BALES_LOST_SUBTRACTED" NUMBER(5,0) DEFAULT NULL,
"CROP" VARCHAR2(20 CHAR),
"CUTTING" NUMBER(5,0),
"STACKYARD" VARCHAR2(25),
"DATE_MOVED" DATE,
"STORAGE_ID" NUMBER(15,0),
"BALES_RECEIVED_GAINED" NUMBER(5,0),
"FED_ID" NUMBER(15,0),
"HARVEST_ID" NUMBER(15,0),
"CONSUMED_ID" NUMBER(15,0),
"MOVED_ID" NUMBER(15,0),
"STOCKTAKE_ID" NUMBER(15,0),
"BALE_YEAR" VARCHAR2(4),
"FED_DEST_ID" NUMBER(15,0)
) ;
--------------------------------------------------------
-- DDL for Table BALES_FED_PASTURE
--------------------------------------------------------
CREATE TABLE "BALES_FED_PASTURE"
( "BALES_FED" NUMBER(5,0) DEFAULT NULL,
"CROP" VARCHAR2(20 CHAR),
"CUTTING" NUMBER(5,0),
"STACKYARD" VARCHAR2(25),
"DATE_FED" VARCHAR2(18),
"PASTURE" VARCHAR2(25),
"TARGET_LB_PER_DAY" NUMBER(3,1),
"FED_ID" NUMBER(15,0),
"SPECIES" VARCHAR2(30),
"BALE_YEAR" VARCHAR2(4),
"POLE_SHED" VARCHAR2(8),
"USERNAME" VARCHAR2(40),
"STORAGE_SUMMARY" VARCHAR2(80)
) ;
[Updated on: Mon, 13 October 2014 15:16] Report message to a moderator
|
|
|
|
|
|
Goto Forum:
Current Time: Wed Apr 24 19:41:02 CDT 2024
|