Labels

Showing posts with label ANSI SQL. Show all posts
Showing posts with label ANSI SQL. Show all posts

Saturday, April 11, 2026

ISNULL vs COALESCE


FeatureISNULLCOALESCE
OriginT-SQL Specific (Microsoft)ANSI SQL Standard (Universal)
ParametersAccepts only 2 arguments.Accepts 2 or more arguments.
Data TypeUses the type of the first argument.Uses the type with the highest precedence.
PerformanceSlightly faster for simple 1-to-1 checks.Can be slightly slower in complex subqueries.
EvaluationEvaluates the first argument once.May evaluate the expression multiple times.
Key Differences Explained

1. Number of Arguments
  • ISNULL: Limited to checking one value and providing one replacement.
                SELECT ISNULL(Price, 0) FROM Products;

  • COALESCE: Can check multiple columns/values in order and returns the first non-null one.
                SELECT COALESCE(Mobile, HomePhone, OfficePhone, 'N/A') FROM Contacts;

2. Data Type Handling

This is often where developers run into unexpected results or errors.
  • ISNULL is rigid. It looks at the data type of the first expression and tries to convert the replacement to match it. If the replacement is longer than the first argument, it may even truncate the data.
  • COALESCE follows "Data Type Precedence." It looks at all arguments and converts everything to the type that ranks highest.

3. Performance in SQL Server

In most standard queries, the performance difference is negligible. However:
  • ISNULL is built directly into the SQL Server engine as a function.
  • COALESCE is actually translated by the engine into a CASE statement.
  • Best Practice: Use ISNULL for high-frequency, simple null-to-zero conversions where performance is critical. Use COALESCE for multi-column logic or when writing code that needs to be portable to other databases like PostgreSQL or MySQL.

Note :
Use ISNULL when you are sure you only have two values and want to force a specific data type. Use COALESCE when you need flexibility, multiple fallbacks, or are following ANSI standards


Wednesday, June 14, 2017

T-SQL Trick: How to Find Monday (or Any Day) of the Current Week

Ever needed to filter a report so it only shows data starting from the beginning of the current week? In SQL Server, calculating the date for "this past Monday" is a classic challenge that every dev faces eventually.

While there are many ways to do it, the most efficient method uses a clever combination of DATEDIFF and DATEADD.

The Logic: How It Works

The secret sauce is calculating the number of weeks that have passed since "Day 0" (which is 1900-01-01, a Monday) and then adding those weeks back to that same starting point.

Here is the cleanest way to write it:

-- Using the 'WK' shorthand

SELECT DATEADD(WK, DATEDIFF(WK, 0, GETDATE()), 0) AS MondayOfCurrentWeek;

-- Or using the full 'WEEK' keyword for readability

SELECT DATEADD(WEEK, DATEDIFF(WEEK, 0, GETDATE()), 0) AS MondayOfCurrentWeek;

Both queries will return the exact date of Monday for the current week (e.g., 2026-04-13).


Getting Other Days of the Week

The beauty of this snippet is its flexibility. If you need to find Tuesday, Wednesday, or any other day, you don't need to rewrite the logic—you just need to shift the "anchor" value (the 0 in the formula).

By incrementing the integer, you move the target day forward:

-- Tuesday

SELECT DATEADD(WK, DATEDIFF(WK, 0, GETDATE()), 1) AS Tuesday;

-- Wednesday

SELECT DATEADD(WK, DATEDIFF(WK, 0, GETDATE()), 2) AS Wednesday;

-- Thursday

SELECT DATEADD(WK, DATEDIFF(WK, 0, GETDATE()), 3) AS Thursday;


Why use this instead of DATEPART?

The main advantage here is that this method is deterministic and ignores your server's DATEFIRST settings. Whether your server thinks the week starts on Sunday or Monday, this math always anchors back to that original Monday in 1900, keeping your reports consistent across different environments.