0% found this document useful (0 votes)
64 views15 pages

Single Row Functions

Sql has built-in functions for calculations and data type conversions. There are two types of functions: single row functions that return one value per row, and multi-row functions that return aggregate values after grouping rows. Single row functions include string, numeric, date, and conversion functions. Multi-row functions calculate values for an entire group of rows, such as count, sum, average, min, and max.

Uploaded by

Manoj Babu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
64 views15 pages

Single Row Functions

Sql has built-in functions for calculations and data type conversions. There are two types of functions: single row functions that return one value per row, and multi-row functions that return aggregate values after grouping rows. Single row functions include string, numeric, date, and conversion functions. Multi-row functions calculate values for an entire group of rows, such as count, sum, average, min, and max.

Uploaded by

Manoj Babu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 15

FUNCTIONS

Sql has many built in functions for performing calculations on data and to convert the argument
to the expected datatype before performing the sql function.

There are two types of functions in oracle.

 Single row functions


 Multi row functions

Single row functions:-


Single row or scalar functions return a value for every row that is processed in a query.

Multi row functions:-


These functions group the row of data based on the values returned by the query. 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.

Dual table:-
This is a single row and single column dummy table provided by oracle. This used to perform
mathematical calculations without using a table.

SINGLE ROW FUNCTIONS:-

There are six types of single row functions

 String function
 Numeric function
 Date function
 Conversion function
 Null function
 Conditional function

String function:-

There are two types of string functions.

 Case function
 Character function
Case functions:-

 Upper case
 Lower case
 Initcap

UPPER CASE  convert a field to upper case

Syntax :-

SQL > Select upper (column name) from table name;

Example :-

SQL > Select upper (name) from emp;

LOWER CASE  covert a field to lower case

Syntax :-

SQL > Select lower (column name) from table name;

Example :-

SQL > Select lower (name) from emp;

INITCAP  convert a field to initcap

Syntax :-

SQL > Select initcap (column name) from table name;

Example :-

SQL > Select initcap (name) from emp;

Character functions:-

 Length
 Replace
 Translate
 Trim
 Ltrim
 Rtrim
 Lpad
 Rpad
 Instr
 Substr

LENGTH  Returns the length of a string in bytes


Syntax:-

SQL > select length(columnname) from tablename;

SQL > select length(‘string’) from dual;

Example :-

SQL > select length(name) from t1;

SQL > select length(‘sainora’) from dual;

REPLACE  Replaces occurances of a specified string

Syntax :-

SQL > select replace (‘string’,’from_string’,’to_string’) from dual;

Example :-

SQL > select replace(‘king Kong’ , ’ki’ , ’ho’) from dual;

TRANSLATE  Replaces occurances of a specified string

Syntax :-

SQL > select replace (‘string’,’from_string’,’to_string’) from dual;

Example :-

SQL > select replace(‘king kong’ , ’ki’ , ’ho’) from dual;

TRIM Removes both, leading and ending spaces

Syntax :-

SQL > select trim(both ‘rmstr’ from ‘string’) from dual;

SQL > select trim(leading ‘rmstr’ from ‘string’) from dual;

SQL > select trim(ending ‘rmstr’ from ‘string’) from dual;


Example :-

SQL > select trim(‘a’ from ‘asia’) from dual;

SQL > select trim(‘k’ from ‘king kong’) from dual;

SQL >select trim(‘a’ from ‘sainora’) from dual;

LTRIM  Removes leading spaces

Syntax :-

SQL > select ltrim(leading ‘string’ , ‘rmstr’) from dual;

Example :-

SQL > select ltrim( ‘sainora’ , ‘s’) from dual;

RTRIM  Removes ending spaces

Syntax :-

SQL > select rtim(trailing ‘string’ , ‘rmstr’) from dual;

Example :-

SQL > select rtrim(‘sainora’ , ‘a’) from dual;

LPAD Returns the string argument,left-padded with the specified string

Syntax :-

SQL > select lpad(‘string’, n ,’padstring’)from dual;

Example :-

SQL > select lpad(‘good’,10,’*’)from dual;

RPAD  Appends string the specified number of times.

Syntax :-

SQL > select rpad(‘string’, n , ‘padstring’) from dual;

Example :-

SQL > select rpad(‘good’ , 10 ,’*’) frm dual;


INSTR  Returns the index of the first occurrence of substring

Syntax :-

SQL > select instr(‘string’ , ‘substring’ , position , occurrence) from dual;

Example :-

SQL > select instr(‘los angeles’ ,’e’ ,1,2) from dual;

SUBSTR Returns the substring as specified

Syntax :-

SQL > select substr(‘string’ , position , string_length) from dual;

Example :-

