Oracle Date Functions With Examples
Oracle Date Functions With Examples
Given below are the various oracle date functions with examples:
1. CURRENT_DATE
This function is used to get the current date in the session time zone. It requires no parameters and
is a very simple function.
Syntax:
CURRENT_DATE
Example:
In this example we will try to find the current date of the session using this function.
Code:
In the output we will see the current date of the session time zone.
2. SYSDATE
This function returns the current date and time of the Operating system in which the Oracle
database is installed.
Syntax:
SYSDATE
Example:
In this example we are going to find the sysdate of the operating system in which the current
database is installed.
Code:
Output:
As, we can see the screen shot shows us the system date.
3. EXTRACT
This extract function in Oracle is used to retrieve a specific component which can be year, day,
month, hour, minute, second from a date value.
Syntax:
Parameters:
component: It refers to the component we want to extract (year, day, month, hour, minute, second).
source: It refers to the value (DATE, TIMESTAMP) from which we want to extract.
Example:
Code:
SELECT
EXTRACT( YEAR FROM TO_DATE( '29-Apr-2020 05:30:20 ', 'DD-Mon-YYYY HH24:MI:SS' ) ) YEAR
FROM DUAL;
We have used to_date function.
Output:
In the above screen shot we can see that the year has been successfully extracted.
4. TO_DATE
This function converts a date which is in string type to date value. It takes three arguments.
Syntax:
Parameters:
format: It refers to the date and time format in which we want to convert and it is an optional
parameter.
nls_language: It refers to the language for the day and month names. It is also an optional
parameter.
Example:
Code:
SELECT
TO_DATE( '20 APR 2020', 'DD MON YYYY' )CONVERTED_DATE FROM dual;
Output:
oracle date function 3
As we can see in the screen shot the DATE value has been converted into a specific format.
5. TO_CHAR
Syntax:
TO_CHAR(expression, date_format)
Parameters:
expression: It refers to the DATE or an INTERVAL value which needs to be converted. The expression
can be of type DATE OR TIMESTAMP
date_format: It refers to the specified format in which we are going to convert the expression. It is
optional parameter.
Example:
In this example we are going to convert the system date or current date into a string value in a
format DD-MM-YYYY.
Code:
SELECT
Output:
TO_CHAR
As, we can see in the screen shot the sysdate has been converted in a specified format.
6. LAST_DAY
This function is used to return the last day of the month of the particular date. It takes a DATE
argument as a parameter.
Syntax:
LAST_DAY(date)
Parameter:
date: This refers to the date value for which we want to get the last day of the month.
Example:
In this example we are going to extract the last day of the month of sysdate.
Code:
SELECT
Output:
As we can see in the screen shot the query displays the last day of the month of April.
7. MONTHS_BETWEEN
Syntax:
MONTHS_BETWEEN(from_date, to_date)
Parameters:
Example:
In this example we will calculate the months between system date and the date on which India won
its second cricket world cup which was 2 April 2011.
Code:
Output:
MONTHS_BETWEEN
As we can see in the screen shot the query displays the months between the two dates.
8. ADD_MONTHS
This function adds N months to a date and returns the same day N month after.
Syntax:
ADD_MONTHS(expression, N)
Parameters:
Example:
To get the today system day date after 2 months using the ADD_MONTHS function.
Code:
Output:
ADD_MONTHS
9. CURRENT_TIMESTAMP
This function returns the current date and time in the session time zone.
Syntax:
CURRENT_TIMESTAMP
Example:
Let us try to get the current time stamp of this particular session time zone.
Code:
Output:
CURRENT_TIMESTAMP
10. DBTIMEZONE
This represents the database time zone.
Syntax:
DBTIMEZONE
Example:
Code:
Output:
DBTIMEZONE
11. FROM_TZ
This function converts the TIMESTAMP to TIMESTAMP with TIME ZONE value.
Syntax:
FROM_TZ(timestamp, timezone)
Parameters:
Example:
Output:
12. NEW_TIME()
This function converts a date from one time zone to a different time zone.
Syntax:
Parameters:
Code:
Output:
13. ROUND
Syntax:
ROUND(date, format)
Parameters:
Example:
In this example we will round the current date 01-May-2020 20:27:15 to nearest date.
Code:
FROM
dual;
Output:
14. SESSIONTIMEZONE
This function as the name suggest returns the time zone of the current working session.
Syntax:
SESSIONTIMEZONE
Example:
Output:
15. SYSTIMESTAMP
This function represents a timestamp with a time zone. It displays the result up to fractional seconds.
Syntax:
SYSTIMESTAMP
Example:
Code:
Output:
16. TRUNC
Syntax:
TRUNC(date, format)
Parameters:
format: It refers to the unit to which the date value will be truncated.
Example:
Code:
Output:
17. TZ_OFFSET
Syntax:
TZ_OFFSET(value)
Parameter:
Example:
Output:
TZ_OFFSET
a word in a string set, ‘Space’ to leave a blank space, ‘Length’ to get the
‘TRIM’ to remove a letter or a word from the start or end of a string, etc.
1. Contains
Contain String function followed by a pattern like >0; this means that for
the particular row which was selected, the calculated score value greater
than Zero.
3. Ends with
This method finds the new value, which contains a string from starting.
4. Starts with
This method gets the new value contains a starting string.
Related Courses
Oracle DBA Database Management System Training (2 Courses)All in One Financial Analyst
Bundle (250+ Courses, 40+ Projects)
5. EqualsignoreCase
The equal sign or case function is for comparing a particular string to
6. IsEmpty
The isEmpty function is come into use for the string to verify that the length
() is zero.
Example: IsEmpty ()
7. Matches
The matches function is all about particular string matches with the
regex(regular expression).
Example: Text.matches(regex,string)
8. Replace
Replace function is all about the string search-replace with string
9. ReplaceAll
This Function is used to replaces all substring of the string which matches
Example: ReplaceAll(<oldvalregex>,<newal>
10. Split
Splits the string around matches that given regular expression.
Example: Text.Split(<regexpattern>
Parameter name used to reclaim the value of the field after the form
Before stored value during field validation, the variable names would
Oracle functions are sorted under different categories, and the string is of
functions and the procedures, etc. This article explains the fundamentals of
the string functions. String functions are ASCII, ASCIISTR, CHR, COMPOSE,
CONCAT, CONVERT, LENGTH 4, LENGTHB, LENGTHC, LOWER, LPAD, LTRIM,
return.
Example: Char_Length
5. Concat: Concat string function allows a particular string at one end and
string characters.
Example: Lower(Customer_Name)
13. Position; This function comes into use to find a substring from a string
and search the location of the string in the substring. The function return to
the position of starting character when the substring is equal to the found
substring.
Example: Position (‘d’, ‘abcdef’)
16. Right: Return a particular number of characters from the right of the
string.
18. Substring: This function permits you to excerpt substring from the
original string.
20. TrimBoth: Particular strips leading & trailing character from a character
string.
string.