Oracle PL/SQL Tutorial/SQL Data Types/Date Format

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

Содержание

AD or BC: AD or BC as appropriate

SQL>
SQL> SELECT TO_CHAR(SYSDATE, "AD")
  2  FROM dual;
TO
--
AD
SQL>


A.D. or B.C.: A.D. or B.C. as appropriate (2)

SQL>
SQL> SELECT TO_CHAR(SYSDATE, "A.D.")
  2  FROM dual;
TO_C
----
A.D.
SQL>


AM or PM: AM or PM as appropriate

SQL>
SQL> SELECT TO_CHAR(SYSDATE, "AM")
  2  FROM dual;
TO
--
PM


A.M. or P.M.: A.M. or P.M. as appropriate (2)

SQL>
SQL> SELECT TO_CHAR(SYSDATE, "A.M.")
  2  FROM dual;
TO_C
----
P.M.
SQL>


By default, the database outputs dates in the format DD-MON-YY

By default, the database outputs dates in the format DD-MON-YY, where

YY are the last two digits of the year.



SQL>
SQL> -- create demo table
SQL> create table Employee(
  2    ID                 VARCHAR2(4 BYTE)         NOT NULL,
  3    First_Name         VARCHAR2(10 BYTE),
  4    Last_Name          VARCHAR2(10 BYTE),
  5    Start_Date         DATE,
  6    End_Date           DATE,
  7    Salary             Number(8,2),
  8    City               VARCHAR2(10 BYTE),
  9    Description        VARCHAR2(15 BYTE)
 10  )
 11  /
Table created.
SQL>
SQL> -- prepare data
SQL> insert into Employee(ID,  First_Name, Last_Name, Start_Date,                     End_Date,                       Salary,  City,       Description)
  2               values ("01","Jason",    "Martin",  to_date("19960725","YYYYMMDD"), to_date("20060725","YYYYMMDD"), 1234.56, "Toronto",  "Programmer")
  3  /
1 row created.
SQL> insert into Employee(ID,  First_Name, Last_Name, Start_Date,                     End_Date,                       Salary,  City,       Description)
  2                values("02","Alison",   "Mathews", to_date("19760321","YYYYMMDD"), to_date("19860221","YYYYMMDD"), 6661.78, "Vancouver","Tester")
  3  /
1 row created.
SQL> insert into Employee(ID,  First_Name, Last_Name, Start_Date,                     End_Date,                       Salary,  City,       Description)
  2                values("03","James",    "Smith",   to_date("19781212","YYYYMMDD"), to_date("19900315","YYYYMMDD"), 6544.78, "Vancouver","Tester")
  3  /
1 row created.
SQL> insert into Employee(ID,  First_Name, Last_Name, Start_Date,                     End_Date,                       Salary,  City,       Description)
  2                values("04","Celia",    "Rice",    to_date("19821024","YYYYMMDD"), to_date("19990421","YYYYMMDD"), 2344.78, "Vancouver","Manager")
  3  /
1 row created.
SQL> insert into Employee(ID,  First_Name, Last_Name, Start_Date,                     End_Date,                       Salary,  City,       Description)
  2                values("05","Robert",   "Black",   to_date("19840115","YYYYMMDD"), to_date("19980808","YYYYMMDD"), 2334.78, "Vancouver","Tester")
  3  /
1 row created.
SQL> insert into Employee(ID,  First_Name, Last_Name, Start_Date,                     End_Date,                       Salary, City,        Description)
  2                values("06","Linda",    "Green",   to_date("19870730","YYYYMMDD"), to_date("19960104","YYYYMMDD"), 4322.78,"New York",  "Tester")
  3  /
1 row created.
SQL> insert into Employee(ID,  First_Name, Last_Name, Start_Date,                     End_Date,                       Salary, City,        Description)
  2                values("07","David",    "Larry",   to_date("19901231","YYYYMMDD"), to_date("19980212","YYYYMMDD"), 7897.78,"New York",  "Manager")
  3  /