SQL > select substr(‘sainora’ , 1 , 3) from dual;

NUMERIC FUNCTIONS :-

 Round
 Trunc
 Power
 Square root
 Mod
 Floor
 Ceil
 Abs

ROUND Returns numeric expression rounded to an integer. Can be used to round an


expression to a number of decimal points.

Syntax :- This function returns X rounded to the nearest integer.

SQL > select round( X values) from dual;

Example :-

SQL > select round(10.678) from dual;


TRUNC  Returns numeric exp 1 truncated to exp2 decimal places. If exp2 is 0,then the result
will have no decimal point.

Syntax :- This function is used to return the value of X truncated to D number of decimal
places.

SQL > select trunc(X values , D decimal number) from dual;

Example :-

SQL >select trunc(10.23456 , 2) from dual;

POWER  Returns the value of one expression raised to the power of another expression

Syntax :- These two functions return the value of X raised to the power of Y.

SQL > select power(X,Y) from dual;

Example :-

SQL > select power(5,5) from dual;

SQUAREROOT  Returns the non-negative square root of numeric expressions.

Syntax :- This function returns the non-negative square root of X.

SQL > select sqrt(X) from dual;

Example :-

SQL > select sqrt(25) from dual;

MOD Returns the remainder of one expression by diving by another expression.

Syntax :- This function returns the remainder of N divided by M.

SQL > select mod(N,M) from dual;

Example :-

SQL > select mod(29,3) from dual;

FLOOR  Returns the largest integer value that is not greater than passed numeric expression.

Syntax :- This function returns the largest integer value that is not greater than X.

SQL > select floor(X) from dual;


Example :-

SQL > select floor(9.66) from dual;

CEIL  Returns the smallest integer value that is not less than passed numeric expression.

Syntax :- These functions return the smallest integer value that is not smaller than X

SQL > select ceil(X) from dual;

Example :-

SQL > select ceil(6.43) from dual;

ABS  Returns the absolute value of numeric expression.

Syntax :- The ABS() function returns the absolute value of X.

SQL > select abs(X) from dual;

Example :-

SQL > select abs(2) from dual;

SQL > select abs(-2) from dual;

DATE FUNCTIONS :-

 Sysdate
 Systimestamp
 Months_between
 Next_day
 Last_day
 Add_months

SYSDATE The sysdate function is used to retrieve the current database system time in Oracle

Syntax :-

SQL > select sysdate from tablename;

SQL > select sysdate from dual;

Example :-

SQL > select sysdate from v$database;

SQL > select sysdate from dual;


SYSTIMESTAMP  The systimestamp function is used to retrieve the current database system
time in Oracle

Syntax :-

SQL > select systimestamp from tablename;

SQL > select systimestamp from dual;

Example :-

SQL > select systimestamp from v$database;

SQL > select systimestamp from dual;

MONTHS_BETWEEN Returns number of months between dates date1 and date2.if date1 is
later than date2,then result is positive.if date1 is earlier than date2,then the result is negative.

Syntax :-

SQL > select months_between(date 1,date 2) from dual;

SQL > select months_between(date 1,date 2) from tablename;

Example :-

SQL > select months_between (sysdate,'22-jan-16') from dual;

SQL > select months_between ('22-oct-15','22-sep-15’') from v$database;

NEXT_DAY  Returns the date of the first weekday named by char that is later than the date.

Syntax :-

SQL > select next_day(date,’char’) from tablename;

SQL > select next_day(date,’char’) from dual;

Example :-

SQL > select next_day(’22-oct-15’,’mon’) from v$database;

SQL > select next_day(sysdate,’tues’) from dual;


LAST_DAY Returns the date of the last day of the month that contains date.

Syntax :-

SQL > select last_day(date) from tablename;

SQL > select last_day(date) from dual;

Example :-

SQL > select last_day(sysdate) from v$database;

SQL > select last_day(sysdate) from dual;

ADD_MONTHS Returns the date plus integer months.

Syntax :-

SQL >select add_months(date,integer) from tablename;

SQL > select add_months(date,integer) from dual;

Example :-

SQL > select add_months(sysdate,2) from v$database;

SQL > select add_months(’22-oct-15’,2) from dual;

CONVERSION FUNCTION :

 TO_CHAR
 TO_DATE

TO_CHAR  Converts numeric and date values to a character string value . it cannot be used
for calculations since it is a string value

Syntax :-

SQL > select to_char(date,’HH:MI:SS) from dual;

Example :-

SQL >select to_char(sysdate,’HH:MI:SS) form dual;


TO_DATE Converts a valid numeric and character values to date value . date is formatted to
the format specified by date_format.

Syntax :-

