Functions, Operators, Date - Time Lab Manual: Dummy X Select 777 888 From Dual
Functions, Operators, Date - Time Lab Manual: Dummy X Select 777 888 From Dual
LAB MANUAL
Oracle Built in Functions
1) Single Row Functions: Single row or Scalar functions return a value for every row that is
processed in a query.
2) Group Functions: These functions group the rows of data based on the values returned by
the query. This is discussed in SQL GROUP Functions. The group functions are used to
calculate aggregate values like total or average, which return just one total or one average value
after processing a group of rows.
Numeric Functions: These are functions that accept numeric input and return numeric values
Character or Text Functions: These are functions that accept character input and can return
both character and number values.
Date Functions: These are functions that take values that are of datatype DATE as input and
return values of datatype DATE, except for the MONTHS_BETWEEN function, which returns a
number.
Conversion Functions: These are functions that help us to convert a value in one form to
another form. For Example: a null value into an actual value, or a value from one datatype to
another datatype like NVL, TO_CHAR, TO_NUMBER, TO_DATE etc.
You can combine more than one function together in an expression. This is known as nesting of
functions.
What is a DUAL Table in Oracle? This is a single row and single column dummy table
provided by oracle. This is used to perform mathematical calculations without using a table.
Output:
DUMMY
X
Output:
777 * 888
689976
1) Numeric Functions:
Numeric functions are used to perform operations on numbers. They accept numeric values as
input and return numeric values as output. Few of the Numeric functions are:
The following examples explains the usage of the above numeric functions
Character or text functions are used to manipulate text strings. They accept strings or characters as
input and can return both character and number values as output.
Few of the character or text functions are as given below:
Function Name Return Value
LOWER (string_value) All the letters in 'string_value'is converted to
UPPER (string_value) lowercase.
For Example, we can use the above UPPER () text function with the column value as follows.
RPAD (string_value, n,
pad_value) RPAD ('Good', 6, '*') Good**
3) Date Functions:
These are functions that take values that are of datatype DATE as input and return values of
datatypes DATE, except for the MONTHS_BETWEEN function, which returns a number as output.
ROUND (x, date_format) Returns the date 'x' rounded off to the nearest
century, year, month, date, hour, minute, or
second as specified by the'date_format'.
The below table provides the examples for the above functions
4) Conversion Functions:
These are functions that help us to convert a value in one form to another form. For Ex: a null value
into an actual value, or a value from one datatype to another datatype like NVL, TO_CHAR,
TO_NUMBER, TO_DATE.
NVL (x, y) If 'x' is NULL, replace it with 'y'. 'x'and 'y' must
be of the same datatype.
DECODE (a, b, c, d, e, default_value) Checks the value of 'a', if a = b, then returns 'c'.
If a = d, then returns 'e'. Else, returns
default_value
The below table provides the examples for the above functions
Numeric Functions:
These are functions that accept numeric input and return numeric values. Below are few of the
examples
ABS: Absolute value of the number
ABS (12)
---------
12
CEIL (48.99)
-----------
49
SQL> SELECT CEIL (48.11) FROM DUAL;
CEIL (48.11)
-----------
49
FLOOR: Integer value that is Less than or equal to the number
FLOOR (49.99)
------------
49
SQL> SELECT FLOOR (49.11) FROM DUAL;
FLOOR (49.11)
------------
49
ROUND: Rounded off value of the number 'x' up to the number 'y' decimal places
ROUND (49.11321, 2)
-----------------
49.11
ROUND (49.11321, 3)
-----------------
49.113
Function 9: Rtrim
Purpose: Trim the whitespaces in right the side of the string
Syntax: Rtrim(‘str’)
Example: SELECT RTRIM (‘ karu ‘) FROM DUAL;
Output….karu(. dot are spaces)
———————————————————————————————-
Function 10: Length
Purpose: length of the string
Syntax: length (‘str’)
Example: SELECT LENGTH (‘karuvachi’) FROM DUAL;
Output:9
———————————————————————————————-
Function 11: Instr
Purpose: Find the position of the string in another string
Syntax: Instr(‘str1′,’str2’)
Example: SELECT INSTR (‘karuvachi’,’ka’) FROM DUAL;
Output:1
———————————————————————————————-
Function 12: substr
Purpose: get a sub string from string
Syntax: substr(‘str’,start_pos,number_of_chars)
Example: SELECT substr(‘karuvachi’,2,4) FROM DUAL;
Output: aruv
To see the system date and time use the following functions:
CURRENT_DATE:
Returns the current date in the session time zone, in a value in the Gregorian calendar of datatype
DATE SYSDATE: Returns the current date and time.
SYSTIMESTAMP:
MON
Full Month like “January”, ”February”, ”April”
Month in Roman Characters (I-XII, I-Jan, II-Feb,…XII-Dec)
MONTH Quarter of the Month
RM Last two digits of the year.
Q Full year
YY Year in words like “Nineteen Ninety Nine”
YYYY Hours in 12 hour format
YEAR Hours in 12 hour format
HH Hours in 24 hour format
HH12 Minutes
HH24 Seconds
MI Fractional Seconds
SS
FF Milliseconds
Julian Day i.e Days since 1st-Jan-4712BC to till-date
SSSSS
J If the year is less than 50 Assumes the year as 21ST
Century. If the year is greater than 50 then assumes the
RR year in 20th Century.
Date and time functions and formats are quite different in various databases. In this article, let’s
review the most common functions that manipulates dates in an Oracle database
The function SYSDATE() returns a 7 byte binary data element whose bytes represents:
Century,
Year,
Month,
Day,
Hour,
Minute,
Second
Oracle enables you to extract the day, month, and year from
a date using an extract function:
Example:
select MONTHS_BETWEEN ('31-JAN-2014', '28-FEB-2014')
from dual
Date + number
Date – number
select sysdate - 1 as Yesterday
from dual
A VARCHAR2 or CHAR value can be implicitly converted to NUMBER or DATE type value by Oracle.
Similarly, a NUMBER or DATA type value can be automatically converted to character data by Oracle
server. Note that the impicit interconversion happens only when the character represents the valid
number or date type value respectively.
For example, examine the below SELECT queries. Both the queries will give the same result because
Oracle internally treats 15000 and '15000' as same.
Query-1
SELECT employee_id,first_name,salary
FROM employees
WHERE salary > 15000;
Query-2
SELECT employee_id,first_name,salary
FROM employees
WHERE salary > '15000';
SQL Conversion functions are single row functions which are capable of typecasting column value,
literal or an expression. TO_CHAR, TO_NUMBER and TO_DATE are the three functions which
perform cross modification of data types.
TO_CHAR function
TO_CHAR function is used to typecast a numeric or date input to character type with a format
model (optional).
Syntax
For number to character conversion, nls parameters can be used to specify decimal characters,
group separator, local currency model, or international currency model. It is an optional
specification - if not available, session level nls settings will be used. For date to character
conversion, the nls parameter can be used to specify the day and month names, as applicable.
Dates can be formatted in multiple formats after converting to character types using TO_CHAR
function. The TO_CHAR function is used to have Oracle 11g display dates in a particular format.
Format models are case sensitive and must be enclosed within single quotes.
Consider the below
SELECT query. The query format the HIRE_DATE and SALARY columns of EMPLOYEES table using
TO_CHAR function.
SELECT first_name,
TO_CHAR (hire_date, 'MONTH DD, YYYY') HIRE_DATE,
TO_CHAR (salary, '$99999.99') Salary
FROM employees
WHERE rownum < 5;
The first TO_CHAR is used to convert the hire date to the date format MONTH DD, YYYY i.e. month
spelled out and padded with spaces, followed by the two-digit day of the month, and then the four-
digit year. If you prefer displaying the month name in mixed case (that is, "December"), simply use
this case in the format argument: ('Month DD, YYYY').
The second TO_CHAR function in Figure 10-39 is used to format the SALARY to display the currency
sign and two decimal positions.
TO_NUMBER function
The TO_NUMBER function converts a character value to a numeric datatype. If the string being
converted contains nonnumeric characters, the function returns an error.
Syntax
TO_DATE function
The function takes character values as input and returns formatted date equivalent of the same.
The TO_DATE function allows users to enter a date in any format, and then it converts the entry
into the default format used by Oracle 11g.
Syntax: