Built-in functions are the tools you will use in almost every real T-SQL query: they clean text, work with dates, make decisions and handle NULL safely. This guide goes through the most useful ones, each with a short example and its result.
Part of the SQL Server for Beginners series. This article is the written version of lesson 8 of the Bigland Network video course, with the same sample data. The results shown were worked out by hand from that data, so run every example on a test database, not on production data.
Watch lesson 8 as a video or open it on YouTube. The written lesson below follows the same steps and the same sample data.
Contents
- 1. String functions
- 2. Date functions
- 3. CASE and IIF
- 4. NULL handling
- 5. CAST and CONVERT
- 6. Putting it together
- 7. Mistakes and best practices
- 8. Practice
1. String functions
A scalar function takes values and returns a value, once for every row. Start with text, using the Employees table from Lesson 2: JOINs:
SELECT FirstName,
UPPER(FirstName) AS Up,
LEN(FirstName) AS Len,
LEFT(FirstName, 3) AS First3
FROM Employees ORDER BY Id;
| FirstName | Up | Len | First3 |
|---|---|---|---|
| Amina | AMINA | 5 | Ami |
| Youssef | YOUSSEF | 7 | You |
| Salma | SALMA | 5 | Sal |
| Karim | KARIM | 5 | Kar |
| Nadia | NADIA | 5 | Nad |
| Omar | OMAR | 4 | Oma |
CONCAT joins text, converts numbers for you and treats NULL as an empty string. The + operator does not: if one part is NULL, the whole result becomes NULL.
SELECT CONCAT(FirstName, ' earns ', Salary) AS Info
FROM Employees
WHERE Id <= 3;
| Info |
|---|
| Amina earns 4200 |
| Youssef earns 5200 |
| Salma earns 3600 |
SELECT SUBSTRING('Bigland Network', 1, 7) AS Part, -- Bigland
REPLACE('Bigland Network', ' ', '_') AS Rep, -- Bigland_Network
TRIM(' SQL ') AS Trimmed, -- SQL
CHARINDEX('Net', 'Bigland Network') AS Pos; -- 9
2. Date functions
DECLARE @d DATE = '2026-03-15';
SELECT YEAR(@d) AS Y, MONTH(@d) AS M, DAY(@d) AS D; -- 2026, 3, 15
DECLARE creates a variable. GETDATE() returns the current date and time.
DECLARE @d DATE = '2026-03-15';
SELECT DATEADD(DAY, 30, @d) AS Plus30, -- 2026-04-14
DATEDIFF(DAY, @d, '2026-12-25') AS DaysLeft, -- 285
EOMONTH(@d) AS MonthEnd; -- 2026-03-31
DECLARE @d DATE = '2026-03-15';
SELECT FORMAT(@d, 'dd/MM/yyyy') AS Style1, -- 15/03/2026
FORMAT(@d, 'MMMM yyyy') AS Style2, -- March 2026
DATENAME(WEEKDAY, @d) AS DayName; -- Sunday
FORMAT is great for reports but slower on large tables, so avoid it in heavy queries.
3. CASE and IIF
CASE works like if, then, else inside a query. SQL Server checks each WHEN in order and returns the first match. Always finish with END, and add ELSE.
SELECT FirstName, Salary,
CASE WHEN Salary >= 5000 THEN 'High'
WHEN Salary >= 4000 THEN 'Medium'
ELSE 'Low' END AS Level
FROM Employees ORDER BY Id;
| FirstName | Salary | Level |
|---|---|---|
| Amina | 4200 | Medium |
| Youssef | 5200 | High |
| Salma | 3600 | Low |
| Karim | 3100 | Low |
| Nadia | 4800 | Medium |
| Omar | 3900 | Low |
IIF is the short form for two choices:
SELECT FirstName, Salary,
IIF(Salary >= 4000, 'Yes', 'No') AS Above4000
FROM Employees ORDER BY Id;
Amina, Youssef and Nadia show Yes; Salma, Karim and Omar show No.
4. NULL handling
- NULL means unknown: it is not zero and not an empty string.
NULL = NULLis not true. Always useIS NULLorIS NOT NULL.- Math with NULL gives NULL:
10 + NULLis NULL. - Aggregates ignore NULL;
COUNT(*)counts every row.
ISNULL(value, replacement) replaces NULL (two arguments, SQL Server specific). COALESCE is the standard version: it returns the first value that is not NULL, from any number of values.
SELECT e.FirstName,
COALESCE(d.DeptName, 'No department') AS Dept
FROM Employees e
LEFT JOIN Departments d ON e.DeptId = d.DeptId
ORDER BY e.Id;
| FirstName | Dept |
|---|---|
| Amina | Sales |
| Youssef | IT |
| Salma | Sales |
| Karim | HR |
| Nadia | IT |
| Omar | No department |
NULLIF(a, b) returns NULL when both values are equal. Its best use is avoiding division by zero:
SELECT NULLIF(5, 5) AS A, -- NULL
NULLIF(5, 3) AS B, -- 5
100 / NULLIF(0, 0) AS SafeDivide; -- NULL instead of an error
SELECT COUNT(*) AS AllRows, COUNT(DeptId) AS WithDept, 10 + NULL AS Math
FROM Employees;
| AllRows | WithDept | Math |
|---|---|---|
| 6 | 5 | NULL |
If your numbers look wrong, check for NULL values first.
5. CAST, CONVERT and TRY_CAST
DECLARE @d DATE = '2026-03-15';
SELECT CAST('123' AS INT) + 1 AS A, -- 124
TRY_CAST('abc' AS INT) AS B, -- NULL instead of an error
CONVERT(VARCHAR(10), @d, 103) AS C; -- 15/03/2026 (style 103 = day/month/year)
6. Putting it together
SELECT CONCAT(e.FirstName, ' (', COALESCE(d.DeptName, 'None'), ')') AS Label
FROM Employees e
LEFT JOIN Departments d ON e.DeptId = d.DeptId
ORDER BY e.Id;
| Label |
|---|
| Amina (Sales) |
| Youssef (IT) |
| Salma (Sales) |
| Karim (HR) |
| Nadia (IT) |
| Omar (None) |
7. Common mistakes and best practices
- Writing
WHERE Col = NULLinstead ofIS NULL. - Joining text with
+when a value can be NULL. - A
CASEwithoutELSE: unmatched rows silently return NULL. - Dividing by a value that can be zero: use
NULLIF. - A function on a column in
WHERE(YEAR(HireDate) = 2026) prevents index use (SQL Server Indexes Explained: Clustered, Nonclustered and Covering). - Best practice: prefer
COALESCE, always addELSE, useTRY_CASTfor dirty data, filter dates with ranges (>= '2026-01-01' AND < '2027-01-01'), and give every calculated column an alias.
8. Practice
- Show first name, salary and Senior (4500 or more) or Junior with IIF.
- Show the date 90 days after 2026-01-01 and the months between 2026-01-01 and 2026-09-15.
- Build an email for each employee: first name in lowercase, then @bigland.com.
Answers: (1) IIF(Salary >= 4500, 'Senior', 'Junior'): Youssef and Nadia are Senior. (2) DATEADD(DAY, 90, '2026-01-01') gives 2026-04-01 and DATEDIFF(MONTH, '2026-01-01', '2026-09-15') gives 8. (3) CONCAT(LOWER(FirstName), '@bigland.com') gives amina@bigland.com, youssef@bigland.com and so on.
Frequently asked questions
ISNULL or COALESCE?
COALESCE is standard SQL and accepts many values, so it is usually the better choice. ISNULL takes exactly two arguments and is SQL Server specific.
Why does my text become NULL when I use plus?
With the plus operator, any NULL part makes the whole result NULL. Use CONCAT, which treats NULL as an empty string.
CAST or CONVERT?
CAST is standard SQL. CONVERT is SQL Server specific and adds style numbers, mainly useful for dates.
What to learn next
Next, create your own functions and keep temporary results in Lesson 9: Functions, Temp Tables and Table Variables. Previous lesson: Lesson 7: CTEs and Window Functions.
The whole series
- Lesson 1: SELECT, WHERE and ORDER BY
- Lesson 2: JOINs
- Lesson 3: GROUP BY, HAVING and Subqueries
- Lesson 4: INSERT, UPDATE, DELETE and Transactions
- Lesson 5: CREATE TABLE, Data Types and Constraints
- Lesson 6: Indexes, Views and Stored Procedures
- Lesson 7: CTEs and Window Functions
- Lesson 8: Built-in Functions (this article)
- Lesson 9: Functions, Temp Tables and Table Variables
- Lesson 10: Triggers and Audit Tables
- Lesson 11: Security: Logins, Users and Roles
- Lesson 12: Backup, Restore and Recovery Models
- Lesson 13: Query Performance Tuning
- Lesson 14: SQL Server Agent Jobs and Maintenance
- Lesson 15: Encryption and Auditing Basics
- Lesson 16: Working with JSON
- Lesson 17: Temporal Tables and Row History
- Lesson 18: Partitioning and Large Tables
- lesson 19: Full Text Search
- lesson 20: Query Store and Slow Queries
Sources and further reading
- Microsoft Learn, Transact-SQL reference (Database Engine), on learn.microsoft.com: the official syntax for every statement used here.
- Microsoft Learn, SQL Server documentation, for SQL Server Management Studio (SSMS) and the sample concepts.
More tutorials like this
Msg 208 Invalid Object Name in SQL Server: Causes and Fix
Fix SQL Server Msg 208 Invalid object name: check the spelling, the database and the schema, with real SSMS examples and scripts to find…
SQL Server Query Store Tutorial: Find Slow Queries, Compare Plans and Force a Plan
Turn on Query Store, find the most expensive queries with the sys.query_store views and the SSMS reports, compare the plans of one query and…
SQL Server Full Text Search Tutorial: CONTAINS, FORMSOF and CONTAINSTABLE
Search words inside text columns fast: create a full text catalog and index, use CONTAINS, word forms, phrases and rank the best matches with…
