SQL Server Inbuilt and User Defined Functions
SQL Server Inbuilt and User Defined Functions
Functions
Types of Functions
o System Functions
o User-Defined Functions
Advertisement
1
System Functions
2
SQL Server Functions
SELECTCast(10ASVARCHAR);
Output:
'10'
3
Coalesce Function Example:
SELECTCOALESCE(NULL,'value2','value3');
Output:
'value2'
SELECTIsnull('value','null');
Output:
'value'
SELECTIfnull('value','null');
Output:
'value'
SELECTNvl(NULL,'default value');
Output:
4
'default value'
SELECTCASE
WHEN1=1THEN'true'
ELSE'false'
END;
Output:
'true'
SELECTCONVERT(VARCHAR,10);
Output:
'10'
SELECT*
FROMString_split('apple,banana,cherry',',');
Output:
5
The random function generates a random number
between 0 and 1.
SELECTRand();
Output:
0.7750247322012854
If Function Example:
SELECTIF(1=1,'true','false');
Output:
'True'
SELECTMid('apple',2,3);
Output:
'ppl'
6
SELECTDateadd(day,7,'2023-05-05');
Output:
'2023-05-12'
SELECTDate_format('2023-05-05','%m/%d/%Y');
Output:
'05/05/2023'
SELECTDatepart(year,'2023-05-05');
Output:
2023
The day of the week function returns the day of the week
for a given date.
SELECTDayofweek('2023-05-05');
Output:
7
16. Week Function: WEEK()
SELECTWeek('2023-05-05');
Output:
18
SELECTWeekday('2023-05-05');
Output:
SELECTYear('2023-05-05');
Output:
2023
8
SELECTLen('banana');
Output:
SELECTLEFT('banana',3);
Output:
'ban'
SELECTRIGHT('banana',3);
Output:
'ana'
Output:
'banana'
9
23. Ascii Function: ASCII()
SELECTAscii('A');
Output:
65
SELECTConcat('apple','banana','cherry');
Output:
'applebananacherry'
SELECTFormat(12345.6789,'#,##0.00');
Output:
12,345.68
10
SELECTReplace('banana','a','e');
Output:
Benene
SELECTSubstring('banana',2,3);
Output:
'man'
SELECTInstr('banana','a');
Output:
SELECTStuff('banana',2,3,'oro');
Output:
'borona'
11
Looking for more SQL functions? Check out these
additional guides:
Database: Beverages
12
The sum function returns the total of all values in a
column.
SELECTSum(inventory)
FROMbeverages;
Output:
28
SELECTAverage(inventory)
FROMbeverages;
Output:
5.6
SELECTRound(330.3333,1)
Output:
13
330.3
SELECTTruncate(330.3333,1)
Output:
330.3
SELECTMax(price)
FROMbeverages;
Output:
400
14
SELECTMin(price)
FROMbeverages;
Output:
12
SELECTCount(inventory)
FROMbeverages
Output:
SELECTPower(10,2);
Output:
100
15
The log function returns the natural logarithmic value of a
given number. You can also specify the base by adding an
additional number, like in the example below.
SELECTLog(5,2)
Output:
2.3219280948873622
SELECTAbs(-8)
Output:
SELECTSqrt(100);
Output:
10
16
/* This example calculates the square root of 100, which is
10.
SELECTSin(100);
Output:
-0.50636564110975879
SELECTCos(100);
Output:
0.86231887228768389
SELECTMod(8,3);
Output:
17
/*For this example, the output would be 2 because 8
divided by 3 creates a remainder of 2.
SELECTCeil(91.8);
Output:
92
Note: The ceil function can be used with MySQL. For more
MySQL functions, click here.
18
are typically those in a column or group of columns
meeting certain conditions.
SELECT product,
Sum(revenue) AS Total_Revenue
FROM sales
GROUP BY product;
Database: Fruits
1 Apple LB 50 0.75
19
2 Orange LB 75 0.60
SELECTAvg(price)ASAveragePrice
FROMfruits;
Output:
1.24
SELECTCount(*)ASFruitsWithInventoryGreaterThan50
FROMfruits
WHEREinventory>=50;
Output:
20
3
SELECTMax(price)ASMaximumPrice
FROMfruits;
Output:
SELECTMin(inventory)ASMinimumInventory
FROMfruits;
Output:
15
SELECTSum(inventory)ASTotalInventory
FROMfruits;
Output:
260
Note: For more math functions, check out our guide here.
21
6. Group Concatenates Function: GROUP_CONCAT()
SELECTGroup_concat(`product
name`SEPARATOR', ')ASFruits
FROMfruits;
Output:
SELECTStd(price)ASStandardDeviation
FROMfruits;
Output:
1.80052309609941
SELECTVar(inventory)ASVariance
FROMfruits;
Output:
1616
22
The median function calculates the median value of a
specified column.
SELECTAvg(inventory)ASmedianinventory
FROM(
SELECTinventory
FROMfruits
ORDERBYinventorylimit2-
(
SELECTcount(*)
FROMfruits) % 2offset
(
SELECT(count(*)-1)/2
FROMfruits))subquery;
Output:
50
SELECTfirst_value(`productname`)OVER(ORDERBY`produc
tNAME`)ASfirstfruit
FROMfruits;
Output:
Apple
23
functions help you make informed decisions and uncover
meaningful patterns in your data.
User-Defined Functions
1. Scalar Functions
2. Table-Valued Functions
Scalar Functions
24
CREATE FUNCTION schema_name.function_name (para
meter_list)
RETURNS data_type AS
BEGIN
statements
RETURN value
END
The above syntax parameters are described below:
Example
25
We can call the scalar functions the same as the built-in
function in SQL Server. For example, we can call the above
udfNet_Sales function as below:
Table-Valued Functions
26
Table-valued functions in SQL Server are the user-
defined function that returns data of a table type. Since
this function's return type is a table, we can use it the
same way as we use a table.
Example
27
SQL Server also allows us to modify the table-valued
functions using the ALTER keyword instead of the CREATE
keyword. The rest of the script is the same.
28
We can call the multi-statement table-valued functions by
using the FROM clause of the SELECT query. For
example, we can call the above function as below:
Conclusion
29