Oracle PL/SQL Tutorial/Date Timestamp Functions/EXTRACT — различия между версиями

Материал из SQL эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 10:04, 26 мая 2010

extract day from birthday

SQL>
SQL>
SQL> create table employees(
  2    empno      NUMBER(4)
  3  , ename      VARCHAR2(8)
  4  , init       VARCHAR2(5)
  5  , job        VARCHAR2(8)
  6  , mgr        NUMBER(4)
  7  , bdate      DATE
  8  , msal       NUMBER(6,2)
  9  , comm       NUMBER(6,2)
 10  , deptno     NUMBER(2) ) ;
Table created.
SQL>
SQL>
SQL> insert into employees values(1,"Jason",  "N",  "TRAINER", 2,   date "1965-12-18",  800 , NULL,  10);
1 row created.
SQL> insert into employees values(2,"Jerry",  "J",  "SALESREP",3,   date "1966-11-19",  1600, 300,   10);
1 row created.
SQL> insert into employees values(3,"Jord",   "T" , "SALESREP",4,   date "1967-10-21",  1700, 500,   20);
1 row created.
SQL> insert into employees values(4,"Mary",   "J",  "MANAGER", 5,   date "1968-09-22",  1800, NULL,  20);
1 row created.
SQL> insert into employees values(5,"Joe",    "P",  "SALESREP",6,   date "1969-08-23",  1900, 1400,  30);
1 row created.
SQL> insert into employees values(6,"Black",  "R",  "MANAGER", 7,   date "1970-07-24",  2000, NULL,  30);
1 row created.
SQL> insert into employees values(7,"Red",    "A",  "MANAGER", 8,   date "1971-06-25",  2100, NULL,  40);
1 row created.
SQL> insert into employees values(8,"White",  "S",  "TRAINER", 9,   date "1972-05-26",  2200, NULL,  40);
1 row created.
SQL> insert into employees values(9,"Yellow", "C",  "DIRECTOR",10,  date "1973-04-27",  2300, NULL,  20);
1 row created.
SQL> insert into employees values(10,"Pink",  "J",  "SALESREP",null,date "1974-03-28",  2400, 0,     30);
1 row created.
SQL>
SQL> select bdate
  2  ,      extract(day   from bdate) as day_of_birth
  3  from   employees;
BDATE     DAY_OF_BIRTH
--------- ------------
18-DEC-65           18
19-NOV-66           19
21-OCT-67           21
22-SEP-68           22
23-AUG-69           23
24-JUL-70           24
25-JUN-71           25
26-MAY-72           26
27-APR-73           27
28-MAR-74           28
10 rows selected.
SQL>
SQL> drop table employees;
Table dropped.
SQL>
SQL>


EXTRACT() gets the hour, minute, and second from a TIMESTAMP returned by TO_TIMESTAMP()

SQL>
SQL> SELECT
  2    EXTRACT(HOUR FROM TO_TIMESTAMP("01-JAN-2005 19:15:26",
  3      "DD-MON-YYYY HH24:MI:SS")) AS HOUR
  4  FROM dual;
      HOUR
----------
        19
SQL>
-


EXTRACT()gets the time zone hour, minute, second, region, and region abbreviation from a TIMESTAMP WITH TIMEZONE returned by TO_TIMESTAMP_TZ()

SQL>
SQL> SELECT
  2    EXTRACT(TIMEZONE_HOUR FROM TO_TIMESTAMP_TZ(
  3      "01-JAN-2005 19:15:26 -7:15", "DD-MON-YYYY HH24:MI:SS TZH:TZM"))
  4      AS TZH
  5  FROM dual;
       TZH
----------
        -7
SQL>


EXTRACT(MINUTE FROM TO_TIMESTAMP("01-JAN-15:26", "DD-MON-YYYY HH24:MI:SS")) AS MINUTE

SQL> SELECT
  2    EXTRACT(MINUTE FROM TO_TIMESTAMP("01-JAN-2005 19:15:26",
  3      "DD-MON-YYYY HH24:MI:SS")) AS MINUTE
  4  FROM dual;
    MINUTE
----------
        15
SQL>


extract month from birthday

SQL>
SQL> create table employees(
  2    empno      NUMBER(4)
  3  , ename      VARCHAR2(8)
  4  , init       VARCHAR2(5)
  5  , job        VARCHAR2(8)
  6  , mgr        NUMBER(4)
  7  , bdate      DATE
  8  , msal       NUMBER(6,2)
  9  , comm       NUMBER(6,2)
 10  , deptno     NUMBER(2) ) ;
