SQL Server Built-in Functions: String, Date, CASE, IIF, COALESCE and NULL Handling

SQL Server 6 min readUpdated Nov 20, 2026
5 views0 comments

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

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 = NULL is not true. Always use IS NULL or IS NOT NULL.
  • Math with NULL gives NULL: 10 + NULL is 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 = NULL instead of IS NULL.
  • Joining text with + when a value can be NULL.
  • A CASE without ELSE: 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 add ELSE, use TRY_CAST for 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

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.
Did you find this helpful?Tap the heart to let us know. It takes one second and no account.

More tutorials like this

Leave a Comment

Your email address will not be published. Required fields are marked *

Bigland Network IT

Practical tutorials for people who run, build and fix things: operating systems, databases, development, networking and security.

Topics

More

Site

© 2026 Bigland Network. All rights reserved.Always test commands on a non-production system first.
Scroll to Top