SQL > select to_date(date ,’DD-MON-YY’) from dual;

Example :-

SQL > select to_date(’10-21-15’,’DD-MON-YY’) from dual;

NULL FUNCTIONS :

 Nvl
 Nvl2
 Nullif
 Coalesce

NVL  the nvl function substitutes an alternate value for a null value.

Syntax :-

SQL > select nvl (column_name ,’value is null’) from table_name;

Example :-

SQL > select nvl(name, ’name is not null’) from t1;

NVL2  Oracle introduced a function to substitute value not only for NULL columns values but
also for NOT NULL columns. NVL2 function can be used to substitute an alternate value for
NULL as well as non NULL value.

Syntax :-

SQL > select nvl2(column_name ,’value is not null’ ,’value is null’) from table_name;

Example :-

SQL > select nvl2(name ,’name is not null’ ,’name is null’) from t1;

NULLIF  The NULLIF function compares two arguments expr1 and expr2. If expr1 and
expr2 are equal, it returns NULL; else, it returns expr1. Unlike the other null handling function,
first argument can't be NULL.
Syntax :-

SQL >select nullif(column1,column2) from tablename;

Example :-

SQL > select nullif(id , mgrid) from t1;

COALESCE  COALESCE function, a more generic form of NVL, returns the first non-null
expression in the argument list. It takes minimum two mandatory parameters but maximum
arguments has no limit.

Syntax :-

SQL > select coalesce(column1,column2,column3) from table_name;

Example :-

SQL > select coalesce(id ,mgrid , deptno) from t1;

CONDITIONAL FUNCTIONS :

 Decode
 Case

Difference between case and decode

Decode is a function Case is a statement

Decode is used to 9i Case is used to 10g and 11g

Decode is used in sql Case is used in sql and plsql

We cannot using where clause We can using where clause

DECODE

Syntax :-

SQL > select decode(columnname, search, result……,default) from tablename;

Example :-

SQL > select decode(id,1,’one’,2,’two’,3,’three’,’invalid’) from t1;


CASE

Syntax :-

SQL > select column_name , case

When condition 1 then result 1

When condition 2 then result 2

When condition 3 then result 3

End case

From tablename;

Example :-

SQL > select salary , case

When salary < 5000 then ‘low’

When salary >10000 and salary <15000 then ‘medium’

When salary > 15000 then ‘high’

End case

From t1;

MULTIROW FUNCTIONS :-

 Aggregate function
 Order by function
 Group by function
 Having function
 Where clauses

AGGERGATE FUNCTIONS :-

 Sum
 Max
 Min
 Avg
 Count
 Distinct
SUM  Returns the sum

Syntax :-

SQL > select sum(columnname) from tablename;

Example :-

SQL > select sum(salary) from t1;

MAX  Returns the largest value

Syntax :-

SQL > select max(columnname) from tablename;

Example :-

SQL > select max(salary) from t1;

MIN  Returns the smallest value

Syntax :-

SQL > select min(columnname) fom tablename;

Example :-

SQL > select min(salary) from t1;

AVG  Returns the average value

Syntax :-

SQL > select avg(columnname) from tablename;

Example :-

SQL > select avg(salary) from t1;

COUNT  Returns the number of rows

Syntax :-

SQL > select count(columnname) from tablename;

Example :-

SQL > select count(salary) from t1;


DISTINCT  Returns the without only unique rows

Syntax :-

SQL > select distinct(columnname) from tablename;

Example :-

SQL > select distinct(salary) from t1;

ORDER BY FUNCTIONS :

Order by clause is used to sort the data in ascending or descending order.

Syntax :-

SQL > select * from tablename order by columnname asc;

Example :-

SQL > select * from t1 order by salary asc;

Syntax :-

SQL > select * from tablename order by columnname desc;

Example :-

SQL > select * from t1 order by salary desc;

GROUP BY FUNCTION :

Group by clause is used along with the group functions to retrieve data grouped according to one
or more columns.

Syntax :-

SQL > select sum(columnname) from tablename group by columnname;

Example :-

SQL > select sum(salary) from t1 group by dept;

HAVING CLAUSES
Having clause is used to filter data based on the group functions.this is similar to where
condition but is used with group functions.group functions cannot be used in where clause but
can be used in having clause.

Syntax :-

SQL > select sum(columnname ) from tablename group by columnname having


columnname=’values’;

Example :-

SQL > select sum(salary) from t1 group by dept having dept=’DBA’;

WHERE CLAUSE :

Where clause is used when you want to retrieve specific information from a table excluding
other irrelevant data.

Syntax :-

SQL > select * from tablename where columnname=’values’;

Example :-

SQL > select * from t1 where name=’sainora’;

You might also like