Professional Documents
Culture Documents
Scalar Functions: Pamantasan NG Cabuyao
Scalar Functions: Pamantasan NG Cabuyao
Scalar Functions: Pamantasan NG Cabuyao
MODULE
7 SCALAR FUNCTIONS
OVERVIEW
OBJECTIVES
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.
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
Syntax of UCASE,
Example 1:
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
Example 2
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.
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:
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.
Example 4:
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
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