Lecture 11sdcdc
Lecture 11sdcdc
LECTURE 11
Today’s Agenda
• Functions In Oracle
• Date Arithmetic
Functions In Oracle
You might think that the above query is wrong and will give
error. But it is not so.
It has just one column named DUMMY with the data type
VARCHAR2 and contains one row with a value X.
The Dual Table
So for all our general purpose queries and function calls we will
be using DUAL
Date Functions In Oracle
Given on next slide are the most commonly used Oracle Date
Functions .
.
Function Description
sysdate() Returns the current system date and time
Syntax
ADD_MONTHS(date_expression, month)
Return Value
If date_expression is the last day of the month, the resulting date is always
the last day of the month e.g., adding 1 month to 29-FEB-2020 will result in
31-MAR-2020, not 29-MAR-2020.
Otherwise, the function returns a date whose day is the same as the day
component of the date_expression
The Function
Add_months()
The Function
Add_months()
The Function
Last_day()
Syntax
LAST_DAY(date)
Return Value
Always returns a DATE value that represents the last day of the month
of that input date.
The Function
Last_day()
The Function
Next_day()
Syntax
NEXT_DAY(date,weekday)
The weekday can be full name e.g., Tuesday or abbreviation e.g., Tue.
Return Value
Always returns a DATE value that represents the next weekday after the
date.
The Function
Next_day()
The Function
Months_between()
Syntax
MONTHS_BETWEEN (date1, date2)
Return Value
If date1 comes after date2, then MONTHS_BETWEEN returns a positive number.
If date1 comes before date2, then MONTHS_BETWEEN returns a negative
number.
If date1 and date2 both fall on the last day or same day of their respective
months, then MONTHS_BETWEEN returns a whole number (no fractional
component).
If date1 and date2 are in different months and at least one of the dates is not a
last day in the month, MONTHS_BETWEEN returns a fractional number.
The fractional component is calculated on a 31-day month basis
The Function
Months_between()
The Function Greatest()
This function returns the greatest date from the set of dates
passed as argument.
Syntax
GREATEST(date1, date2, . . . . .)
Return Value
The date which is greater.
A greater date is one which falls after the other dates.
The Function Greatest()
This function returns the smallest date from the set of dates
passed as argument.
Syntax
LEAST(date1, date2, . . . . .)
Return Value
The date which is smallest.
A smaller date is one which falls before the other dates.
The Function Least()
Syntax
EXTRACT(<field> FROM <source_date>)
Field can be YEAR / MONTH / DAY
Return Value
Returns the value of the field of the source.
The Function Least()
Oracle Date Arithmetic
These are :
Adding or subtracting a number to or from a date
Subtracting two dates to find the number of days between those
Adding hours to a date
Adding And Subtracting
Days
Surprised !!!
Suppose you are hired on a new job today , but with a salary
which is less than what you had expected . However the HR
manager has promised to raise your salary on the first day of
the next month after 6 months.
Find out:
Your increment date
No of days you have to wait for increment
Queries