Mastering SQL: A Comprehensive Guide to Database Management and Querying — Part IV

A Comprehensive Guide to Microsoft SQL Server Functions

S Praneel
4 min readSep 24, 2023

Introduction

Microsoft SQL Server provides a wide array of built-in functions to manipulate, analyze, and transform data. These functions are essential tools for SQL developers and database administrators. In this article, we’ll explore various categories of SQL Server functions, including aggregate functions, date functions, string functions, system functions, window functions, mathematical functions, and user-defined functions, both scalar and table-valued, with examples to illustrate their usage.

1. Aggregate Functions

Aggregate functions perform calculations on a set of values and return a single result. Common aggregate functions include SUM, AVG, COUNT, MAX, and MIN.

Example:

This SQL query comprises multiple aggregate functions to derive key statistical insights from the “salary” column within the “employees” table. Specifically, it calculates the total sum of salaries, the average salary, the count of salary entries, the maximum salary value, and the minimum salary value for all employees in the dataset..

2. Date Functions

Date functions in SQL Server allow you to manipulate and extract information from date and time data types. Examples include GETDATE(), DATEADD, DATEDIFF, and FORMAT.

Example:

  1. ETDATE(): Retrieves the current date and time, providing an accurate timestamp.
  2. DATEADD(): Demonstrates how to add a specified number of days (in this case, 30 days) to the current date, allowing for date manipulation.
  3. DATEDIFF(): Calculates the difference in days between two given dates, offering a valuable tool for date-related calculations.
  4. FORMAT(): Shows how to format a date as a string in a user-defined pattern, facilitating customized date representations.

3. String Functions

String functions manipulate text data. Common string functions include CONCAT, LEN, SUBSTRING, and REPLACE.

Example:

CONCAT Function: Combines first and last names into a full name.
LEN/LENGTH Function:Computes the length of first names.
SUBSTRING Function:Extracts a shortened description.
REPLACE Function:Updates shipping addresses by replacing ‘Street’ with ‘St.’.

4. System Functions

System functions provide information about the SQL Server environment. An example is @@VERSION, which returns the SQL Server version information.

Example:

This query retrieves the SQL Server version.

5. Window Functions

Window functions operate on a set of rows related to the current row. They include functions like ROW_NUMBER(), RANK(), DENSE_RANK(), and NTILE().

Example:

This query assigns a rank to each employee based on their salary.

6. Mathematical Functions

Mathematical functions perform mathematical operations on numeric data. Common mathematical functions include ROUND, CEILING, FLOOR, and POWER.

Example:

This query rounds the salary to two decimal places.

7. Rank Functions (ROW_NUMBER, RANK, DENSE_RANK, NTILE)

Rank functions are a subset of window functions that assign rankings to rows based on specified criteria. These functions include:
- ROW_NUMBER(): Assigns a unique number to each row.
- RANK(): Assigns a rank to rows with the same values, leaving gaps.
- DENSE_RANK(): Assigns a rank to rows with the same values, without gaps.
- NTILE(): Divides the result set into specified number of roughly equal parts.

Example (using NTILE):

This query divides employees into four salary quartiles.

8. User-Defined Functions (Scalar and Table-Valued)

SQL Server allows you to create custom functions:
- Scalar User-Defined Functions (UDFs) return a single value.
- Table-Valued Functions (TVFs) return a table as a result.

Example (Scalar UDF):

This example creates a scalar UDF to calculate taxes on employee salaries.

Example (Table-Valued UDF):

This example creates a table-valued UDF to retrieve employees earning above a certain threshold.

Conclusion

Microsoft SQL Server offers a robust set of functions to handle various data manipulation tasks. Understanding and utilizing these functions effectively can greatly enhance your SQL development and database management capabilities, enabling you to extract valuable insights and perform complex operations on your data. Whether you’re aggregating data, working with dates, manipulating strings, or creating custom functions, SQL Server provides the tools you need to get the job done efficiently.

Next Part available at : https://medium.com/@spraneel/mastering-sql-a-comprehensive-guide-to-database-management-and-querying-part-v-a0ffbc6463b4

--

--