SQL Server LAG and LEAD Functions

SQL Server LAG and LEAD Functions

A sales dashboard shows the wrong month-over-month change even though its totals look correct. The reporting query compares one customer’s revenue with another customer’s previous row. Meanwhile, a separate batch process uses a loop to calculate gaps between orders. SQL Server LAG and LEAD Functions solve adjacent-row comparisons without requiring a self-join for every calculation. … Read more

SQL Server CHARINDEX vs PATINDEX

SQL Server CHARINDEX vs PATINDEX

The main difference in SQL Server CHARINDEX vs PATINDEX is straightforward: CHARINDEX finds a literal substring, while PATINDEX finds a wildcard pattern. Both return a starting position, but only CHARINDEX accepts a starting search location. You’ll learn how to choose the right function, extract values safely, handle NULLs and collations, and keep string searches from … Read more

SQL Server LEN vs DATALENGTH

SQL Server LEN vs DATALENGTH

SQL Server LEN vs DATALENGTH comes down to what you measure. LEN counts characters but excludes trailing spaces. DATALENGTH counts the bytes representing an expression, including trailing spaces in string values. Unicode encoding and fixed-length data types make that distinction especially important. You’ll learn how to choose the right function, validate incoming data, investigate padding, … Read more

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

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 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

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

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 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