Implementation of Oracle Auto insert current time year month day YYYY-MM-DD format

Source: Internet
Author: User

Oracle does not have the date () function. The sysdate function value includes the time, minute, and second. It is really troublesome to insert the default value of the current time.

You have to write the stored procedure on your own, and you cannot call the stored procedure in the field default value. You have to write a trigger! The first few digits of sysdate obtained during the storage process are only in the format of-07, instead of what you want, in the format of-11-17, you have to combine the values of year, month, and day separately, so that the return value cannot be a date type but a character type.

A word is annoying! But I still implemented it. I will share with you the code below. If there is a better way, please let us know.

1. Storage Process

Create or replace function "GET_DATE" RETURN VARCHAR2
IS
Yyyy varchar2 (36 );
Mm varchar2 (36 );
Dd varchar2 (36 );
Tempdate varchar2 (36 );
BEGIN
Tempdate: = '';
Select to_char (to_date (sysdate), 'yyyy') into YYYY from dual;
Select to_char (to_date (sysdate), 'mm') into MM from dual;
Select to_char (to_date (sysdate), 'dd') into DD from dual;
Tempdate: = substr (yyyy, 1, 4) | '-' |
Substr (mm, 1, 2) | '-' |
Substr (dd, 1, 2)
;
Return tempdate;
END;

2. triggers

Create or replace trigger STATWEEK_tg
-- STATWEEK fdate trigger
BEFORE INSERT ON STATWEEK FOR EACH ROW
BEGIN
SELECT get_date INTO: NEW. fdate from dual;
END;

Note: STATWEEK indicates that the corresponding data table fdate is an Automatically increasing field, and get_date indicates the corresponding stored procedure name.

3. Data Table

-- Create table
Create table STATWEEK
(
Monday VARCHAR2 (20) default 0,
Tuesday VARCHAR2 (20) default 0,
Wednesday VARCHAR2 (20) default 0,
Thursday VARCHAR2 (20) default 0,
Friday VARCHAR2 (20) default 0,
Saturday VARCHAR2 (20) default 0,
Sunday VARCHAR2 (20) default 0,
TWEEK VARCHAR2 (10 ),
ADMIN VARCHAR2 (50 ),
FDATE VARCHAR2 (20)
)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.