Explore the power of Transact-SQL functions for data manipulation and retrieval.
T-SQL functions are pre-defined routines that perform a specific task, typically involving data. They accept input parameters, perform operations, and return a single value. Functions are crucial for manipulating data, performing calculations, and enhancing the readability and reusability of your SQL code.
Scalar functions return a single value for each row processed. These are the most common type of function.
GETDATE()
Returns the current database system date and time.
SELECT GETDATE() AS CurrentDateTime;
Aggregate functions perform a calculation on a set of rows and return a single value. They are often used with the GROUP BY
clause.
COUNT(expression)
Returns the number of rows that match a specified criterion.
SELECT COUNT(*) AS NumberOfOrders FROM Orders;
Table-valued functions return a table as their result set. They can be used in the FROM
clause of a query.
CREATE FUNCTION dbo.GetEmployeeByDepartment (@DepartmentID INT)
RETURNS TABLE
AS
RETURN
(
SELECT EmployeeID, FirstName, LastName
FROM Employees
WHERE DepartmentID = @DepartmentID
);
Returns a list of employees belonging to a specific department.
SELECT EmployeeID, FirstName, LastName
FROM dbo.GetEmployeeByDepartment(3);
LEN(string_expression)
Returns the number of characters in a string expression, excluding trailing blanks.
SELECT LEN('SQL Functions') AS StringLength;
-- Result: 13
LEFT(character_expression, number_of_characters)
Returns the left part of a character string with the specified number of characters.
SELECT LEFT('Transact-SQL', 8) AS LeftPart;
-- Result: Transact
RIGHT(character_expression, number_of_characters)
Returns the right part of a character string with the specified number of characters.
SELECT RIGHT('Transact-SQL', 3) AS RightPart;
-- Result: SQL
SUBSTRING(expression, start, length)
Returns a part of a character, binary, text or image expression. Starts at the specified position and returns the specified number of characters.
SELECT SUBSTRING('SQL Server Functions', 5, 6) AS MiddlePart;
-- Result: Server
UPPER(character_expression)
Converts a character expression to uppercase.
SELECT UPPER('mixedCaseString') AS UppercaseString;
-- Result: MIXEDCASESTRING
LOWER(character_expression)
Converts a character expression to lowercase.
SELECT LOWER('MixedCaseString') AS LowercaseString;
-- Result: mixedcasestring
REPLACE(string_expression, string_to_replace, replacement_string)
Returns the character string where all occurrences of a specified string are replaced with another specified string.
SELECT REPLACE('Hello World', 'World', 'SQL') AS ReplacedString;
-- Result: Hello SQL
TRIM( [ [ LEADING | TRAILING | BOTH ] [ characters FROM ] ] string_expression )
Removes leading and/or trailing spaces or specified characters from a string.
SELECT TRIM(' Spaces ') AS TrimmedString;
-- Result: Spaces
ABS(numeric_expression)
Returns the absolute value of a numeric expression.
SELECT ABS(-100.5) AS AbsoluteValue;
-- Result: 100.5
ROUND(numeric_expression, length [, function])
Returns a numeric value, rounded to the specified number of decimal places.
SELECT ROUND(123.4567, 2) AS RoundedValue;
-- Result: 123.46
CEILING(numeric_expression)
Returns the smallest integer greater than or equal to the specified numeric expression.
SELECT CEILING(10.2) AS CeilingValue;
-- Result: 11
FLOOR(numeric_expression)
Returns the largest integer less than or equal to the specified numeric expression.
SELECT FLOOR(10.8) AS FloorValue;
-- Result: 10
POWER(numeric_expression, exponent)
Returns the value of a numeric expression raised to the specified power.
SELECT POWER(2, 5) AS PowerResult;
-- Result: 32
GETDATE()
Returns the current database system date and time as a datetime value.
SELECT GETDATE() AS CurrentDateTime;
DATEPART(datepart, date)
Returns an integer representing the specified part of a specified date.
SELECT DATEPART(year, '2023-10-27') AS YearPart;
-- Result: 2023
DATENAME(datepart, date)
Returns a character string representing the specified part of a specified date.
SELECT DATENAME(weekday, '2023-10-27') AS DayName;
-- Result: Friday
DATEDIFF(datepart, startdate, enddate)
Returns the difference between two dates, in the specified datepart.
SELECT DATEDIFF(day, '2023-10-20', '2023-10-27') AS DaysDifference;
-- Result: 7
DATEADD(datepart, number, date)
Returns a specific date with a specified interval added to it.
SELECT DATEADD(week, 1, '2023-10-27') AS DateAfterOneWeek;
-- Result: 2023-11-03
USER_NAME([user_id])
Returns the name of the database user associated with the specified user ID.
SELECT USER_NAME() AS CurrentUserName;
DB_NAME([database_id])
Returns the name of the specified database.
SELECT DB_NAME() AS CurrentDatabaseName;
HOST_NAME()
Returns the network computer name of the client that is connected to SQL Server.
SELECT HOST_NAME() AS ClientHostName;