[Import] common Oracle functions: DateDiff ()

Source: Internet
Author: User
/*
-- Function: Convert the string to the year, month, and day format, and delete the time, minute, and second.
-- Source: http://jorkin.reallydo.com/article.asp? Id = 529
-- Parameter:
Datechar varchar2
-- Oracle9i passed the test
*/

Create or replace function cdate (datechar in varchar2) return date is
Reallydo date;
Begin
Select to_date (to_char (datechar), 'yyyy-MM-DD hh24: MI: ss '),
'Yyyy-MM-DD '),
'Yyyy-MM-DD ')
Into reallydo
From dual;
Return (reallydo );
End cdate;

/*
-- Function: convert a string to the format of year, month, day, hour, minute, and second.
-- Source: http://jorkin.reallydo.com/article.asp? Id = 529
-- Parameter:
Datechar varchar2
-- Oracle9i passed the test
*/

Create or replace function cdatetime (datechar in varchar2) return date is
Reallydo date;
Begin
Select to_date (to_char (datechar), 'yyyy-MM-DD hh24: MI: ss '),
'Yyyy-MM-DD hh24: MI: ss '),
'Yyyy-MM-DD hh24: MI: ss ')
Into reallydo
From dual;
Return (reallydo );
End cdatetime;

/*
-- Function: Date comparison function similar to MSSQL
-- Source: http://jorkin.reallydo.com/article.asp? Id = 529
-- Update Time: 20080721
-- Parameter:
Datepart varchar2 -- year comparison? Month? ?
Startdate varchar2 -- start date
EndDate Varchar2 -- end date
-- Oracle9i passed the test
*/

Create Or Replace Function Datediff
(
Datepart In Varchar2,
StartDate In Varchar2,
EndDate In Varchar2
) Return Number Is
ReallyDo Numeric;
Begin
Select Case Upper (Datepart)
When 'yyyy' Then
Trunc (Extract (Year From CDate (EndDate ))-
Extract (Year From CDate (StartDate )))
When 'M' Then
Datediff ('yyyy', StartDate, EndDate) * 12 +
(Extract (Month From CDate (EndDate ))-
Extract (Month From CDate (StartDate )))
When 'd 'then
Trunc (cdate (enddate)-cdate (startdate ))
When 'H' then
Datediff ('D', startdate, enddate) * 24 +
(To_number (to_char (cdatetime (enddate), 'hh24 '))-
To_number (to_char (cdatetime (startdate), 'hh24 ')))
When 'n' then
Datediff ('D', StartDate, EndDate) * 24*60 +
(To_Number (to_char (CDateTime (EndDate), 'mi '))-
To_Number (to_char (CDateTime (StartDate), 'mi ')))
When's Then
Datediff ('D', StartDate, EndDate) * 24*60*60 +
(To_Number (to_char (CDateTime (EndDate), 'ss '))-
To_Number (to_char (CDateTime (StartDate), 'ss ')))
Else
-29252888
End
Into ReallyDo
From Dual;
Return (ReallyDo );
End Datediff;

Source: http://Jorkin.Reallydo.Com/default.asp? Id = 529

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.