[Oracle Function] Date Function Warehouse

Just Make sure to keep the useful Oracle date function in my memery.

 

 

Oracle/PLSQL: REGEXP_REPLACE Function

 

Example: SELECT REGEXP_REPLACE('123abcde^+_','[^0-9]+','0') FROM DUAL

Result: 1230

 

Replace all nut [0~9] chart to 0.

 

SELECT REGEXP_REPLACE(substr(str_id, instr(str_id,'_',-1,1)+1),'[^0-9]+','0') from t_string_resource;

 

 

Oracle/PLSQL: SUBSTR and INSTR Function

substr(str_data, from_index); // cut string

instr(str_data, find_char,from, to)  // find char

 

select substr('MSG_GS_12345', instr('MSG_GS_12345','_',-1,1)+1) from dual;

 

Oracle/PLSQL: Trunc Function (with dates)


In Oracle/PLSQL, the trunc function returns a date truncated to a specific unit of measure.

The syntax for the trunc function is:

trunc ( date, [ format ] )

date is the date to truncate.

format is the unit of measure to apply for truncating. If the format parameter is omitted, the trunc function will truncate the date to the day value, so that any hours, minutes, or seconds will be truncated off.

 

Below are the valid format parameters:

Unit Valid format parameters
Year SYYYY, YYYY, YEAR, SYEAR, YYY, YY, Y
ISO Year IYYY, IY, I
Quarter Q
Month MONTH, MON, MM, RM
Week WW
IW IW
W W
Day DDD, DD, J
Start day of the week DAY, DY, D
Hour HH, HH12, HH24
Minute MI

 

Applies To:

  • Oracle 8i, Oracle 9i, Oracle 10g, Oracle 11g

 

For example:

trunc(to_date('22-AUG-03'), 'YEAR') would return '01-JAN-03'返回当前日期所在年的第一天
trunc(to_date('22-AUG-03'), 'Q') would return '01-JUL-03'返回当前日期所在季度的第一天
trunc(to_date('22-AUG-03'), 'MONTH') would return '01-AUG-03'返回当前日期所在月的第一天
trunc(to_date('22-AUG-03'), 'DDD') would return '22-AUG-03'返回当前日期所在周的第一个周日
trunc(to_date('22-AUG-03'), 'DAY') would return '17-AUG-03'返回当前日期所在日

 

 

Coral example:

select to_char(t.issue_date, 'w') "week",
       sum(t.commission) "commission",
       sum(t.fee) "fee"
  from t_fee_general t
 where t.status_id = 'issued'
   and t.issue_date >=TRUNC(SYSDATE, 'MM') 
   and t.issue_date <=last_day(SYSDATE)
 group by to_char(t.issue_date, 'w') order by "week";

select trunc(to_date('22-AUG-10'), 'YEAR') from dual;
select trunc(to_date('23-AUG-10'), 'DDD') from dual;
select trunc(to_date('23-AUG-10'), 'DAY') from dual;

 

 

 

Oracle/PLSQL: Next_Day Function


In Oracle/PLSQL, the next_day function returns the first weekday that is greater than a date .

The syntax for the next_day function is:

next_day( date, weekday )

date is used to find the next weekday.

weekday is a day of the week (ie: SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY)

 

Applies To:

  • Oracle 9i, Oracle 10g, Oracle 11g

 

For example:

next_day('01-Aug-03', 'TUESDAY') would return '05-Aug-03' 返回当前日期的下一个周二
next_day('06-Aug-03', 'WEDNESDAY') would return '13-Aug-03'返回当前日期的下一个周三
next_day('06-Aug-03', 'SUNDAY') would return '10-Aug-03'返回当前日期的下一个周日

 

Coral example:

select next_day('03-Aug-10', 'TUESDAY') from dual;

 

 

Oracle/PLSQL: Add_Months Function


In Oracle/PLSQL, the add_months function returns a date plus n months.

The syntax for the add_months function is:

add_months( date1, n )

date1 is the starting date (before the n months have been added).

n is the number of months to add to date1 .

 

Applies To:

  • Oracle 8i, Oracle 9i, Oracle 10g, Oracle 11g

 

For example:

add_months('01-Aug-03', 3) would return '01-Nov-03'返回当前日期的下3个月的这天
add_months('01-Aug-03', -3) would return '01-May-03'返回当前日期的前3个月的这天
add_months('21-Aug-03', -3) would return '21-May-03'返回当前日期的前3个月的这天
add_months('31-Jan-03', 1) would return '28-Feb-03'返回当前日期的下1个月的这天,如果前一个月没有这天,则使用前一个月最大日期。

 

Coral example

select add_months('30-Aug-10', -6) from dual; 结果为2010-2-28

 

 

 

Other Function introduce

new_time

In Oracle/PLSQL, the new_time function returns a date in time zone1 to a date in time zone2 .

The syntax for the new_time function is:

new_time( date, zone1, zone2 )

For Detail: http://www.techonthenet.com/oracle/functions/new_time.php

 

To_Char

In Oracle/PLSQL, the to_char function converts a number or date to a string.

The syntax for the to_char function is:

to_char( value, [ format_mask ], [ nls_language ] )

value can either be a number or date that will be converted to a string.

format_mask is optional. This is the format that will be used to convert value to a string.

nls_language is optional. This is the nls language used to convert value to a string.

For Detail: http://www.techonthenet.com/oracle/functions/to_char.php

 

 

last_day

 

In Oracle/PLSQL, the last_day function returns the last day of the month based on a date value.

The syntax for the last_day function is:

last_day( date )

http://www.techonthenet.com/oracle/functions/last_day.php

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(oracle,PHP,IE,J#)