Vous êtes sur la page 1sur 26

SQL Functions

String Functions
A string is a bunch of things such as
houses, places numbers, or characters
in a sentences
It includes any mixture of letters,
numbers,
spaces
and symbols(special
Function
Use
Name
characters/punctuations
etc)
| |

Glues or concatenates 2 string strings


together. Symbol is called vertical Bar OR
pipe..

ASCII

Returns the decimal representation in the


database character set of the 1st character
string

CHR

Returns the character having binary


equivalent to the string.

INITCAP

Initial capital. Capitalization the 1st letter of

String Functions
Function Name Use
INSTR

Finds the location of character in string .

LENGTH

Tells the length of string

LOWER

Converts every character in lower case

LTRIM

Left trim. Trims all the occurrences of any one of


a set of characters off the left side of string

RTEIM

Right trim. Trims all the occurrences of any one


of a set of characters off the Right side of string

SOUNDEX

Finds words that sound like the example


specified.

SUBSTR

Substring. Clips out piece of string.

TRIM

Trims all occurrences of any one of a set of


characters off either or both.

UPPER

Converts every letter in a string into uppercase..

Reverse

Gives o/p in reserve order, character by


character

String Functions
Table LOCATION is created :Column
Name

Data Type

Nullabl Defaul
e
t

CITY

VARCHAR

Yes

COUNTRY

VARCHAR

Yes

Yes

Yes

TEMPERAT
NUMBER
URE
POPULATIO
NUMBER
N

Primary
Key

String Functions
Table location with data..
CITY
athens

COUNTRY
greece

TEMPERATU POPULATIO
RE
N
56
100000

chicago

united states

50

200000

lima

peru

75

300000

madras

india

60

400000

london

england

30

500000

moscow

russia

20

600000

paris

france

35

700000

bejing

china

55

800000

rome

italy

65

900000

tokyo

japan

45

900000

colombo

sri lanka

80

1100000

String Functions
1) concatenation: Select city ||country from location;
CITY||COUNTRY
athens greece
chicago united states
lima peru
madras india
london england
moscow russia
paris france
bejing china
rome italy
tokyo japan
colombo sri lanka

Select city || , ||
country from
location

String Functions
TRIM,RTRIM,LTRIM: Select RTRIM(city, ) from location;
They trims unwanted characters from the
left, right or from both ends
Select RTRIM(city, ) from location;
Set of characters being
trimmed

String Functions
LOWER: Select lower(YTC) from dual;
Select lower(city) from location;

UPPER: Select Upper(ytc) from dual;


Select upper(city) from location;

INITCAP: INITCAP(yspm) from dual;

REVERSE : REVERSE(yspm) from dual;

String Functions
Length: Tells no. of characters in a string
Select population, length(population) from
location;

SUBSTR: Substring gives piece of string


Substr(string, start, count)
Tells SQL to clip out a subsection of string,
beginning at position start, & continuing
for count characters.
select substr(city,1,2) from location;

String Functions
INSTR: It doesnt clip anything off.
Allows searching through a string for a set
of characters
Syntax:- INSTR(string,start, occurance)..
Select instr(YSPMYTC,set,1,2) from
dual;
Set part of function is case sensitive..

String Functions
ASCII and CHR: select chr(70) || chr(83)||chr(85) ||
chr(71)as chr_Values from dual;
CHR_VALUE
S
FSUG

select ASCII('FSUG') from dual;


ASCII('FSU
G')
70

String Functions
SOUNDEX: It has ability to find words that sounds like
other words, virtually regardless of how
either is spelled.
This is specially useful, when we are not
certain about a word or name is really
spelled..
Syntax:- SOUNDEX(string)
SOUNDEX compares he sound of the entry
in the selected column with the sound of
the word in a single quotation marks, & it
looks for a close match..

SQL GROUP
Functions

SQL GROUP Functions


Group functions are built-in SQL
functions that operate on groups of
rows and return one value for the entire
group.
These functions are:
COUNT,
MAX,
MIN,
AVG,
SUM,
DISTINCT

SQL GROUP Functions


SQL COUNT ():
This function returns the number of rows in
the table that satisfies the condition
specified in the WHERE condition.
If the WHERE condition is not specified,
then the query returns the total number of
rows in the table.
For Example: If you want the number of
employees in a particular department, the
query would be:
SELECT COUNT (*) FROM employee WHERE dept
= 'Electronics';

SQL GROUP Functions


Eg-2-) If you want the total number of employees in
all the department, the query would take the form:
SELECT COUNT (*) FROM employee;

SQL DISTINCT():
This function is used to select the distinct rows.
For Example: If you want to select all distinct
department names from employee table, the query
would be:
SELECT DISTINCT dept FROM employee;

To get the count of employees with unique


name, the query would be:
SELECT COUNT (DISTINCT name) FROM employee;

SQL GROUP Functions


SQL MAX():
This function is used to get the maximum
value from a column.
To get the maximum salary drawn by an
employee, the query would be:
SELECT MAX (salary) FROM employee;

SQL MIN():
This function is used to get the minimum
value from a column.
To get the minimum salary drawn by an
employee, the query would be:
SELECT MIN (salary) FROM employee;

SQL GROUP Functions


SQL AVG():
This function is used to get the average
value of a numeric column.
To get the average salary, the query would be
SELECT AVG (salary) FROM employee;

SQL SUM():
This function is used to get the sum of a
numeric column
To get the total salary given out to the
employees,
SELECT SUM (salary) FROM employee;

SQL GROUP Functions

Date Function
1) SYSDATE: Select sysdate from dual;

2) CURRENT_DATE: Select CURRENT_DATE from dual;

3) SYSTIMESTAMP: Select SYSTIMESTAMP from dual;

DIFFERENCE BETWEEN 2 DATES


select holiday, actualdate,celebratedate
from holiday where celebratedateactualdate !=0;

Date Function
Which holiday are not celebrated on actual date of
their anniversary, this can be easily answered by
subtracting celebration date from actualdate.
If the answer is not zero , there is difference
between the two dates.

Date Function

Date Function

Date Function

Date Function

Date Function

Vous aimerez peut-être aussi