SQL Server CAST vs CONVERT

SQL Server CAST vs CONVERT

A nightly order import fails because one row contains “pending” in an amount column. Meanwhile, a report displays dates differently between environments, and a customer lookup scans far more data than expected. Each problem involves conversion, but each needs a different fix. The SQL Server CAST vs CONVERT decision is straightforward: use CAST for standard, … Read more

NULLIF vs COALESCE

NULLIF vs COALESCE

In this guide, I will break down the essential differences between NULLIF and COALESCE, explore their syntax, compare their performance under the hood, and walk through practical scenarios where each function shines. NULLIF vs COALESCE Understanding SQL NULL Values and Why They Matter Before diving into the functions themselves, we need to address why NULL … Read more

SQL COUNT vs COUNT DISTINCT Difference

SQL COUNT vs COUNT DISTINCT Difference

A sales dashboard shows 40,000 customers, but the customer table contains only 12,000 accounts. The report joined customers to orders and counted the resulting rows. Repeat buyers appeared several times, turning an order count into an inflated customer count. The SQL COUNT vs COUNT DISTINCT difference comes down to what you count: COUNT(*) counts rows, … Read more

SQL Server NULLIF: Syntax, Examples

SQL Server NULLIF

SQL Server NULLIF compares two expressions and returns NULL when they are equal. Otherwise, it returns the first expression. That makes it useful for protecting division, normalizing placeholder values, and excluding specific values from calculations. You’ll learn how to use NULLIF correctly, control data types, combine it with other expressions, and test reporting logic without … Read more

SQL Server CEILING

SQL Server CEILING

An order-processing report says 25 items fit into two cartons that each hold 12. The query runs without errors, but the warehouse needs three cartons. Integer division removed the remainder before the rounding function could see it. SQL Server CEILING returns the smallest whole-number value greater than or equal to its input. It helps calculate … Read more

How to Convert Rows Into Columns in SQL

How to Convert Rows Into Columns in SQL

A finance team once asked me for a report that looked simple on paper: one line per product, with four columns for Q1 through Q4. Their database stored sales the way databases should, with one row per product per quarter. An analyst had been exporting the data to Excel and rebuilding the layout by hand … Read more

How to Find Even and Odd Numbers in SQL Server

How to Find Even and Odd Numbers in SQL Server

A warehouse team once asked me to split pick tickets between two packing lines. Odd quantities went to Line A, which handled loose items, and even quantities went to Line B, which handled boxed pairs. The developer wrote WHERE Quantity % 2 = 1 and shipped it. Within a week, returns adjustments were landing on … Read more

How to Use CASE WHEN in SQL Server

How to Use CASE WHEN in SQL Server

A support lead once asked me why her “high priority” ticket report showed refunds as low priority. The developer had written a long chain of IF logic in the application, and every time the rules changed, the app and the report disagreed. We moved the rule into one T-SQL expression, and the two numbers matched … Read more

How to Calculate Total Sales in SQL

How to Calculate Total Sales in SQL

A finance manager once sent me two dashboard numbers for the same month. They were $38,000 apart, and both came from “total sales” queries. One query summed order headers, and the other joined to order lines and counted each header total once per line. Nobody had been careless. The query had simply never been checked … Read more

How to Find Percentage of Total in SQL

How to Find Percentage of Total in SQL

A finance director once forwarded me a screenshot of her new sales dashboard. Every region showed a share of revenue of exactly 0 percent, except the largest one, which showed 100. The query looked fine. It divided each region’s sales by total sales and multiplied by 100. The columns held whole numbers, and SQL Server … Read more