Table created.
SQL>
SQL>
SQL> insert into employees values(1,"Jason",  "N",  "TRAINER", 2,   date "1965-12-18",  800 , NULL,  10);
1 row created.
SQL> insert into employees values(2,"Jerry",  "J",  "SALESREP",3,   date "1966-11-19",  1600, 300,   10);
1 row created.
SQL> insert into employees values(3,"Jord",   "T" , "SALESREP",4,   date "1967-10-21",  1700, 500,   20);
1 row created.
SQL> insert into employees values(4,"Mary",   "J",  "MANAGER", 5,   date "1968-09-22",  1800, NULL,  20);
1 row created.
SQL> insert into employees values(5,"Joe",    "P",  "SALESREP",6,   date "1969-08-23",  1900, 1400,  30);
1 row created.
SQL> insert into employees values(6,"Black",  "R",  "MANAGER", 7,   date "1970-07-24",  2000, NULL,  30);
1 row created.
SQL> insert into employees values(7,"Red",    "A",  "MANAGER", 8,   date "1971-06-25",  2100, NULL,  40);
1 row created.
SQL> insert into employees values(8,"White",  "S",  "TRAINER", 9,   date "1972-05-26",  2200, NULL,  40);
1 row created.
SQL> insert into employees values(9,"Yellow", "C",  "DIRECTOR",10,  date "1973-04-27",  2300, NULL,  20);
1 row created.
SQL> insert into employees values(10,"Pink",  "J",  "SALESREP",null,date "1974-03-28",  2400, 0,     30);
1 row created.
SQL>
SQL> select bdate
  2  ,      extract(month from bdate) as month_of_birth
  3  from   employees;
BDATE     MONTH_OF_BIRTH
--------- --------------
18-DEC-65             12
19-NOV-66             11
21-OCT-67             10
22-SEP-68              9
23-AUG-69              8
24-JUL-70              7
25-JUN-71              6
26-MAY-72              5
27-APR-73              4
28-MAR-74              3
10 rows selected.
SQL>
SQL> drop table employees;
Table dropped.
SQL>
SQL>


EXTRACT(SECOND FROM TO_TIMESTAMP("01-JAN-15:26", "DD-MON-YYYY HH24:MI:SS")) AS SECOND

SQL> SELECT
  2    EXTRACT(SECOND FROM TO_TIMESTAMP("01-JAN-2005 19:15:26",
  3      "DD-MON-YYYY HH24:MI:SS")) AS SECOND
  4  FROM dual;
    SECOND
----------
        26
SQL>


EXTRACT(TIMEZONE_ABBR FROM TO_TIMESTAMP_TZ("01-JAN-15:26 PST", "DD-MON-YYYY HH24:MI:SS TZR"))

SQL> SELECT
  2    EXTRACT(TIMEZONE_ABBR FROM TO_TIMESTAMP_TZ(
  3      "01-JAN-2005 19:15:26 PST", "DD-MON-YYYY HH24:MI:SS TZR"))
  4      AS TZA
  5  FROM dual;
TZA
----------
PST
SQL>


EXTRACT(TIMEZONE_MINUTE FROM TO_TIMESTAMP_TZ("01-JAN-15:26 -7:15", "DD-MON-YYYY HH24:MI:SS TZH:TZM"))

SQL> SELECT
  2    EXTRACT(TIMEZONE_MINUTE FROM TO_TIMESTAMP_TZ(
  3      "01-JAN-2005 19:15:26 -7:15", "DD-MON-YYYY HH24:MI:SS TZH:TZM"))
  4      AS TZM
  5  FROM dual;
       TZM
----------
       -15
SQL>


EXTRACT(TIMEZONE_REGION FROM TO_TIMESTAMP_TZ("01-JAN-15:26 PST", "DD-MON-YYYY HH24:MI:SS TZR"))

SQL> SELECT
  2    EXTRACT(TIMEZONE_REGION FROM TO_TIMESTAMP_TZ(
  3      "01-JAN-2005 19:15:26 PST", "DD-MON-YYYY HH24:MI:SS TZR"))
  4      AS TZR
  5  FROM dual;
TZR
----------------------------------------------------------------
PST
SQL>


EXTRACT() to extract and return a year, month, day, hour, minute, second, or time zone from the timestamp types or a DATE.

SQL>
SQL> SELECT
  2    EXTRACT(YEAR FROM TO_DATE("01-JAN-2005 19:15:26",
  3      "DD-MON-YYYY HH24:MI:SS")) AS YEAR
  4  FROM dual;
      YEAR
----------
      2005
SQL>
SQL>


extract year from birthday

SQL>
SQL> create table employees(
  2    empno      NUMBER(4)
  3  , ename      VARCHAR2(8)
  4  , init       VARCHAR2(5)
  5  , job        VARCHAR2(8)
  6  , mgr        NUMBER(4)
  7  , bdate      DATE
  8  , msal       NUMBER(6,2)
  9  , comm       NUMBER(6,2)
 10  , deptno     NUMBER(2) ) ;