1 row created.
SQL> insert into Employee(ID,  First_Name, Last_Name, Start_Date,                     End_Date,                       Salary, City,        Description)
  2                values("08","James",    "Cat",     to_date("19960917","YYYYMMDD"), to_date("20020415","YYYYMMDD"), 1232.78,"Vancouver", "Tester")
  3  /
1 row created.
SQL>
SQL>
SQL>
SQL> -- display data in the table
SQL> select * from Employee
  2  /
ID   FIRST_NAME LAST_NAME  START_DAT END_DATE      SALARY CITY       DESCRIPTION
---- ---------- ---------- --------- --------- ---------- ---------- ---------------
01   Jason      Martin     25-JUL-96 25-JUL-06    1234.56 Toronto    Programmer
02   Alison     Mathews    21-MAR-76 21-FEB-86    6661.78 Vancouver  Tester
03   James      Smith      12-DEC-78 15-MAR-90    6544.78 Vancouver  Tester
04   Celia      Rice       24-OCT-82 21-APR-99    2344.78 Vancouver  Manager
05   Robert     Black      15-JAN-84 08-AUG-98    2334.78 Vancouver  Tester
06   Linda      Green      30-JUL-87 04-JAN-96    4322.78 New York   Tester
07   David      Larry      31-DEC-90 12-FEB-98    7897.78 New York   Manager
08   James      Cat        17-SEP-96 15-APR-02    1232.78 Vancouver  Tester
8 rows selected.
SQL>
SQL>
SQL>
SQL>
SQL>
SQL> -- clean the table
SQL> drop table Employee
  2  /
Table dropped.


CC: Two-digit century

SQL> --
SQL>
SQL> SELECT TO_CHAR(SYSDATE, "CC")
  2  FROM dual;
TO
--
21
SQL>


Datetime Formatting Parameters for TO_CHAR() function

Aspect Parameter Description Example Century CC Two-digit century. 21 Century SCC Two-digit century with a negative sign (C) for B.C. -10 Quarter Q One-digit quarter of the year. 1 Year YYYY All four digits of the year. 2006 Year IYYY All four digits of the ISO year. 2006 Year RRRR All four digits of the rounded year, which depends on the current year. 2006 Year SYYYY All four digits of the year with a negative sign (C) for B.C. C1001 Year Y,YYY All four digits of the year with a comma. 2,006 Year YYY Last three digits of the year. 006 Year IYY Last three digits of the ISO year. 006 Year YY Last two digits of the year. 06 Year IY Last two digits of the ISO year. 06 Year RR Last two digits of the rounded year, which depends on the current year. 06 Year Y Last digit of the year. 6 Year I Last digit of the ISO year. 6 Year YEAR Name of the year in uppercase. TWO THOUSAND-SIX Year Year Name of the year with the first letter in uppercase. Two Thousand-Six Month MM Two-digit month of the year. 01 Month MONTH Full name of the month in uppercase, right-padded with spaces to a total length of nine characters. JANUARY Month Month Full name of the month with first letter in uppercase, right-padded with spaces to a total length of nine characters. January Month MON First three letters of the name of the month in uppercase. JAN Month Mon First three letters of the name of the month with the first letter in uppercase. Jan Month RM Roman numeral month. The Roman numeral month for the fourth month (April) is IV. Week WW Two-digit week of the year. 02 Week IW Two-digit ISO week of the year. 02 Week W One-digit week of the month. 2 Day DDD Three-digit day of the year. 103 Day DD Two-digit day of the month. 31 Day D One-digit day of the week. 5 Day DAY Full name of the day in uppercase. SATURDAY Day Day Full name of the day with the first letter in uppercase. Saturday Day DY First three letters of the name of the day in uppercase. SAT Day Dy First three letters of the name of the day with the first letter in uppercase. Sat Day J Julian day-the number of days that have passed since January 1, 4713 B.C. 2439892 Hour HH24 Two-digit hour in 24-hour format. 23 Hour HH Two-digit hour in 12-hour format. 11 Minute MI Two-digit minute. 57 Second SS Two-digit second. 45 Second FF[1..9] Fractional seconds with an optional number of digits to the right of the decimal point. Only applies timestamps,When dealing with 0.123456789 seconds, FF3 would round to 0.123. Second SSSSS Number of seconds past 12 a.m. 46748 Second MS Millisecond (millionths of a second). 100 Second CS Centisecond (hundredths of a second). 10 Separators -/,.;: "text" Characters that allow you to separate the aspects of a date and time. You can supply freeform text in quotes as a separator. When dealing with the date December 13, 1969, DD-MM-YYYY would produce 12-13-1969 and DD/MM/YYYY would produce 12/13/1969 Suffixes AM or PM AM or PM as appropriate. AM Suffixes A.M. or P.M. A.M. or P.M. as appropriate. P.M. Suffixes AD or BC AD or BC as appropriate. AD Suffixes A.D. or B.C. A.D. or B.C. as appropriate. B.C. Suffixes TH Suffix to a number. You can make the suffix uppercase by specifying the numeric format in uppercase and vice versa for lowercase.When dealing with a day number of 28, ddTH would produce 28th and DDTH would produce 28TH Suffixes SP Number is spelled out.When dealing with a day number of 28, DDSP would produce TWENTY-EIGHT and ddSP would produce twenty-eight Suffixes SPTH Combination of TH and SP.When dealing with a day number of 28, DDSPTH would produce TWENTY-EIGHTH and ddSPTH would produce twenty-eighth Era EE Full era name for Japanese Imperial, ROC Official, and Thai Buddha calendars. No example Era E Abbreviated era name. No example Time zones TZH Time zone hour. 12 Time zones TZM Time zone minute. 30 Time zones TZR Time zone region. PST Time zones TZD Time zone with daylight savings information. No example

