Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
WEEK- 2 / A:
DATE AND
DATE FUNCTIONS
TO_CHAR
TO_DATE
MONTHS_BETWEEN
ADD_MONTHS
LAST_DAY
LEAST
NEXT_DAY
EXTRA
TO CREATE TABLE
SQL> CREATE TABLE MyFirstTable( ID NUMBER, NAME VARCHAR2(10) ) ;
TO INSERT RECORDS
SQL> INSERT INTO MyFirstTable VALUES(1, ’MARAT’);
SQL> INSERT INTO MyFirstTable VALUES(2, ’ALIYA’);
ID NAME
-------------------------
1 MARAT
2 ALIYA
SQL> CREATE TABLE MySecondTable( ID NUMBER,
NAME VARCHAR2(10) ,
DOB DATE) );
ID NAME DOB
------------------------------------------------------
1 NURLAN 11-FEB-1985
2 SAMAT 05-JAN-1986
DATE
Overview
Oracle supports both date and time.Rather than using two separate entities, date and
time, Oracle only uses one, DATE.
The DATE type is stored in a special internal format that includes not just the month,
day, and year, but also the hour, minute, and second.
For example, the following SQL statement creates a relation with an attribute of type
DATE:
Oracle's default format for DATE is "DD-MON-YY". Therefore, when you issue the query
Whenever a DATE value is displayed, Oracle will call TO_CHAR automatically with the default
DATE format.
However, you may override the default behavior by calling TO_CHAR explicitly with your own DATE
format.
For example,
SELECT TO_CHAR(B, 'YYYY/MM/DD') AS B FROM X; returns the result:
B
------------------
1998/04/01
The general usage of TO_CHAR is: TO_CHAR(<date>, '<format>')
where the <format> string can be formed from over 40 options. Some of the more
popular ones include:
Whenever Oracle expects a DATE value, it will automatically convert your input string
using TO_DATE according to the default DATE format "DD-MON-YY". For example, to
insert a tuple with a DATE attribute, you can simply type:
TO_DATE(<string>, '<format>')
SYSDATE
----------------
25-JAN-10
ADD_MONTHS(SYSDATE,2)
-----------------------------------------
25-MAR-10
MONTHS_BETWEEN(SYSDATE,'01-JAN-2009')
-------------------------------------------------------------------
12.33
NEXT_DAY : Date of next specified date following a date.
Options are SUN, MON, TUE, WED, THU, FRI, SAT
SQL> SELECT NEXT_DAY(SYSDATE,’WED’) AS RESULT FROM DUAL ;
RESULT
----------------
27-JAN-10
RESULT
----------------
28-FEB-10
RESULT
---------------
01-JAN-10
STUFF