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:
SELECT CURRENT_DATE FROM DUAL;
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:
select sysdate from dual;
Output:
oracle date function 1
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:
EXTRACT (component from source)
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:
In this example we are going to extract the year from a date.
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:
oracle date function 2
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:
TO_DATE(string, format, nls_language)
Parameters:
string: It refers to the date in string type which we want to convert.
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:
In this example we will convert a date in string to date value.
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
It is used to convert a date from DATE value to a specified date format.
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
TO_CHAR( sysdate, 'DD-MM-YYYY' )NEW_DATE FROM dual;
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
LAST_DAY(sysdate) LAST_DAY FROM dual;
Output:
oracle date function 5
As we can see in the screen shot the query displays the last day of the month of April.
7. MONTHS_BETWEEN
This function is used to measure the months between two dates.
It takes two parameters as arguments.
Syntax:
MONTHS_BETWEEN(from_date, to_date)
Parameters:
from_date: It refers to the date which is subtracted from.
to_date: It refers to the date which is to be subtracted.
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:
SELECT MONTHS_BETWEEN( sysdate, DATE '2011-04-02' ) MONTH_DIFFERENCE FROM DUAL;
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:
expression: It refers to the date value.
N: It represents the number of months.
Example:
To get the today system day date after 2 months using the ADD_MONTHS function.
Code:
SELECT ADD_MONTHS( sysdate, 2 ) NEWDATE FROM dual;
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:
SELECT CURRENT_TIMESTAMP FROM dual;
Output:
CURRENT_TIMESTAMP
10. DBTIMEZONE
This represents the database time zone.
Syntax:
DBTIMEZONE
Example:
To get the current database time zone.
Code:
SELECT DBTIMEZONE FROM dual;
Output:
DBTIMEZONE
11. FROM_TZ
This function converts the TIMESTAMP to TIMESTAMP with TIME ZONE value.
Syntax:
FROM_TZ(timestamp, timezone)
Parameters:
timestamp: It refers to the timestamp value.
timezone: It is a character string TZH:TZM.
Example:
To convert a timestamp to a timestamp with timezone value.
Code:
SELECT FROM_TZ(TIMESTAMP '2020-05-01 19:35:10', '-07:00')NEWVALUE FROM DUAL;
Output:
oracle date function 10
12. NEW_TIME()
This function converts a date from one time zone to a different time zone.
Syntax:
NEW_TIME(date, from_timezone, new_timezone)
Parameters:
date: It refers to the date which we want to convert.
from_timezone: It refers to the time zone of the date.
new_timezone: It refers to the time zone to which we want to convert.
Code:
SELECT NEW_TIME( sysdate, 'PST', 'AST' ) TIME_IN_AST FROM DUAL;
Output:
oracle date function 11
13. ROUND
This function rounds the date to a specific format.
Syntax:
ROUND(date, format)
Parameters:
date: It represents the date which we want to round.
format: It represents the format to which we want to round.
Example:
In this example we will round the current date 01-May-2020 20:27:15 to nearest date.
Code:
SELECT TO_CHAR( ROUND( TO_DATE( '01-May-2020 20:27:15', 'DD-Mon-YYYY HH24:MI:SS' ) ),
'DD-Mon-YYYY HH24:MI:SS' ) rounded_date
FROM
dual;
Output:
oracle date function 12
14. SESSIONTIMEZONE
This function as the name suggest returns the time zone of the current working session.
Syntax:
SESSIONTIMEZONE
Example:
To get the time zone of the current working session.
Code:
SELECT SESSIONTIMEZONE FROM dual;
Output:
oracle date function
15. SYSTIMESTAMP
This function represents a timestamp with a time zone. It displays the result up to fractional seconds.
Syntax:
SYSTIMESTAMP
Example:
In this example we will try to get the system timestamp.
Code:
SELECT SYSTIMESTAMP FROM dual;
Output:
oracle date function
16. TRUNC
This function returns a date value truncated to a specific format/unit.
Syntax:
TRUNC(date, format)
Parameters:
date: It represents the date value which is to be truncated.
format: It refers to the unit to which the date value will be truncated.
Example:
To get the first day of the current month.
Code:
SELECT TRUNC( SYSDATE, 'MM' ) MONTH FROM dual;
Output:
oracle date function 15JPG
17. TZ_OFFSET
This function returns offset of a time zone name from UTC.
Syntax:
TZ_OFFSET(value)
Parameter:
value: It refers to a valid time zone.
Example:
We will get the offset of time zone ‘Asia/Kolkata.
Code:
SELECT TZ_OFFSET('Asia/Kolkata') OFFSET FROM DUAL;
Output:
TZ_OFFSET
Introduction To Oracle Strings
Functions
Oracle has several functions to operate on string characters. Some of which
are ‘Char’ to convert a number into a character, ‘Locate’ to locate a letter or
a word in a string set, ‘Space’ to leave a blank space, ‘Length’ to get the
number of characters in a string, ‘Replace’ to replace a word or a letter,
‘TRIM’ to remove a letter or a word from the start or end of a string, etc.
Features of Oracle String Function
An oracle string function has special key features that are as under:
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.
Example: contains (text, ‘function’)>0
2. Equals
The equal string function comes into use to examine equality and to get an
exact match which returns to a true value.
Example: Equals (text = ‘function’)
3. Ends with
This method finds the new value, which contains a string from starting.
Example: Ends with (<suffix>)
4. Starts with
This method gets the new value contains a starting string.
Popular Course in this category
Oracle Training (14 Courses, 8+ Projects) 14 Online Courses | 8 Hands-
on Projects | 120+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (6,796 ratings)
Course Price
₹6999 ₹41999
View Course
Related Courses
Oracle DBA Database Management System Training (2 Courses)All in One Financial Analyst
Bundle (250+ Courses, 40+ Projects)
Example: Starts with(<prefix>)
5. EqualsignoreCase
The equal sign or case function is for comparing a particular string to
others by ignoring the considerations of the case.
Example: EqualsIgnoreCase (String other string)
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
replacement in order to get char.
Example: Replace (char,<search string>,<replacement string>)
9. ReplaceAll
This Function is used to replaces all substring of the string which matches
the given regex with the given replacement.
Example: ReplaceAll(<oldvalregex>,<newal>
10. Split
Splits the string around matches that given regular expression.
Example: Text.Split(<regexpattern>
String valuation consists of different parameter areas
For the variable during field validation: new value
Parameter name used to reclaim the value of the field after the form
is submitted: old value
Before stored value during field validation, the variable names would
be used instead of me string var
Oracle functions are sorted under different categories, and the string is of
them. It can be used in SQL statements or questions of Oracle,
programming environment of Oracle database such as saved triggers,
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,
NCHR, REGEXP INSTR, VSIZE, REGEXP, REPLACE, REGEXP SUBSTR, RPAD,
SOUNDEX, RTRIM, SUBSTR, TRIM, TRANSLATE, UPPER. The string functions
used in programming culture, which help in smooth functioning.
Common Oracle String Functions
Below are the most common Oracle string functions which help in
manipulating the string character effectively.
1. ASCII: The ASCII code comparable to the one character in the expression
return.
Example: ASCII (‘a’)
2. Bit_Length: Return length in bits of a particular string; each Unicode
value of the character is 2bytes in length (equal to 16bits)
Example: Bit_Length (‘abcdef’)
3. Char; It converts a numeric value to the analogous ASCII Character code.
Example: Char (35)
4. Char_Length: Blanks are not counted in string length. Return length in
the number of characters of a particular string.
Example: Char_Length
5. Concat: Concat string function allows a particular string at one end and
back to the same string.
Example: Concat (‘text a’). concat (‘text b’)
6. Insert: A specified string character into a particular location in other
string characters.
Example: select insert (‘123456’),2, 3, ‘abcd’
7. Left: Specified number of character from the left of a string
Example: select left (‘123456’, 3)
8. Length: Return the length, number of character of a particular string. The
length is returned to exclude any blank characters.
Example: Length (Customer_Name)
9. Locate: This function comes into use to search string in other string, but
it is not found the string it returns to its original index that is 0.
Example: Locate (‘d’ ‘abcdef’)
10. LocateN: Return the numeric position of a string character in other
character string. This includes an integer that enables to specify an initial
position to start the search.
Example: Locate (‘d’, ‘abcdef’,3)
11. Lower: This converts a string character to lowercase
Example: Lower(Customer_Name)
12. Ortet_Length: Return a number of bytes of a particular string.
Example: Octet_Length (‘abcdef’)
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’)
14. Repeat: Repeat a particular expression pie times.
Example: Repeat (‘abc’,4)
15. Replace: Replace one or more character from a particular character
expression with one or more characters.
Example: Replace (‘abc1234’, ‘123’ ,‘zz’)
16. Right: Return a particular number of characters from the right of the
string.
Example: SELECT Right (‘123456’,3)
17. Space: Insert blank space
Example: Space (2)
18. Substring: This function permits you to excerpt substring from the
original string.
Example: Substring (‘abcdef’)
19. SubstringN: SubstringN help you to get the length of the string, which
includes an integer in the character number.
Example: Substring (‘abcdef’)
20. TrimBoth: Particular strips leading & trailing character from a character
string.
Example: Trim (Both ‘_’ From ‘abcdef’)
21. TrimLeading: Particular strips leading characters from a character
string.
Example: Trim (LEADING ‘_’ From ‘_abcdef_’)
22. Trim Trailing: Particular trailing characters from a character string.
Example: Trim (TRAILING ‘_’ From ‘abcdef_’)
23. Upper: It converts a string character to uppercase
Example: Upper (Cutomer_Name)