Quote from:

Oracle Database 10g SQL (Osborne ORACLE Press Series) (Paperback)

# Paperback: 608 pages

# Publisher: McGraw-Hill Osborne Media; 1st edition (February 20, 2004)

# Language: English

# ISBN-10: 0072229810

# ISBN-13: 978-0072229813

DAY: Full name of the day in uppercase

SQL>
SQL> SELECT TO_CHAR(SYSDATE, "DAY")
  2  FROM dual;
TO_CHAR(S
---------
WEDNESDAY
SQL>


Day: Full name of the day with the first letter in uppercase

SQL>
SQL> SELECT TO_CHAR(SYSDATE, "Day")
  2  FROM dual;
TO_CHAR(S
---------
Wednesday
SQL>


DAY MON, YY AD

SQL>
SQL> SELECT TO_CHAR(SYSDATE, "DAY MON, YY AD")
  2  FROM dual;
TO_CHAR(SYSDATE,"DAY
--------------------
THURSDAY  MAY, 07 AD
SQL>


DDD: Three-digit day of the year

SQL>
SQL> SELECT TO_CHAR(SYSDATE, "DDD")
  2  FROM dual;
TO_
---
150
SQL>


DDSPTH "of" MONTH, YEAR A.D.

SQL>
SQL> SELECT TO_CHAR(SYSDATE, "DDSPTH "of" MONTH, YEAR A.D.")
  2  FROM dual;
TO_CHAR(SYSDATE,"DDSPTH"OF"MONTH,YEARA.D.")
----------------------------------------------------------------------------
THIRTY-FIRST of MAY      , TWO THOUSAND SEVEN A.D.
SQL>


DD:Two-digit day of the month

SQL>
SQL> SELECT TO_CHAR(SYSDATE, "DD")
  2  FROM dual;
TO
--
30
SQL>


D: One-digit day of the week

SQL>
SQL> SELECT TO_CHAR(SYSDATE, "D")
  2  FROM dual;
T
-
4
SQL>


DY: First three letters of the name of the day in uppercase

SQL>
SQL> SELECT TO_CHAR(SYSDATE, "DY")
  2  FROM dual;
TO_
---
WED
SQL>


Dy: First three letters of the name of the day with the first letter in uppercase

SQL>
SQL> SELECT TO_CHAR(SYSDATE, "Dy")
  2  FROM dual;
TO_
---
Wed
SQL>


HH24: Two-digit hour in 24-hour format

SQL>
SQL> SELECT TO_CHAR(SYSDATE, "HH24")
  2  FROM dual;
TO
--
21
SQL>


HH: Two-digit hour in 12-hour format

SQL>
SQL> SELECT TO_CHAR(SYSDATE, "HH")
  2  FROM dual;
TO
--
09
SQL>


I: Last digit of the ISO year

SQL>
SQL> SELECT TO_CHAR(SYSDATE, "I")
  2  FROM dual;
T
-
7
SQL>


Insert Date value with default format

SQL>
SQL>
SQL> CREATE TABLE myTable (
  2       name  VARCHAR2(25),
  3       price NUMBER(4,2),
  4       start_date DATE);
Table created.
SQL>
SQL>
SQL> INSERT INTO myTable VALUES (NULL, 2.5, "29-JUN-04");
1 row created.
SQL>
SQL> INSERT INTO myTable VALUES ("Product Name 3", null, "10-DEC-05");
1 row created.
SQL>
SQL> INSERT INTO myTable VALUES (NULL, NULL, "31-AUG-06");
1 row created.
SQL>
SQL> SELECT * FROM myTable;
NAME                           PRICE START_DAT
------------------------- ---------- ---------
                                 2.5 29-JUN-04
Product Name 3                       10-DEC-05
                                     31-AUG-06
3 rows selected.
SQL>
SQL>
SQL> drop table myTable;
Table dropped.
SQL>
SQL>


IW: Two-digit ISO week of the year

SQL>
SQL> SELECT TO_CHAR(SYSDATE, "IW")
  2  FROM dual;
TO
--
22
SQL>


IY: Last two digits of the ISO year

SQL>
SQL> SELECT TO_CHAR(SYSDATE, "IY")
  2  FROM dual;
TO
--
07
SQL>


IYY: Last three digits of the ISO year

SQL>
SQL> SELECT TO_CHAR(SYSDATE, "IYY")
  2  FROM dual;
TO_
---
007
SQL>


IYYY: All four digits of the ISO year

SQL>
SQL> SELECT TO_CHAR(SYSDATE, "IYYY")
  2  FROM dual;
TO_C
----
2007
SQL>


J: Julian day-the number of days that have passed since January 1, 4713 B.C.

SQL>
SQL> SELECT TO_CHAR(SYSDATE, "J")
  2  FROM dual;
TO_CHAR
-------
2454251
SQL>


MI: Two-digit minute

SQL>
SQL> SELECT TO_CHAR(SYSDATE, "MI")
  2  FROM dual;
TO
--
42
SQL>


MM: Two-digit month of the year

SQL>
SQL> SELECT TO_CHAR(SYSDATE, "MM")
  2  FROM dual;
TO
--
05


MON: First three letters of the name of the month in uppercase

SQL>
SQL> SELECT TO_CHAR(SYSDATE, "MON")
  2  FROM dual;
TO_
---
MAY


Mon: First three letters of the name of the month with the first letter in uppercase

SQL>
SQL> SELECT TO_CHAR(SYSDATE, "Mon")
  2  FROM dual;
TO_
---
May


MONTH: Full name of the month in uppercase, right-padded with spaces to a total length of nine characters

SQL>
SQL> SELECT TO_CHAR(SYSDATE, "MONTH")
  2  FROM dual;
TO_CHAR(S
---------
MAY


Month: Full name of the month with first letter in uppercase, right-padded with spaces to a total length of nine characters

SQL>
SQL> SELECT TO_CHAR(SYSDATE, "Month")
  2  FROM dual;
TO_CHAR(S
---------
May


Q: One-digit quarter of the year

SQL>
SQL>
SQL> SELECT TO_CHAR(SYSDATE, "Q")
  2  FROM dual;
T
-
2


RM:Roman numeral month.

SQL>
SQL> SELECT TO_CHAR(SYSDATE, "RM")
  2  FROM dual;
TO_C
----
V


RR: Last two digits of the rounded year, which depends on the current year

SQL>
SQL> SELECT TO_CHAR(SYSDATE, "RR")
  2  FROM dual;
TO
--
07
SQL>


RRRR: All four digits of the rounded year, which depends on the current year

SQL>
SQL> SELECT TO_CHAR(SYSDATE, "RRRR")
  2  FROM dual;
TO_C
----
2007
SQL>


SCC: Two-digit century with a negative sign (-) for B.C.

SQL>
SQL>
SQL> SELECT TO_CHAR(SYSDATE, "SCC")
  2  FROM dual;
TO_
---
 21


SELECT TO_CHAR(ADD_MONTHS(TO_DATE("01-JAN-15:26","DD-MON-YYYY HH24:MI:SS"), 2), "DD-MON-YYYY HH24:MI:SS") FROM dual;

TO_CHAR(ADD_MONTHS(T
--------------------
01-MAR-2005 19:15:26


SELECT TO_CHAR(SYSDATE, "DD/MM/YYYY")

2  FROM dual;
TO_CHAR(SY
----------
31/05/2007
SQL>


SELECT TO_CHAR(SYSDATE, "PM")

2  FROM dual;
TO
--
PM
SQL>


SELECT TO_CHAR(SYSDATE, "P.M.") (2)

2  FROM dual;
TO_C
----
P.M.
SQL>


SELECT TO_CHAR(TO_DATE("05-FEB-1968"), "MONTH DD, YYYY")

SQL> SELECT TO_CHAR(TO_DATE("05-FEB-1968"), "MONTH DD, YYYY")
  2  FROM dual;
TO_CHAR(TO_DATE("0
------------------
FEBRUARY  05, 1968
SQL>


SS: Two-digit second

SQL>
SQL> SELECT TO_CHAR(SYSDATE, "SS")
  2  FROM dual;
TO
--
40
SQL>


SYYYY: All four digits of the year with a negative sign (-) for B.C.

SQL>
SQL> SELECT TO_CHAR(SYSDATE, "SYYYY")
  2  FROM dual;
TO_CH
-----
 2007
SQL>


-/,.;: "text"

Characters that allow you to separate the aspects of a date and time.

You can supply freeform text in quotes as a separator.



SQL>
SQL> SELECT TO_CHAR(SYSDATE, "DD-MM-YYYY")
  2  FROM dual;
TO_CHAR(SY
----------
31-05-2007


TO_CHAR(SYSDATE, "BC")

SQL> SELECT TO_CHAR(SYSDATE, "BC")
  2  FROM dual;
TO
--
AD
SQL>


TO_CHAR(SYSDATE, "B.C.") (2)

SQL> SELECT TO_CHAR(SYSDATE, "B.C.")
  2  FROM dual;
TO_C
----
A.D.
SQL>


W: One-digit week of the month

SQL>
SQL> SELECT TO_CHAR(SYSDATE, "W")
  2  FROM dual;
T
-
5
SQL>


WW: Two-digit week of the year

SQL>
SQL> SELECT TO_CHAR(SYSDATE, "WW")
  2  FROM dual;
TO
--
22
SQL>


YEAR: Name of the year in uppercase

SQL>
SQL> SELECT TO_CHAR(SYSDATE, "YEAR")
  2  FROM dual;
TO_CHAR(SYSDATE,"YEAR")
------------------------------------------
TWO THOUSAND SEVEN
SQL>


Year: Name of the year with the first letter in uppercase

SQL> SELECT TO_CHAR(SYSDATE, "Year")
  2  FROM dual;
TO_CHAR(SYSDATE,"YEAR")
------------------------------------------
Two Thousand Seven
SQL>


Y: Last digit of the year

SQL> SELECT TO_CHAR(SYSDATE, "Y")
  2  FROM dual;
T
-
7
SQL>


YY: Last two digits of the year

SQL>
SQL> SELECT TO_CHAR(SYSDATE, "YY")
  2  FROM dual;
TO
--
07
SQL>


YYY: Last three digits of the year

SQL>
SQL> SELECT TO_CHAR(SYSDATE, "YYY")
  2  FROM dual;
TO_
---
007
SQL>


YYYY: All four digits of the year

SQL>
SQL> SELECT TO_CHAR(SYSDATE, "YYYY")
  2  FROM dual;
TO_C
----
2007
SQL>


Y,YYY: All four digits of the year with a comma

SQL>
SQL> SELECT TO_CHAR(SYSDATE, "Y,YYY")
  2  FROM dual;
TO_CH
-----
2,007
SQL>