Table created.
SQL>
SQL>
SQL> insert into employees values(1,"Jason",  "N",  "TRAINER", 2,   date "1965-12-18",  800 , NULL,  10);
1 row created.
SQL> insert into employees values(2,"Jerry",  "J",  "SALESREP",3,   date "1966-11-19",  1600, 300,   10);
1 row created.
SQL> insert into employees values(3,"Jord",   "T" , "SALESREP",4,   date "1967-10-21",  1700, 500,   20);
1 row created.
SQL> insert into employees values(4,"Mary",   "J",  "MANAGER", 5,   date "1968-09-22",  1800, NULL,  20);
1 row created.
SQL> insert into employees values(5,"Joe",    "P",  "SALESREP",6,   date "1969-08-23",  1900, 1400,  30);
1 row created.
SQL> insert into employees values(6,"Black",  "R",  "MANAGER", 7,   date "1970-07-24",  2000, NULL,  30);
1 row created.
SQL> insert into employees values(7,"Red",    "A",  "MANAGER", 8,   date "1971-06-25",  2100, NULL,  40);
1 row created.
SQL> insert into employees values(8,"White",  "S",  "TRAINER", 9,   date "1972-05-26",  2200, NULL,  40);
1 row created.
SQL> insert into employees values(9,"Yellow", "C",  "DIRECTOR",10,  date "1973-04-27",  2300, NULL,  20);
1 row created.
SQL> insert into employees values(10,"Pink",  "J",  "SALESREP",null,date "1974-03-28",  2400, 0,     30);
1 row created.
SQL>
SQL>
SQL> select bdate
  2  ,      extract(year  from bdate) as year_of_birth
  3  from   employees;
BDATE     YEAR_OF_BIRTH
--------- -------------
18-DEC-65          1965
19-NOV-66          1966
21-OCT-67          1967
22-SEP-68          1968
23-AUG-69          1969
24-JUL-70          1970
25-JUN-71          1971
26-MAY-72          1972
27-APR-73          1973
28-MAR-74          1974
10 rows selected.
SQL>
SQL> drop table employees;
Table dropped.
SQL>
SQL>


Extract year, month, day from a date

SQL> create table emp
  2  ( empno      NUMBER(4)    constraint E_PK primary key
  3  , ename      VARCHAR2(8)
  4  , init       VARCHAR2(5)
  5  , job        VARCHAR2(8)
  6  , mgr        NUMBER(4)
  7  , bdate      DATE
  8  , sal       NUMBER(6,2)
  9  , comm       NUMBER(6,2)
 10  , deptno     NUMBER(2)    default 10
 11  ) ;
Table created.
SQL> insert into emp values(1,"Tom","N",   "Coder", 13,date "1965-12-17",  800 , NULL,  20);
1 row created.
SQL> insert into emp values(2,"Jack","JAM", "Tester",6,date "1961-02-20",  1600, 300,   30);
1 row created.
SQL> insert into emp values(3,"Wil","TF" ,  "Tester",6,date "1962-02-22",  1250, 500,   30);
1 row created.
SQL> insert into emp values(4,"Jane","JM",  "Designer", 9,date "1967-04-02",  2975, NULL,  20);
1 row created.
SQL> insert into emp values(5,"Mary","P",  "Tester",6,date "1956-09-28",  1250, 1400,  30);
1 row created.
SQL> insert into emp values(6,"Black","R",   "Designer", 9,date "1963-11-01",  2850, NULL,  30);
1 row created.
SQL> insert into emp values(7,"Chris","AB",  "Designer", 9,date "1965-06-09",  2450, NULL,  10);
1 row created.
SQL> insert into emp values(8,"Smart","SCJ", "Coder", 4,date "1959-11-26",  3000, NULL,  20);
1 row created.
SQL> insert into emp values(9,"Peter","CC",   "Designer",NULL,date "1952-11-17",  5000, NULL,  10);
1 row created.
SQL> insert into emp values(10,"Take","JJ", "Tester",6,date "1968-09-28",  1500, 0,     30);
1 row created.
SQL> insert into emp values(11,"Ana","AA",  "Coder", 8,date "1966-12-30",  1100, NULL,  20);
1 row created.
SQL> insert into emp values(12,"Jane","R",   "Manager",   6,date "1969-12-03",  800 , NULL,  30);
1 row created.
SQL> insert into emp values(13,"Fake","MG",   "Coder", 4,date "1959-02-13",  3000, NULL,  20);
1 row created.
SQL> insert into emp values(14,"Mike","TJA","Manager",   7,date "1962-01-23",  1300, NULL,  10);
1 row created.
SQL>
SQL>
SQL> select bdate
  2  ,      extract(year  from bdate) as year_of_birth
  3  ,      extract(month from bdate) as month_of_birth
  4  ,      extract(day   from bdate) as day_of_birth
  5  from   emp
  6  where  ename = "Peter";
