Purpose
ADD_MONTHS
returns the date date plus integer months. The date argument can be a datetime value or any value that can be implicitly converted toDATE
. The integer argument can be an integer or any value that can be implicitly converted to an integer. The return type is alwaysDATE
, regardless of the datatype of date. If date is the last day of the month or if the resulting month has fewer days than the day component of date, then the result is the last day of the resulting month. Otherwise, the result has the same day component as date.
Examples
The following example returns the month after the hire_date in the sample table employees
:
SELECT TO_CHAR(
ADD_MONTHS(hire_date,1),
'DD-MON-YYYY') "Next month"
FROM employees
WHERE last_name = 'Baer';
Next Month
-----------
07-JUL-1994
Purpose
CURRENT_DATE
returns the current date in the session time zone, in a value in the Gregorian calendar of datatype DATE
.
Examples
The following example illustrates that CURRENT_DATE
is sensitive to the session time zone:
ALTER SESSION SET TIME_ZONE = '-5:0';
ALTER SESSION SET NLS_DATE_FORMAT = 'DD-MON-YYYY HH24:MI:SS';
SELECT SESSIONTIMEZONE, CURRENT_DATE FROM DUAL;
SESSIONTIMEZONE CURRENT_DATE
--------------- --------------------
-05:00 29-MAY-2000 13:14:03
ALTER SESSION SET TIME_ZONE = '-8:0';
SELECT SESSIONTIMEZONE, CURRENT_DATE FROM DUAL;
SESSIONTIMEZONE CURRENT_DATE
--------------- --------------------
-08:00 29-MAY-2000 10:14:33
Purpose
CURRENT_TIMESTAMP returns the current date and time in the session time zone, in a value of datatype TIMESTAMP WITH TIME ZONE. The time zone offset reflects the current local time of the SQL session. If you omit precision, then the default is 6. The difference between this function and LOCALTIMESTAMP is that CURRENT_TIMESTAMP returns a TIMESTAMP WITH TIME ZONE value while LOCALTIMESTAMP returns a TIMESTAMP value.
In the optional argument, precision specifies the fractional second precision of the time value returned.
Examples
The following example illustrates that CURRENT_TIMESTAMP is sensitive to the session time zone:
ALTER SESSION SET TIME_ZONE = '-5:0';
ALTER SESSION SET NLS_DATE_FORMAT = 'DD-MON-YYYY HH24:MI:SS';
SELECT SESSIONTIMEZONE, CURRENT_TIMESTAMP FROM DUAL;
SESSIONTIMEZONE CURRENT_TIMESTAMP
--------------- ---------------------------------------------------
-05:00 04-APR-00 01.17.56 .917550 PM -05:00
ALTER SESSION SET TIME_ZONE = '-8:0';
SELECT SESSIONTIMEZONE, CURRENT_TIMESTAMP FROM DUAL;
SESSIONTIMEZONE CURRENT_TIMESTAMP
--------------- ----------------------------------------------------
-08:00 04-APR-00 10.18.21 .366065 AM -08:00
If you use the CURRENT_TIMESTAMP with a format mask, take care that the format mask matches the value returned by the function. For example, consider the following table:
CREATE TABLE current_test (col1 TIMESTAMP WITH TIME ZONE);
The following statement fails because the mask does not include the TIME ZONE portion of the type returned by the function:
INSERT INTO current_test VALUES
(TO_TIMESTAMP_TZ(CURRENT_TIMESTAMP, 'DD-MON-RR HH.MI.SSXFF PM'));
The following statement uses the correct format mask to match the return type of CURRENT_TIMESTAMP:
INSERT INTO current_test VALUES (TO_TIMESTAMP_TZ
(CURRENT_TIMESTAMP, 'DD-MON-RR HH.MI.SSXFF PM TZH:TZM'));
Purpose
DBTIMEZONE
returns the value of the database time zone. The return type is a time zone offset (a character type in the format '[+|-]TZH:TZM'
) or a time zone region name, depending on how the user specified the database time zone value in the most recent CREATE
DATABASE
or ALTER
DATABASE
statement.
Examples
The following example assumes that the database time zone is set to UTC time zone:
SELECT DBTIMEZONE FROM DUAL;
DBTIME
------
+00:00
| MONTH
| DAY
| HOUR
| MINUTE
| SECOND
}
| { TIMEZONE_HOUR
| TIMEZONE_MINUTE
}
| { TIMEZONE_REGION
| TIMEZONE_ABBR
}
}
FROM { datetime_value_expression
| interval_value_expression
}
)
Purpose
EXTRACT extracts and returns the value of a specified datetime field from a datetime or interval value expression. When you extract a TIMEZONE_REGION or TIMEZONE_ABBR (abbreviation), the value returned is a string containing the appropriate time zone name or abbreviation. When you extract any of the other values, the value returned is in the Gregorian calendar. When extracting from a datetime with a time zone value, the value returned is in UTC. For a listing of time zone names and their corresponding abbreviations, query the V$TIMEZONE_NAMES dynamic performance view.
This function can be very useful for manipulating datetime field values in very large tables, as shown in the first example below.
Some combinations of datetime field and datetime or interval value expression result in ambiguity. In these cases, Oracle Database returns UNKNOWN (see the examples that follow for additional information).
The field you are extracting must be a field of the datetime_value_expr or interval_value_expr. For example, you can extract only YEAR, MONTH, and DAY from a DATE value. Likewise, you can extract TIMEZONE_HOUR and TIMEZONE_MINUTE only from the TIMESTAMP WITH TIME ZONE datatype.
Examples
The following example returns from the oe.orders table the number of orders placed in each month:
SELECT EXTRACT(month FROM order_date) "Month",
COUNT(order_date) "No. of Orders"
FROM orders
GROUP BY EXTRACT(month FROM order_date)
ORDER BY "No. of Orders" DESC;
Month No. of Orders
---------- -------------
11 15
7 14
6 14
3 11
5 10
9 9
2 9
8 7
10 6
1 5
12 4
4 1
12 rows selected.
The following example returns the year 1998.
SELECT EXTRACT(YEAR FROM DATE ' 1998-03-07 ') FROM DUAL;
EXTRACT(YEARFROMDATE' 1998-03-07 ')
---------------------------------
1998
The following example selects from the sample table hr.employees all employees who were hired after 1998:
SELECT last_name, employee_id, hire_date
FROM employees
WHERE EXTRACT(YEAR FROM
TO_DATE(hire_date, 'DD-MON-RR')) > 1998
ORDER BY hire_date;
LAST_NAME EMPLOYEE_ID HIRE_DATE
------------------------- ----------- ---------
Landry 127 14-JAN-99
Lorentz 107 07-FEB-99
Cabrio 187 07-FEB-99
. . .
The following example results in ambiguity, so Oracle returns UNKNOWN:
SELECT EXTRACT(TIMEZONE_REGION
FROM TIMESTAMP '1999-01-01 10:00:00 -08:00')
FROM DUAL;
EXTRACT(TIMEZONE_REGIONFROMTIMESTAMP'1999-01-0110:00:00-08:00')
----------------------------------------------------------------
UNKNOWN
The ambiguity arises because the time zone numerical offset is provided in the expression, and that numerical offset may map to more than one time zone region.
Purpose
LAST_DAY
returns the date of the last day of the month that contains date. The return type is always DATE
, regardless of the datatype of date.
Examples
The following statement determines how many days are left in the current month.
SELECT SYSDATE,
LAST_DAY(SYSDATE) "Last",
LAST_DAY(SYSDATE) - SYSDATE "Days Left"
FROM DUAL;
SYSDATE Last Days Left
--------- --------- ----------
30-MAY-01 31-MAY-01 1
The following example adds 5
months to the hire date of each employee to give an evaluation date:
SELECT last_name, hire_date, TO_CHAR(
ADD_MONTHS(LAST_DAY(hire_date), 5)) "Eval Date"
FROM employees;
LAST_NAME HIRE_DATE Eval Date
------------------------- --------- ---------
King 17-JUN-87 30-NOV-87
Kochhar 21-SEP-89 28-FEB-90
De Haan 13-JAN-93 30-JUN-93
Hunold 03-JAN-90 30-JUN-90
Ernst 21-MAY-91 31-OCT-91
Austin 25-JUN-97 30-NOV-97
Pataballa 05-FEB-98 31-JUL-98
Lorentz 07-FEB-99 31-JUL-99
. . .
Purpose
LOCALTIMESTAMP returns the current date and time in the session time zone in a value of datatype TIMESTAMP. The difference between this function and CURRENT_TIMESTAMP is that LOCALTIMESTAMP returns a TIMESTAMP value while CURRENT_TIMESTAMP returns a TIMESTAMP WITH TIME ZONE value.
The optional argument timestamp_precision specifies the fractional second precision of the time value returned.
Examples
This example illustrates the difference between LOCALTIMESTAMP and CURRENT_TIMESTAMP:
ALTER SESSION SET TIME_ZONE = '-5:00';
SELECT CURRENT_TIMESTAMP, LOCALTIMESTAMP FROM DUAL;
CURRENT_TIMESTAMP LOCALTIMESTAMP
-------------------------------------------------------------------
04-APR-00 01.27.18 .999220 PM -05:00 04-APR-00 01.27.19 PM
ALTER SESSION SET TIME_ZONE = '-8:00';
SELECT CURRENT_TIMESTAMP, LOCALTIMESTAMP FROM DUAL;
CURRENT_TIMESTAMP LOCALTIMESTAMP
----------------------------------- ------------------------------
04-APR-00 10.27.45 .132474 AM -08:00 04-APR-00 10.27.451 AM
If you use the LOCALTIMESTAMP with a format mask, take care that the format mask matches the value returned by the function. For example, consider the following table:
CREATE TABLE local_test (col1 TIMESTAMP WITH LOCAL TIME ZONE);
The following statement fails because the mask does not include the TIME ZONE portion of the return type of the function:
INSERT INTO local_test VALUES
(TO_TIMESTAMP(LOCALTIMESTAMP, 'DD-MON-RR HH.MI.SSXFF'));
The following statement uses the correct format mask to match the return type of LOCALTIMESTAMP:
INSERT INTO local_test VALUES
(TO_TIMESTAMP(LOCALTIMESTAMP, 'DD-MON-RR HH.MI.SSXFF PM'));
Purpose
TO_DATE converts char of CHAR, VARCHAR2, NCHAR, or NVARCHAR2 datatype to a value of DATE datatype. The fmt is a datetime model format specifying the format of char. If you omit fmt, then char must be in the default date format. If fmt is J, for Julian, then char must be an integer.
The default date format is determined implicitly by the NLS_TERRITORY initialization parameter or can be set explicitly by the NLS_DATE_FORMAT parameter.
The 'nlsparam' argument has the same purpose in this function as in the TO_CHAR function for date conversion.
Do not use the TO_DATE function with a DATE value for the char argument. The first two digits of the returned DATE value can differ from the original char, depending on fmt or the default date format.
This function does not support CLOB data directly. However, CLOBs can be passed in as arguments through implicit data conversion.
Examples
The following example converts a character string into a date:
SELECT TO_DATE(
'January 15, 1989, 11: 00 A .M.',
'Month dd, YYYY, HH:MI A.M.',
'NLS_DATE_LANGUAGE = American')
FROM DUAL;
TO_DATE('
---------
15-JAN-89
The value returned reflects the default date format if the NLS_TERRITORY parameter is set to ' AMERICA '. Different NLS_TERRITORY values result in different default date formats:
ALTER SESSION SET NLS_TERRITORY = 'KOREAN';
SELECT TO_DATE(
'January 15, 1989, 11: 00 A .M.',
'Month dd, YYYY, HH:MI A.M.',
'NLS_DATE_LANGUAGE = American')
FROM DUAL;
TO_DATE(
--------
89/01/15
Purpose
TO_CHAR
(datetime) converts a datetime or interval value of DATE
, TIMESTAMP
, TIMESTAMP
WITH
TIME
ZONE
, or TIMESTAMP
WITH
LOCAL
TIME
ZONE
datatype to a value of VARCHAR2
datatype in the format specified by the date format fmt. If you omit fmt, then date is converted to a VARCHAR2
value as follows:
· DATE
values are converted to values in the default date format.
· TIMESTAMP
and TIMESTAMP
WITH
LOCAL
TIME
ZONE
values are converted to values in the default timestamp format.
· TIMESTAMP
WITH
TIME
ZONE
values are converted to values in the default timestamp with time zone format.
Please refer to "Format Models" for information on datetime formats.
The 'nlsparam' argument specifies the language in which month and day names and abbreviations are returned. This argument can have this form:
'NLS_DATE_LANGUAGE = language'
If you omit 'nlsparam', then this function uses the default date language for your session.
Examples
The following example uses this table:
CREATE TABLE date_tab (
ts_col TIMESTAMP,
tsltz_col TIMESTAMP WITH LOCAL TIME ZONE,
tstz_col TIMESTAMP WITH TIME ZONE);
The example shows the results of applying TO_CHAR
to different TIMESTAMP
datatypes. The result for a TIMESTAMP
WITH
LOCAL
TIME
ZONE
column is sensitive to session time zone, whereas the results for the TIMESTAMP
and TIMESTAMP
WITH
TIME
ZONE
columns are not sensitive to session time zone:
ALTER SESSION SET TIME_ZONE = '-8:00';
INSERT INTO date_tab VALUES (
TIMESTAMP'1999-12-01 10:00:00',
TIMESTAMP'1999-12-01 10:00:00',
TIMESTAMP'1999-12-01 10:00:00');
INSERT INTO date_tab VALUES (
TIMESTAMP'1999-12-02 10:00:00 -8:00',
TIMESTAMP'1999-12-02 10:00:00 -8:00',
TIMESTAMP'1999-12-02 10:00:00 -8:00');
SELECT TO_CHAR(ts_col, 'DD-MON-YYYY HH24:MI:SSxFF'),
TO_CHAR(tstz_col, 'DD-MON-YYYY HH24:MI:SSxFF TZH:TZM')
FROM date_tab;
TO_CHAR(TS_COL,'DD-MON-YYYYHH2 TO_CHAR(TSTZ_COL,'DD-MON-YYYYHH24:MI:
------------------------------ -------------------------------------
01-DEC-1999 10:00:00 01-DEC-1999 10:00:00.000000 -08:00
02-DEC-1999 10:00:00 02-DEC-1999 10:00:00.000000 -08:00
SELECT SESSIONTIMEZONE,
TO_CHAR(tsltz_col, 'DD-MON-YYYY HH24:MI:SSxFF')
FROM date_tab;
SESSIONTIMEZONE TO_CHAR(TSLTZ_COL,'DD-MON-YYYY
--------------- ------------------------------
-08:00 01-DEC-1999 10:00:00.000000
-08:00 02-DEC-1999 10:00:00.000000
ALTER SESSION SET TIME_ZONE = '-5:00';
SELECT TO_CHAR(ts_col, 'DD-MON-YYYY HH24:MI:SSxFF'),
TO_CHAR(tstz_col, 'DD-MON-YYYY HH24:MI:SSxFF TZH:TZM')
FROM date_tab;
TO_CHAR(TS_COL,'DD-MON-YYYYHH2 TO_CHAR(TSTZ_COL,'DD-MON-YYYYHH24:MI:
------------------------------ -------------------------------------
01-DEC-1999 10:00:00.000000 01-DEC-1999 10:00:00.000000 -08:00
02-DEC-1999 10:00:00.000000 02-DEC-1999 10:00:00.000000 -08:00
SELECT SESSIONTIMEZONE,
TO_CHAR(tsltz_col, 'DD-MON-YYYY HH24:MI:SSxFF')
FROM date_tab;
SESSIONTIMEZONE TO_CHAR(TSLTZ_COL,'DD-MON-YYYY
------------------------- ------------------------------
-05:00 01-DEC-1999 13:00:00.000000
-05:00 02-DEC-1999 13:00:00.000000
Purpose
MONTHS_BETWEEN
returns number of months between dates date1 and date2. If date1 is later than date2, then the result is positive. If date1 is earlier than date2, then the result is negative. If date1 and date2 are either the same days of the month or both last days of months, then the result is always an integer. Otherwise Oracle Database calculates the fractional portion of the result based on a 31-day month and considers the difference in time components date1 and date2.
Examples
The following example calculates the months between two dates:
SELECT MONTHS_BETWEEN
(TO_DATE('
02-02-1995
','MM-DD-YYYY'),
TO_DATE('
01-01-1995
','MM-DD-YYYY') ) "Months"
FROM DUAL;
Months
----------
1.03225806
其中char表示星期,可能的方式有‘星期一’/‘monday’/0等。
Purpose
NEXT_DAY
returns the date of the first weekday named by char that is later than the date date. The return type is always DATE
, regardless of the datatype of date. The argument char must be a day of the week in the date language of your session, either the full name or the abbreviation. The minimum number of letters required is the number of letters in the abbreviated version. Any characters immediately following the valid abbreviation are ignored. The return value has the same hours, minutes, and seconds component as the argument date.
Examples
This example returns the date of the next Tuesday after February 2, 2001:
SELECT NEXT_DAY('02-FEB-2001','TUESDAY') "NEXT DAY"
FROM DUAL;
NEXT DAY
-----------
06-FEB-2001
Purpose
The TRUNC
(date) function returns date with the time portion of the day truncated to the unit specified by the format model fmt. The value returned is always of datatype DATE
, even if you specify a different datetime datatype for date. If you omit fmt, then date is truncated to the nearest day. Please refer to "ROUND and TRUNC Date Functions" for the permitted format models to use in fmt.
Examples
本周一和下周一
select next_day(sysdate,1),trunc(sysdate,'D') from dual
本月初和下月初
select add_months(trunc(sysdate,'month'), 1) ,trunc(sysdate,'month') from dual
本月末和下月末
select add_months(last_day(sysdate), 1) ,last_day(sysdate) from dual
本年初和下年初
select add_months(trunc(sysdate,'year'), 12) ,trunc(sysdate,'year') from dual
本季度第一天和下季度第一天
select trunc(sysdate,'Q'),add_months(trunc(sysdate,'Q'),3) from dual;
1.Y或YY或YYY 年的最后一位,两位或三位
SQL> Select to_char(sysdate,'Y') from dual;
TO_CHAR(SYSDATE,'Y')
--------------------
7
SQL> Select to_char(sysdate,'YY') from dual;
TO_CHAR(SYSDATE,'YY')
---------------------
07
SQL> Select to_char(sysdate,'YYY') from dual;
TO_CHAR(SYSDATE,'YYY')
----------------------
007
2.Q 季度 1~3月为第一季度,2表示第二季度。
SQL> Select to_char(sysdate,'Q') from dual;
TO_CHAR(SYSDATE,'Q')
--------------------
2
3.MM 月份数
SQL> Select to_char(sysdate,'MM') from dual;
TO_CHAR(SYSDATE,'MM')
---------------------
05
4.RM 月份的罗马表示 (V在罗马数字中表示 5)
SQL> Select to_char(sysdate,'RM') from dual;
TO_CHAR(SYSDATE,'RM')
---------------------
V
5.Month 用9个字符长度表示的月份名
SQL> Select to_char(sysdate,'Month') from dual;
TO_CHAR(SYSDATE,'MONTH')
------------------------
5月
6.WW 当年第几周( 2007年 5月 29日 为2007年第22周)
SQL> Select to_char(sysdate,'WW') from dual;
TO_CHAR(SYSDATE,'WW')
---------------------
22
7.W 本月第几周 ( 2007年 5月 29日 为5月第5周)
SQL> Select to_char(sysdate,'W') from dual;
TO_CHAR(SYSDATE,'W')
--------------------
5
8.DDD 当年第几天( 2007年 5月 29日 为2007年第149天)
SQL> Select to_char(sysdate,'DDD') from dual;
TO_CHAR(SYSDATE,'DDD')
----------------------
149
9. DD 当月第几天
SQL> Select to_char(sysdate,'DD') from dual;
TO_CHAR(SYSDATE,'DD')
---------------------
29
10.D 周内第几天
SQL> Select to_char(sysdate,'D') from dual;
TO_CHAR(SYSDATE,'D')
--------------------
3
11.DY 中文的星期几 (( 2007年 5月 29日 为星期二))
SQL> Select to_char(sysdate,'DY') from dual;
TO_CHAR(SYSDATE,'DY')
---------------------
星期二
12.HH或HH12 12进制小时数(16:09分为用12小时制计时为4点)
SQL> Select to_char(sysdate,'HH') from dual;
TO_CHAR(SYSDATE,'HH')
---------------------
04
13.HH24 24小时制
SQL> Select to_char(sysdate,'HH24') from dual;
TO_CHAR(SYSDATE,'HH24')
-----------------------
16