MS SQL Server Managment Functions - Section-3
MS SQL Server Managment Functions - Section-3
GAWHARSHAD University
Computer Science Department
5th Semester-1399
Subject: RDBMS II
Microsoft SQL Server Management
Lecturer: Rahmatullah Khuram
Unit -6
SQL Server Functions
datetime From January 1, 1753 to December 31, 9999 with an accuracy of 8 bytes
3.33 milliseconds
datetime2 From January 1, 0001 to December 31, 9999 with an accuracy of 6-8
100 nanoseconds bytes
smalldatetime From January 1, 1900 to June 6, 2079 with an accuracy of 1 4 bytes
minute
date Store a date only. From January 1, 0001 to December 31, 9999 3 bytes
▪ABS(X)
This function returns the absolute value of X. For
example:
Select abs(-6);
This returns 6.
▪ MOD(X,Y)
The variable X is divided by Y and their remainder is
returned. For example:
Select mod(9,5);
This returns 4.
▪ SIGN(X)
This method returns 1 if X is positive, -1 if it is negative and
0 if the value of X is 0. For example:
Select sign(10);
This returns 1.
▪ FLOOR(X)
This returns the largest integer value that is either less than
X or equal to it. For example:
Select floor(5.7);
This returns 5.
▪ CEILING(X)
This returns the smallest integer value that is either more
than X or equal to it. For example:
Select ceiling(5.7);
This returns 6.
▪ POWER(X,Y)
This function returns the value of x raised to the power of Y
For example:
Select power(2,5);
This returns 32.
▪ ROUND(X)
This function returns the value of X rounded off to the whole
integer that is nearest to it. For example:
Select round(5.7);
This returns 6.
▪ SQRT(X)
This function returns the square root of X. For example:
Select sqrt(9);
This returns 3.
▪ ASIN(X)
This function accepts a Sin value as the input and returns
the angle in radians. For example:
Select asin(0);
This returns 0.
▪ ACOS(X)
This function accepts a Cos value as the input and returns
the angle in radians. For example:
Select acos(1);
This returns 0.
❑ String Functions
The string functions are used primarily for
string manipulation. There are many types of
string functions in SQL server but the following
details the important string functions which are
in SQL Server.
▪ ASCII ()
The following example returns the ASCII code values of the
character A and Z:
Select ASCII(‘A’);
This returns 65
▪ CHAR()
This function returns the character of the numbers :
Select CHAR(65);
This returns A.
▪ LEFT()
This function extract a given number of character from the left side
of a supplied string.
Select Left (‘input string’ , number of character);
Select left (‘ SQL Server’,3)
This returns SQL.
▪ RIGHT ()
This function extract a given number of character from the right side of a
supplied string:
Select Right (‘input string’ , number of character);
Select Right (‘ SQL Server’,6)
This returns SERVER
▪ REPLACE()
This function replace all occurrences of a substring with a new substring:
Select REPLACE(‘Input String’,’Substring’,’New String’);
Select REPLACE(‘MS SQL SERVER IS TO DIFFICULT TO
LEARN’,’DIFFICULT’,’EASY)
This returns MS SQL SERVER IS TO EASY TO LEARN.
▪ REPLICATE()
This function repeats a string a specified number of times.
Select REPLICATE (‘input string’ Count);
Select left (‘ SQL Server’,3)
This returns SQL SERVER SQL SERVER SQL SERVER.
▪ RTRIM ()
This function returns string after truncating the trailing
blanks.
Select RTITM (‘input string ‘);
Select Right (‘ SQL SERVER ‘)
This returns SQL SERVER
▪ STUFF()
This function deletes a part of a string and then inserts
a substring into the string, beginning at the specified
position.
STUFF (Input String, Start Position, length, replace with
substring)
Select STUFF (‘SQL Tutorial’,1,3, ‘SQL Server’)
This returns SQL Server Tutorial.
2. User Defined Functions
These functions are created by the user in the
system database or in a user-defined database.
There are two types of user-defined functions.
I. Scalar Valued Functions
II. Table Valued Functions
I. Scalar Valued Functions
The user-defined scalar function also returns a
single value as a result of actions performed by the
function. We can return any datatype value from a
function.
The scalar functions help you simplify your code.
For example, you may have a complex calculation
that appears in many queries. Instead of including
the formula in every query, you can create a scalar
function that encapsulates the formula and uses it
in each query. can find it under Programmability >
Functions > Scalar-valued Functions.
Syntax:
CREATE FUNCTION <FUNCTION_NAME>(@<PARAMETER
NAME><DATATYPE>[SIZE],......)
RETURNS <RETURN PARAMETER/VARIABLE DATATYPE>
AS
BEGIN
<FUNCTION BODY / STATEMENTS>;
RETURN <RETURN PARAMETER / VARIABLE NAME>
END
Example:
Drop Function F_GRSAL
II. Table Valued Functions
The user-defined table valued functions returns a
table variable as a result of actions performed by
the function. The value of the table variable should
be derived from a single SELECT statement. Or the
table valued functions returns more than one
values or more than one column from the tables.
Or, a table-valued function is a user-defined
function that returns data of a table type. The
return type of a table-valued function is a table,
therefore, you can use the table-valued function
just like you would use a table.
Syntax:
CREATE [OR ALTER] FUNCTION
<FUNCTION_NAME> (@<PARMAETER NAME>
<DATATYPE> [SIZE],....)
RETURNS TABLE
AS
RETURN (<SELECT QUERY / STATEMENT>)
Function:
CREATE FUNCTION TVF2()
RETURNS TABLE
AS
RETURN (SELECT * FROM EMPLOYEE WHERE SAL >
20000)
Example:
Drop Function TVF1