Experiment Num 1 Record

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 31

1.

Queries to facilitate acquaintance of Built-In Functions, String


Functions, Numeric Functions, Date Functions and Conversion
Functions.
Objective: To understand and implement various types of function in SQL.

String Functions

SQL string functions are used primarily for string manipulation.

1)UPPER()  Function
The SQL UPPER function converts all the letters in a string into uppercase. 
Syntax
UPPER(string)

Parameter Values

Parameter Description

String Required. The string to convert to upper-case

SELECT UPPER('hello sql') from dual;

o/p:

UPPER('HE

---------

HELLO SQL

2) LOWER() Function
The LOWER() function converts a string to lower-case.

Syntax
LOWER(string)

Parameter Values

Parameter Description

String Required. The string to convert to lower-case

SELECT LOWER('DATABASE') FROM dual;

o/p:

LOWER('D

--------

database

3) INITCAP Function
The INITCAP function sets the first character in each word to uppercase and the
rest to lowercase.

Syntax
The syntax for the INITCAP function in Oracle/PLSQL is:

INITCAP( string1 )

Parameters or Arguments
string1
The string argument whose first character in each word will be converted to
uppercase and all remaining characters converted to lowercase.

Returns
The INITCAP function returns a string value.
Select INITCAP(‘database management’) from dual;

o/p:

INITCAP('DATABASEMA

-------------------

Database Management

4) LPAD Function
The LPAD function pads the left-side of a string with a specific set of characters
(when string1 is not null).

Syntax
The syntax for the LPAD function in Oracle/PLSQL is:

LPAD( string1, padded_length [, pad_string] )

Parameters or Arguments
string1

The string to pad characters to (the left-hand side).

padded_length

The number of characters to return. If the padded_length is smaller than the


original string, the LPAD function will truncate the string to the size
of padded_length.

pad_string
Optional. This is the string that will be padded to the left-hand side
of string1. If this parameter is omitted, the LPAD function will pad spaces to
the left-side of string1.

Returns
The LPAD function returns a string value.

SELECT LPAD('tech', 8, '*') FROM dual;

o/p:

LPAD('TE

--------

****tech

5) RPAD Function
The Oracle/PLSQL RPAD function pads the right-side of a string with a specific
set of characters (when string1 is not null).

Syntax
The syntax for the RPAD function in Oracle/PLSQL is:

RPAD( string1, padded_length [, pad_string] )

Parameters or Arguments
string1

The string to pad characters to (the right-hand side).

padded_length
The number of characters to return. If the padded_length is smaller than the
original string, the RPAD function will truncate the string to the size
of padded_length.

pad_string

Optional. This is the string that will be padded to the right-hand side
of string1. If this parameter is omitted, the RPAD function will pad spaces
to the right-side of string1.

Returns
The RPAD function returns a string value.

SELECT RPAD('tech', 8, '*') FROM dual;

o/p:

RPAD('TE

--------

tech****

SELECT RPAD('tech', 8) FROM dual;

o/p:

RPAD('TE

--------

tech

6) LTRIM Function
The LTRIM function removes all specified characters from the left-hand side of a
string.
Syntax
The syntax for the LTRIM function in Oracle/PLSQL is:

LTRIM( string1 [, trim_string] )

Parameters or Arguments
string1

The string to trim the characters from the left-hand side.

trim_string

Optional. The string that will be removed from the left-hand side of string1.
If this parameter is omitted, the LTRIM function will remove all leading
spaces from string1.

Returns
The LTRIM function returns a string value.

SELECT LTRIM('   tech') FROM dual;

o/p: LTRI

----

tech

SELECT LTRIM('000123', '0')FROM dual;

o/p:

LTR

---

123
7) RTRIM Function
The Oracle/PLSQL RTRIM function removes all specified characters from the
right-hand side of a string.

Syntax
The syntax for the RTRIM function in Oracle/PLSQL is:

RTRIM( string1 [, trim_string ] )

Parameters or Arguments
string1

The string to trim the characters from the right-hand side.

trim_string

Optional. The string that will be removed from the right-hand side
of string1. If this parameter is omitted, the RTRIM function will remove all
trailing spaces from string1.

Returns
The RTRIM function returns a string value.

SELECT RTRIM('tech   ') FROM dual;

o/p:

RTRI

----

tech

SELECT RTRIM('123000', '0') FROM dual;

o/p:
RTR

---

123

8) REPLACE Function
The REPLACE function replaces a sequence of characters in a string with another
set of characters, not case-sensitive.

Syntax
The syntax for the REPLACE function in SQL is:

REPLACE( string1, string_to_replace, replacement_string )

Parameters or Arguments
string1

The source string from which a sequence of characters will be replaced by


another set of characters.

string_to_replace

The string that will be searched for in string1.

replacement_string

The replacement string. All occurrences of string_to_replace will be