BDATE      YEAR_OF_BIRTH MONTH_OF_BIRTH DAY_OF_BIRTH
---------- ------------- -------------- ------------
17-11-1952          1952             11           17
SQL>
SQL>
SQL> drop table emp;
Table dropped.


Order date value by only year field with extract() function

SQL>
SQL>
SQL> CREATE TABLE emp (
  2    emp_id               NUMBER,
  3    ename             VARCHAR2(40),
  4    hire_date        DATE DEFAULT sysdate,
  5    end_date DATE,
  6    rate     NUMBER(5,2),
  7    CONSTRAINT emp_pk
  8      PRIMARY KEY (emp_id)
  9  );
Table created.
SQL> INSERT INTO emp(emp_id, ename, hire_date,end_date, rate)VALUES (101, "Mary", to_date("15-Nov-1961","dd-mon-yyyy"),null,169);
1 row created.
SQL> INSERT INTO emp(emp_id, ename, hire_date,end_date, rate)VALUES (102, "Tom", to_date("16-Sep-1964","dd-mon-yyyy"),to_date("5-May-2004","dd-mon-yyyy"),135);
1 row created.
SQL> INSERT INTO emp(emp_id, ename, hire_date,end_date, rate)VALUES (104, "Peter", to_date("29-Dec-1987","dd-mon-yyyy"),to_date("1-Apr-2004","dd-mon-yyyy"),99);
1 row created.
SQL> INSERT INTO emp(emp_id, ename, hire_date,end_date, rate)VALUES (105, "Mike", to_date("15-Jun-2004","dd-mon-yyyy"),null,121);
1 row created.
SQL> INSERT INTO emp(emp_id, ename, hire_date,end_date, rate)VALUES (107, "Less", to_date("2-Jan-2004","dd-mon-yyyy"),null,45);
1 row created.
SQL> INSERT INTO emp(emp_id, ename, hire_date,end_date, rate)VALUES (108, "Park", to_date("1-Mar-1994","dd-mon-yyyy"),to_date("15-Nov-2004","dd-mon-yyyy"),220);
1 row created.
SQL> INSERT INTO emp(emp_id, ename, hire_date,end_date, rate)VALUES (110, "Ink", to_date("4-Apr-2004","dd-mon-yyyy"),to_date("30-Sep-2004","dd-mon-yyyy"),84);
1 row created.
SQL> INSERT INTO emp(emp_id, ename, hire_date,end_date, rate)VALUES (111, "Tike", to_date("23-Aug-1976","dd-mon-yyyy"),null,100);
1 row created.
SQL> INSERT INTO emp(emp_id, ename, hire_date,end_date, rate)VALUES (112, "Inn", to_date("15-Nov-1961","dd-mon-yyyy"),to_date("4-Apr-2004","dd-mon-yyyy"),70);
1 row created.
SQL> INSERT INTO emp(emp_id, ename, hire_date,end_date, rate)VALUES (113, "Kate", to_date("3-Mar-2004","dd-mon-yyyy"),to_date("31-Oct-2004","dd-mon-yyyy"),300);
1 row created.
SQL>
SQL> SET ECHO ON
SQL> SELECT e.emp_id "ID", e.ename "Name",
  2         e.hire_date "Hire Date"
  3  FROM emp e
  4  ORDER BY EXTRACT(YEAR FROM hire_date) DESC, ename ASC;
       110
Ink
04-APR-04
       113
Kate
03-MAR-04
       107
Less
02-JAN-04
       105
Mike
15-JUN-04
       108
Park
01-MAR-94
       104
Peter
29-DEC-87
       111
Tike
23-AUG-76
       102
Tom
16-SEP-64
       112
Inn
15-NOV-61
       101
Mary
15-NOV-61

10 rows selected.
SQL>
SQL> drop table emp;
Table dropped.


SELECT EXTRACT(MONTH FROM TO_DATE("01-JAN-15:26","DD-MON-YYYY HH24:MI:SS")) As MONTH

SQL> SELECT EXTRACT(MONTH FROM TO_DATE("01-JAN-2005 19:15:26","DD-MON-YYYY HH24:MI:SS")) As MONTH
  2  FROM dual;
     MONTH
----------
         1
SQL>


SELECT EXTRACT(YEAR FROM TO_DATE("01-JAN-15:26","DD-MON-YYYY HH24:MI:SS")) AS YEAR

SQL> SELECT EXTRACT(YEAR FROM TO_DATE("01-JAN-2005 19:15:26","DD-MON-YYYY HH24:MI:SS")) AS YEAR
  2  FROM dual;
      YEAR
----------
      2005
SQL>