Scalar Functions: Pamantasan NG Cabuyao

You might also like

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

Pamantasan ng Cabuyao

College of Computing and Engineering


1ST SEMESTER 2020 – 2021

MODULE

7 SCALAR FUNCTIONS

OVERVIEW

The purpose of this module is to introduce SQL scalar command set.

OBJECTIVES

By the end of this module you should be able to:


1. Enhance coding in Sql thru simplification
2. Solve problems that requires less code thru Scalar commands
3. Deliver a clear and easy to troubleshoot codes.

1.1. SCALAR FUNCTIONS

A function, in any programming environment, lets you encapsulate reusable logic and build software that is
“composable”, i.e. built of pieces that can be reused and put together in a number of different ways to meet
the needs of the users. Functions hide the steps and the complexity from other code.

Functions can be Deterministic or Nondeterministic

A deterministic function will return the same result when it is called with the same set of input parameters.
Adding two numbers together is an example of a deterministic function.

A nondeterministic function, on the other hand, may return different results every time they are called with
the same set of input values. Even if the state of the data in the database is the same, the results of the
function might be different.

The GETDATE function, for example, is nondeterministic. One caveat of almost all nondeterministic functions
is that they are executed once per statement, not once per row. If you query 90,000 rows of data and use the
RAND function to attempt to produce a random value for each row you will be disappointed; SQL Server will
only generate a single random number for the entire statement.

The only exception to this rule is NEWID, which will generate a new GUID for every row in the statement.

When we create a function, SQL Server will analyze the code we’ve created and evaluate whether the
function is deterministic. If our function makes calls to any nondeterministic functions, it will, itself, be
Copyright Disclaimer

Fair use of a Copyrighted Work as defined in section 185 of RA 8293, which states, “The fair use of copying, criticism, comment, news reporting, teaching including multiple copies
for classroom use, research and similar purposes is not infringement of copyright.” These lecture handouts are prepared and compiled solely for students of Pamantasan ng
Cabuyao (PnC) under the College of Computing and Engineering enrolled in Database Management System for AY 2020-2021.
Page 1
Pamantasan ng Cabuyao
College of Computing and Engineering
1ST SEMESTER 2020 – 2021

marked as nondeterministic. SQL Server relies on the author of a SQL CLR function to declare the function as
deterministic using an attribute.

Deterministic functions can be used in indexed views and computed columns whereas nondeterministic
functions cannot.

Scalar Functions

Scalar functions return a single value from an input value. Following are some frequently used Scalar
Functions in SQL.

UCASE() Function

UCASE function is used to convert value of string column to Uppercase characters.

Syntax of UCASE,

SELECT UCASE(column_name) from table-name;

Example 1:

Using UCASE() function

Consider the following Emp table

eid name age salary


401 anu 22 9000
402 shane 29 8000
403 rohan 34 6000
404 scott 44 10000
405 Tiger 35 8000

SQL query for using UCASE is,

SELECT UCASE(name) FROM emp;

Result is,
UCASE(name)
ANU
SHANE
ROHAN
SCOTT
TIGER

Copyright Disclaimer

Fair use of a Copyrighted Work as defined in section 185 of RA 8293, which states, “The fair use of copying, criticism, comment, news reporting, teaching including multiple copies
for classroom use, research and similar purposes is not infringement of copyright.” These lecture handouts are prepared and compiled solely for students of Pamantasan ng
Cabuyao (PnC) under the College of Computing and Engineering enrolled in Database Management System for AY 2020-2021.
Page 2
Pamantasan ng Cabuyao
College of Computing and Engineering
1ST SEMESTER 2020 – 2021

LCASE() Function

LCASE function is used to convert value of string columns to Lowecase characters.

Syntax for LCASE is,

SELECT LCASE(column_name) FROM table-name;

Example 2

Using LCASE() function

Consider the following Emp table

eid name age salary


401 Anu 22 9000
402 Shane 29 8000
403 Rohan 34 6000
404 SCOTT 44 10000
405 Tiger 35 8000

SQL query for converting string value to Lower case is,

SELECT LCASE(name) FROM emp;

Result will be,

LCASE(name)
anu
shane
rohan
scott
tiger

MID() Function

MID function is used to extract substrings from column values of string type in a table.

Syntax for MID function is,

SELECT MID(column_name, start, length) from table-name;

Copyright Disclaimer

Fair use of a Copyrighted Work as defined in section 185 of RA 8293, which states, “The fair use of copying, criticism, comment, news reporting, teaching including multiple copies
for classroom use, research and similar purposes is not infringement of copyright.” These lecture handouts are prepared and compiled solely for students of Pamantasan ng
Cabuyao (PnC) under the College of Computing and Engineering enrolled in Database Management System for AY 2020-2021.
Page 3
Pamantasan ng Cabuyao
College of Computing and Engineering
1ST SEMESTER 2020 – 2021

Example 3:

Using MID() function

Consider the following Emp table

eid name age salary


401 anu 22 9000
402 shane 29 8000
403 rohan 34 6000
404 scott 44 10000
405 Tiger 35 8000

SQL query will be,

SELECT MID(name,2,2) FROM emp;

Result will come out to be,

MID(name,2,2)
nu
ha
oh
co
ig

ROUND() Function

ROUND function is used to round a numeric field to number of nearest integer. It is used on Decimal point
values.

Syntax of Round function is,

SELECT ROUND(column_name, decimals) from table-name;

Example 4:

Using ROUND() function

Consider the following Emp table

eid name age salary


401 anu 22 9000.67
402 shane 29 8000.98
403 rohan 34 6000.45
404 scott 44 10000
Copyright Disclaimer

Fair use of a Copyrighted Work as defined in section 185 of RA 8293, which states, “The fair use of copying, criticism, comment, news reporting, teaching including multiple copies
for classroom use, research and similar purposes is not infringement of copyright.” These lecture handouts are prepared and compiled solely for students of Pamantasan ng
Cabuyao (PnC) under the College of Computing and Engineering enrolled in Database Management System for AY 2020-2021.
Page 4
Pamantasan ng Cabuyao
College of Computing and Engineering
1ST SEMESTER 2020 – 2021

SQL query is,

SELECT ROUND(salary) from emp;

Result will be,

ROUND(salary)
9001
8001
6000
10000

Copyright Disclaimer

Fair use of a Copyrighted Work as defined in section 185 of RA 8293, which states, “The fair use of copying, criticism, comment, news reporting, teaching including multiple copies
for classroom use, research and similar purposes is not infringement of copyright.” These lecture handouts are prepared and compiled solely for students of Pamantasan ng
Cabuyao (PnC) under the College of Computing and Engineering enrolled in Database Management System for AY 2020-2021.
Page 5

You might also like