replaced with replacement_string in string1.

SELECT REPLACE('database', 't', '3') from dual;

o/p: REPLACE(

--------
da3abase

SELECT REPLACE('Data base Management', ' ', 'Z') from dual;

o/p:

REPLACE('Database

DataZbaseZManagement

9) TRANSLATE Function
The Oracle/PLSQL TRANSLATE function replaces a sequence of characters in a
string with another set of characters. However, it replaces a single character at a
time.
For example, it will replace the 1st character in the string_to_replace with the 1st
character in the replacement_string. Then it will replace the 2nd character in
the string_to_replace with the 2nd character in the replacement_string, and so on.

Syntax
The syntax for the TRANSLATE function in Oracle/PLSQL is:

TRANSLATE( string1, string_to_replace, replacement_string )

Parameters or Arguments
string1

The string to replace a sequence of characters with another set of characters.

string_to_replace

The string that will be searched for in string1.

replacement_string

All characters in the string_to_replace will be replaced with the


corresponding character in the replacement_string.
Returns
The TRANSLATE function returns a string value.

SELECT TRANSLATE('1tech23', '123', '456') FROM DUAL;

O/P:

TRANSLA

-------

4tech56

SELECT TRANSLATE('222tech', '2ec', '3it') FROM dual;

o/p: TRANSLA

-------

333tith

10) SUBSTR Function


The Oracle/PLSQL SUBSTR functions allows you to extract a substring from a
string.

Syntax
The syntax for the SUBSTR function in Oracle/PLSQL is:

SUBSTR( string, start_position [, length ] )

Parameters or Arguments
string

The source string.

start_position
The starting position for extraction. The first position in the string is always
1.

length

Optional. It is the number of characters to extract. If this parameter is


omitted, the SUBSTR function will return the entire string. If length is a
negative number, then the SUBSTR function will return a NULL value.

Returns
The SUBSTR function returns a string value.

SELECT SUBSTR('This is a test', 6, 2) FROM dual;

o/p:

SU

--

is

SELECT SUBSTR('This is a test', 6) FROM dual;

o/p:

SUBSTR('T

---------

is a test

SELECT SUBSTR('TechOnTheNet', -6, 3) FROM dual;

o/p: SUB

---

The
11) INSTR Function
The Oracle/PLSQL INSTR function returns the location of a substring in a string.

Syntax
The syntax for the INSTR function in Oracle/PLSQL is:

INSTR( string, substring [, start_position [, th_appearance ] ] )

Parameters or Arguments
string

The string to search. string can be CHAR, VARCHAR2, NCHAR,


NVARCHAR2, CLOB, or NCLOB.

substring

The substring to search for in string. substring can be CHAR, VARCHAR2,


NCHAR, NVARCHAR2, CLOB, or NCLOB.

start_position

Optional. The position in string where the search will start. If omitted, it


defaults to 1. The first position in the string is 1. If the start_position is
negative, the INSTR function counts back start_position number of
characters from the end of stringand then searches towards the beginning
of string.

nth_appearance

Optional. The nth appearance of substring. If omitted, it defaults to 1.

Returns
The INSTR function returns a numeric value. The first position in the string is 1.
If substring is not found in string, then the INSTR function will return 0.
SELECT INSTR('Tech on the net', 'e') FROM dual;

o/p:

INSTR('TECHONTHENET','E')

-------------------------

SELECT INSTR('Tech on the net', 'e', 1, 1) FROM dual;

o/p:

INSTR('TECHONTHENET','E',1,1)

-----------------------------

SELECT INSTR('Tech on the net', 'e', 1, 2) FROM dual;

o/p:

INSTR('TECHONTHENET','E',1,2)

-----------------------------

11

SELECT INSTR('Tech on the net', 'e', -3, 2) FROM dual;

o/p:

INSTR('TECHONTHENET','E',-3,2)

------------------------------

2
12) CONCAT Function
The CONCAT function allows you to concatenate strings together.

Syntax
The syntax for the CONCAT function in SQL is:

CONCAT( string1, string2, ... string_n )

Parameters or Arguments
string1, string2, ... string_n

The strings to concatenate together.

SELECT CONCAT('data', 'base') from dual;

o/p:

CONCAT('

--------
database

SELECT CONCAT('data',CONCAT( 'base’,’management’)) from dual;

o/p:

CONCAT('

--------
databasemanagement
13) LENGTH() Function

The LENGTH() function returns the length of the specified string (in bytes).
Syntax
LENGTH(string)

Parameter Values

Parameter Description

String Required. The string to return the length for

SELECT LENGTH(‘data’) from dual;

o/p:

LENGTH('DATA')

--------------

Numeric Functions
1) ABS Function
the ABS function returns the absolute value of a number.

Syntax
The syntax for the ABS function in SQL Server (Transact-SQL) is:

ABS( number )

Parameters or Arguments
number
The number to convert to an absolute value.

SELECT ABS(-24) FROM dual;

o/p:

ABS(-24)

---------

24

SELECT ABS(24.65 * -1) FROM dual;

o/p:

ABS(24.65*-1)

-------------

24.65

2) MOD Function
MOD function returns the remainder of m divided by n.

Syntax
The syntax for the MOD function in Oracle/PLSQL is:

MOD( m, n )

Parameters or Arguments
m Numeric value used in the calculation.

n Numeric value used in the calculation.


SELECT MOD(15, 4) FROM dual;

o/p: MOD(15,4)

---------

SELECT MOD(11.6, 2.1) FROM dual;

o/p:

MOD(11.6,2.1)

-------------

1.1

3) POWER Function
The POWER function returns m raised to the nth power.

Syntax
The syntax for the POW function in SQL is:

POWER( m, n )

Parameters or Arguments
m

Numeric value. It is the base used in the calculation.

Numeric value. It is the exponent used in the calculation.

SELECT POWER(5, 2) FROM dual;


o/p:

POWER(5,2)

----------

25

SELECT POWER(0, 4) FROM dual;


o/p:
POWER(0,4)
----------
0

4) ROUND Function
The ROUND function returns a number rounded to a certain number of decimal
places.

Syntax
The syntax for the ROUND function in SQL is:

ROUND( number, [decimal_places] )

Parameters or Arguments
number

The number to round.

decimal_places

The number of decimal places rounded to. This value must be a positive or
negative integer. If this parameter is omitted, the ROUND function will
round the number to 0 decimal places.
SELECT ROUND(125.315) FROM dual;

o/p:

ROUND(125.315)

----------------

125

SELECT ROUND(125.315, 2) FROM dual;

o/p:

ROUND(125.315,2)

----------------

125.32

SELECT ROUND(125.315, -1) FROM dual;

o/p: ROUND(125.315,-1)

-----------------

130

SELECT ROUND(125.315, -2) FROM dual;

o/p: ROUND(125.315,-2)

-----------------

100

5) TRUNC Function
TRUNC function returns a number truncated to a certain number of decimal
places.

Syntax (with numbers)


The syntax for the TRUNC function in Oracle/PLSQL is:

TRUNC( number [, decimal_places] )

Parameters or Arguments
number

The number to truncate.

decimal_places

Optional. The number of decimal places to truncate to. This value must be
an integer. If this parameter is omitted, the TRUNC function will truncate
the number to 0 decimal places.

Returns
The TRUNC function (as it applies to numbers) returns a numeric value.

SELECT TRUNC(125.815) FROM dual;

o/p: TRUNC(125.815)

--------------

125

SELECT TRUNC(125.815, 1) FROM dual;

o/p:

TRUNC(125.815,1)

----------------
125.8

SELECT TRUNC(125.815, -1) FROM dual;

o/p:

TRUNC(125.815,-1)

-----------------

120

SELECT TRUNC(125.815, -2) FROM dual;

o/p:

TRUNC(125.815,-2)

-----------------

100

6) SQRT Function
SQRT function returns the square root of n.

Syntax
The syntax for the SQRT function in Oracle/PLSQL is:

SQRT( n )

Parameters or Arguments
n

A positive number that is used in the square root calculation.


Returns
The SQRT function returns a numeric value.

SELECT SQRT(9) FROM dual;

o/p:

SQRT(9)

---------

SELECT SQRT(5.617) FROM dual;

o/p:

SQRT(5.617)

-----------

2.3700211

Date Functions
1) MONTHS_BETWEEN Function
MONTHS_BETWEEN function returns the number of months
between date1 and date2.

Syntax
The syntax for the MONTHS_BETWEEN function in Oracle/PLSQL is:

MONTHS_BETWEEN( date1, date2 )

Parameters or Arguments
date1
The first date used to calculate the number of months between.

date2

The second date used to calculate the number of months between.

Returns
The MONTHS_BETWEEN function returns a numeric value.

SELECT MONTHS_BETWEEN ('11-jan-09','11-jan-08') FROM dual;

o/p: MONTHS_BETWEEN('11-JAN-09','11-JAN-08')

---------------------------------------

12

SELECT MONTHS_BETWEEN ('11-jan-08', '11-jan-09') FROM DUAL;

o/p:

MONTHS_BETWEEN('11-JAN-08','11-JAN-09')

---------------------------------------

-12

2)LAST_DAY Function
LAST_DAY function returns the last day of the month based on a date value.

Syntax
The syntax for the LAST_DAY function in Oracle/PLSQL is:

LAST_DAY( date )
Parameters or Arguments
date

The date value to use to calculate the last day of the month.

Returns
The LAST_DAY function returns a date value.

SELECT LAST_DAY(sysdate) FROM DUAL;

