Lecture3_DBP2024
Lecture3_DBP2024
Lecture3_DBP2024
Note: SQL built-in functions may or may not take parameters but always returns a value.
Functions in SQL Server
Built-in String Functions in SQL Server
Function Example Output
ASCII(Character_Expression) Select ASCII(‘A’) 65
LTRIM(Character_Expression) Select LTRIM(‘ Hello’) Hello
LOWER(Character_Expression) Select LOWER(‘CONVERT This String ’) convert this string
UPPER(Character_Expression) Select UPPER(‘CONVERT This String’ ) CONVERT THIS STRING
REVERSE(‘Any_String_Expression’) Select REVERSE(‘ABCDEFG’) GFEDCBA
LEN(String_Expression) Select LEN(‘ Functions ‘) 9
CHARINDEX(‘Find’, ‘Search’, Select CHARINDEX(‘@’,’hina@aaa.com’,1) 5
‘Start_Location’)
SUBSTRING(‘expression’, ‘Start’, Select SUBSTRING( ‘info@dotnettutorials.net’ dotnettutorials.net
‘Length’) ,6, 19)
Functions in SQL Server
Built-in other Functions
Getting Information About Data.
IsDate()
IsNumeric()
Functions for Handling null Values.
IsNull()
Conversion Functions.
Cast (expression AS data_type)
Convert(datatype [(length)], expression [, style])
Functions for Handling Identity Values
@@identity
Functions in SQL Server
Built-in Date and Time Functions
The following set of functions is designed to process data and time values and expressions.
Get (Current) Date GetDate() :It will return the system time in datetime format.
Extracting Parts of Date and Time. From time to time, just one component of the date and
time value will be extracted.
• DAY(date)
• MONTH(date)
• YEAR(date)
Date and Time Calculations Transact-SQL contains two functions for performing calculations
on date and time
DateAdd(datepart, number, date)
DateDiff(datepart, startdate, enddate)
Functions in SQL Server
Built-in Aggregate Functions
perform an operation on a set of records and return a single value. They can be used in the
following situations:
The selection list of the Select statement - A Having clause - A Compute clause
Function Description
Avg([All | Distinct] expression) Returns the average value in the group.
Count([All | Distinct] expression |*) Counts the number of items in the group.
Sum(expression) Returns the sum of the expression's values.
Max(expression) Returns the maximum value in the expression.
Min(expression) Returns the minimum value in the expression.
Functions in SQL Server
OVER Clause in SQL Server with Examples
SELECT dbo.<FunctionName>(Value)
SELECT <DataBaseName>.dbo.<FunctionName>(Value)
Functions in SQL Server
• Examples: Write a Scalar Function to get the date difference.
CREATE FUNCTION CalculateAge (@DOB DATE)
RETURNS INT
AS
BEGIN
DECLARE @AGE INT
SET @AGE = DATEDIFF(YEAR, @DOB, GETDATE())-
CASE
WHEN (MONTH(@DOB) > MONTH(GETDATE())) OR
(MONTH(@DOB) = MONTH(GETDATE()) AND
DAY(@DOB) > DAY(GETDATE()))
THEN 1
ELSE 0 --Calling the above function:
END SELECT dbo.CalculateAge ('02/29/1988')
RETURN @AGE SELECT dbo.CalculateAge ('02/29/1988') AS AGE
END
Functions in SQL Server
Scalar Valued Function in Select Clause:
How can use CalculateAge(@DOB DATE) takes the DOB value from Employee
table and returns the Age .
Let’s write both Inline and Multi-Statement Table-Valued functions in SQL Server
that return the following output.
Functions in SQL Server
-- Multi-statement Table Valued function:
CREATE FUNCTION MSTVF_GetEmployees()
RETURNS @Table Table (ID int, Name varchar(20), DOB Date)
AS
BEGIN
INSERT INTO @Table
SELECT ID, Name, Cast(DOB AS Date)
FROM Employee
Return
End -- Inline Table Valued function:
CREATE FUNCTION ILTVF_GetEmployees()
RETURNS TABLE
AS
RETURN (SELECT ID, Name, Cast(DOB AS Date) AS
DOB
FROM Employee)
Functions in SQL Server
SELECT * FROM dbo.MSTVF_GetEmployees()
--or
SELECT * FROM dbo.ILTVF_GetEmployees()
can perform Select. Update, Insert and Delete Can only be used to perform select operations.
operations
provides the options to perform Transaction These operations are not possible in a function.
Management, Error Handling, etc.
We call a procedure using EXECUTE/ EXEC command A function is called by using SELECT command only.
From a procedure, we can call another procedure or A function we can call another function but not a
a function procedure.
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: