You are on page 1of 3

Oracle DATE

The DATE data type stores the year (which includes the century), the month, the
day, the hours, the minutes, and the seconds.

Oracle Database has its own propriety format for storing date data. It uses fixed-
length fields of 7 bytes, each corresponding to century, year, month, day, hour,
minute, and second to store date data.

Oracle date format

The standard(default) date format for input and output is DD-MON-YY e.g., 01-
JAN-17 which is controlled by the value of the NLS_DATE_FORMAT parameter.

Important :-

So even if you store as below while querying we can see as 01-MAR-85. But
internally hours,min,sec are also get stored in Date datatype column.

insert into test1 values(to_date('01/03/1985 05:40:30','dd/mm/yyyy hh:mi:ss'));

hence to see in column value as well as in output query min,sec we have to


change NLS_DATE_FORMAT as

ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD hh:mi:ss';

Format date using TO_CHAR() function

The TO_CHAR() function takes a DATE value, formats it based on a specified


format, and returns a date string.

For example, to display the current system date in a specific format, you use the
TO_CHAR() function as follows:

SELECT
TO_CHAR( SYSDATE, 'FMMonth DD, YYYY' )
FROM
dual;
The output is: August 1, 2017

The language that the TO_CHAR()function uses for displaying the month name is
controlled by the NLS_DATE_LANGUAGE parameter:

SELECT
value
FROM
V$NLS_PARAMETERS
WHERE
parameter = 'NLS_DATE_LANGUAGE';

ALTER SESSION SET NLS_DATE_LANGUAGE = 'FRENCH';

Now, you can execute the TO_CHAR() function again to see the effect:

SELECT
TO_CHAR( SYSDATE, 'FMMonth DD, YYYY' )
FROM
dual;
Output:- Août 1, 2017

Convert string to date


Because Oracle uses an internal format for storing the DATE data, you often have
to convert a string to a date value before storing it in the date column.

To convert date values that are not in the standard format, you use the
TO_DATE() function with a format string.

SELECT
TO_DATE( 'August 01, 2017', 'MONTH DD, YYYY' )
FROM
dual;
Date literals

Besides using the TO_DATE() function , you can specify a DATE value as a string
literal using the following syntax:
insert into test2 values(DATE '2017-11-01');

Oracle TIMESTAMP

The TIMESTAMP data type allows you to store date and time data including year,
month, day, hour, minute and second.

In addition, it stores the fractional seconds, which is not stored by the DATE data
type.

digits in the fractional part of the SECOND field ranges from 0 to 9, meaning that
you can use the TIMESTAMP data type to store up to nanosecond.

Oracle TIMESTAMP literals


To specify TIMESTAMP literals, you use the following format:

TIMESTAMP 'YYYY-MM-DD HH24:MI:SS.FF'

Extract TIMESTAMP components


To extract components a TIMESTAMP such as year, month, day, hour,minute, and
second, you use the EXTRACT() function:

Syntax - EXTRACT( component FROM timestamp);

Default TIMESTAMP format

Oracle uses the NLS_TIMESTAMP_FORMAT parameter to control the default


timestamp format when a value of the character type is converted to the
TIMESTAMP data type.

SELECT
value
FROM
V$NLS_PARAMETERS
WHERE
parameter = 'NLS_TIMESTAMP_FORMAT';
Output -> DD-MON-RR HH.MI.SSXFF AM

You might also like