O/P:

LAST_DAY(

---------

31-JUL-18

SELECT LAST_DAY('11-jan-09') FROM DUAL;

O/P:

LAST_DAY(

---------

31-JAN-09

3)  ADD_MONTHS Function
 ADD_MONTHS function returns a date with a specified number of months added.

Syntax
The syntax for the ADD_MONTHS function in Oracle/PLSQL is:

ADD_MONTHS( date1, number_months )


Parameters or Arguments
date1

The starting date (before the n months have been added).

number_months

The number of months to add to date1.

Returns
The ADD_MONTHS function returns a date value.

SELECT ADD_MONTHS('01-Aug-03', 3) FROM DUAL;

O/P:

ADD_MONTH

---------

01-NOV-03

SELECT ADD_MONTHS('21-Aug-03', -3) FROM DUAL;

O/P:

ADD_MONTH

---------

21-MAY-03

4) NEXT_DAY Function
 NEXT_DAY function returns the first weekday that is greater than a date.

Syntax
The syntax for the NEXT_DAY function in Oracle/PLSQL is:
NEXT_DAY( date, weekday )

Parameters or Arguments
date

A date value used to find the next weekday.

weekday

The day of the week that you wish to return. It can be one of the following
values:

Returns
The NEXT_DAY function returns a date value.
SELECT NEXT_DAY('01-Aug-03', 'TUESDAY') FROM DUAL;

o/p:NEXT_DAY(

---------

05-AUG-03

SELECT NEXT_DAY('20-JUL-18', 'FRIDAY') FROM DUAL;

O/P:

NEXT_DAY(

---------

27-JUL-18

Conversion Functions
1) TO_CHAR Function
TO_CHAR function converts a number or date to a string.
Syntax
The syntax for the TO_CHAR function in Oracle/PLSQL is:

TO_CHAR( value [, format_mask] [, nls_language] )

Parameters or Arguments
value

A number or date that will be converted to a string.

format_mask

Optional. This is the format that will be used to convert value to a string.

nls_language

Optional. This is the nls language used to convert value to a string.

Returns
The TO_CHAR function returns a string value.

SELECT TO_CHAR(1210.73, '9999.9') FROM DUAL;

O/P:

TO_CHAR

-------

1210.7

SELECT TO_CHAR(1210.73, '9,999.99') FROM DUAL;

O/P:

TO_CHAR(1

---------
1,210.73

SELECT TO_CHAR(1210.73, '$9,999.00') FROM DUAL;

O/P:

TO_CHAR(12

----------

$1,210.73

SELECT TO_CHAR(sysdate, 'yyyy/mm/dd') FROM DUAL;

O/P:

TO_CHAR(SY

----------

2018/07/27

SELECT TO_CHAR(sysdate, 'MON DDth, YYYY') FROM DUAL;

O/P: TO_CHAR(SYSDAT

--------------

JUL 27TH, 2018

2) TO_NUMBER Function
TO_NUMBER function converts a string to a number.

Syntax
The syntax for the TO_NUMBER function in Oracle/PLSQL is:
TO_NUMBER( string1 [, format_mask] [, nls_language] )

Parameters or Arguments
string1

The string that will be converted to a number.

format_mask

Optional. This is the format that will be used to convert string1 to a number.

nls_language

Optional. This is the nls language used to convert string1 to a number.

Returns
The TO_NUMBER function returns a numeric value.

SELECT TO_NUMBER('1210.73', '9999.99') FROM dual;

o/p:

TO_NUMBER('1210.73','9999.99')

------------------------------

1210.73

SELECT TO_NUMBER('546', '999') FROM dual;

o/p:

TO_NUMBER('546','999')

----------------------

546
3) TO_DATE Function
TO_DATE function converts a string to a date.

Syntax
The syntax for the TO_DATE function in Oracle/PLSQL is:

TO_DATE( string1 [, format_mask] [, nls_language] )

Parameters or Arguments
string1

The string that will be converted to a date.

format_mask

Optional. This is the format that will be used to convert string1 to a date. It


can be one or a combination of the following values:
nls_language

Optional. This is the nls language used to convert string1 to a date.

Returns
The TO_DATE function returns a date value.

SELECT TO_DATE('2003/07/09', 'yyyy/mm/dd') FROM dual;

o/p:

TO_DATE('

---------

09-JUL-03
SELECT TO_DATE('070903', 'MMDDYY') FROM dual;

o/p:

TO_DATE('

---------

09-JUL-03

SELECT TO_DATE('20020315', 'yyyymmdd') FROM dual;

o/p:

TO_DATE('

---------

15-MAR-02

SELECT TO_DATE('2015/05/15 8:30:25', 'YYYY/MM/DD HH:MI:SS')


FROM dual;

o/p:

TO_DATE('

---------

15-MAY-15

You might also like