Introduction
In this article, we will learn how to create and use user-defined functions in SQL server. We write the function for reusability purposes to avoid complexity and duplication of code. User-defined functions are precompiled and ready for later use. Using SQL server, you can easily create, view, and modify the user-defined functions. SQL Server user-defined functions are routines that accept the parameters and perform the action such as complex calculation and return the result of that action as value.
- The function accepts only input parameters
- The functions don’t support the exceptional handling
Type of function
SQL provides two types of functions.
- System-defined function
- User-defined function
Benefits of user-defined functions
- It supports modular programming
- Create once and reuse any number of times
- Once compiled, same execution plan we can reuse, It reduces the compilation cost
- It reduces the network traffic
Why we use the function
There are benefits of using the function. Some of them are the following.
- We can use a user-defined function in select as well as where class.
- We can use a user-defined function used as rawest.
- We can use a user-defined function as parameterizing views.
System-defined function
System-defined functions provided by Microsoft SQL Server itself.
User Defined function
In SQL Server there are three types of user-defined functions
- Scalar-Valued Function
The function which returns the single value, It may or may not have the parameters.
- Inline table-valued function
An inline table-valued function returns a row set of SQL server data type and specifies only the table keyword in the return clause, without table definition information.
The code inside the functions returns a single return statement that invokes the select statement.
- Multi-Statement table valued function
This function returns the table variable as a result of an action performed by function and table variable should be explicitly declared and defined by whose value can be derived from multiple SQL statements.
As you know the built-in functions, before going to look at user-defined function, we look at one of the existing system functions. Let’s take the date function.
- declare @datetime datetime = getdate()
- print @datetime
The function may or not may have a parameter but it always returns the value.
Output

Now, we are going to understand the user-defined function.
For example Weekdays – for all days of the week it returns a sequential day of the week.
- CREATE FUNCTION Weekdays(@Day varchar)
- RETURNS INT -- in which datatype you will accept/return output
- AS BEGIN
- IF(@Day IS NULL)
- RETURN 0;
- DECLARE @Output INT –-Declare the veriable
- --Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
- SELECT @Output =
- CASE WHEN SUBSTRING(@Day, 1,1) = 'M' THEN 1
- WHEN SUBSTRING(@Day, 1,1) = 'Tu' THEN 2
- WHEN SUBSTRING(@Day, 1,1) = 'W' THEN 3
- WHEN SUBSTRING(@Day, 1,1) = 'Th' THEN 4
- WHEN SUBSTRING(@Day, 1,1) = 'F' THEN 5
- WHEN SUBSTRING(@Day, 1,1) = 'Sa' THEN 6
- WHEN SUBSTRING(@Day, 1,1) = 'Su' THEN 7
- ELSE
- 99 END
- RETURN @Output;
- END
Execute the below code,
PRINT Weekdays('Monday')
Output

Now, we are going to check if the function is created or not,

- PRINT [dbo].[Weekdays]('Monday')
- PRINT [dbo].[Weekdays]('Friday')
- PRINT [dbo].[Weekdays]('Jitendra')
- declare @datetime datetime = getdate()
- print @datetime
Output

Now we are going to create the table, for example, we are converting existing employee to uppercase. We need to create the table like below.
Employee1
- CREATE TABLE [dbo].[Employee1](
- [eid] [int] NOT NULL,
- [ename] [varchar](20) NULL,
- [designation] [varchar](20) NULL,
- [mgrid] [int] NULL,
- PRIMARY KEY CLUSTERED
- (
- [eid] ASC
- )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
- ) ON [PRIMARY]






Farhan AhmedPosted Sep 25, 2018, 9:23 PM
Hi i know all these system functions. I wanted user define function like calculate days,months and years between to date start date and end date.