This month's discount Comprehensive Database Performance Health Check · Testimonials
SQLAuthority with Pinal Dave
Home
  • Consulting
    • Health Check
  • Free Videos
  • All Articles
    • AI
    • SQL Performance
    • SQL Tips and Tricks
    • Interview Questions and Answers
    • SQL Puzzle
    • SQL Video
    • SQLAuthority News
    • Personal
  • Books
  • Consulting
Gouache painting: a row of wooden crates of graduated widths along a barn wall, a chute pouring mangoes into the first and widest crate, one crate painted vermilion

Data Binning in T-SQL: Fixed Width, NTILE, CASE, DATE_BUCKET

October 13, 2023
Pinal Dave
SQL Tips and Tricks
Ranking Functions, SQL CASE, SQL DateTime, SQL Group By, SQL Scripts

Data binning turns a column of numbers into a few groups you can count and chart. This post runs four T-SQL methods on the same orders on SQL Server 2025: fixed width, NTILE, CASE ranges and DATE_BUCKET. It shows what each method tells you and one trap with BETWEEN.

Read More
A standard collar fitting remains separate from vessel-specific inspection tools.

SQL SERVER – Legal CASE Defaults and Row Dependent Values

July 21, 2021
Pinal Dave
SQL Tips and Tricks
SQL CASE, SQL Scripts, SQL Server, SQL Table Operation

A CASE Expression can appear in a default, but cannot reference another row column there. My earlier blanket prohibition was incorrect.

Read More
A gardener walking a row of berry bushes with three small baskets on one strap, sorting berries in a single pass.

Conditional Aggregation: Several Counts in One Pass With CASE

June 24, 2021
Pinal Dave
SQL Tips and Tricks
PIVOT and UNPIVOT, SQL CASE, SQL Group By, SQL Scripts, SQL Server

Use conditional aggregation for several ticket counts, avoid COUNT with ELSE zero, and calculate percentages with an explicit denominator.

Read More
Unmatched guide channels are inspected beside an empty receiving recess.

SQL Puzzle – Unsolved CASE Expression

April 20, 2021
Pinal Dave
SQL Puzzle
SQL CASE, SQL Scripts, SQL Server

This Unsolved CASE puzzle asks you to predict its result before execution. The preceding bitwise challenge drew more than 125 responses.

Read More
Gouache painting of a market stall with two rows of sorted apples and a vermilion arrow post pointing at one row

ORDER BY a Parameter in SQL Server: CASE, Types and Plans

June 8, 2020
Pinal Dave
SQL Tips and Tricks
SQL CASE, SQL Order By, SQL Scripts, SQL Stored Procedure

To ORDER BY a parameter in SQL Server, write one CASE expression for each column and direction. This post builds the procedure, shows the type conversion and DISTINCT errors that trip people up, measures the cost of the sort that CASE forces, and compares OPTION (RECOMPILE) and dynamic SQL.

Read More
Gouache painting of a short straight path and a long winding path leading to the same vermilion gate

IIF in SQL Server: IF THEN Logic Compared With CASE

December 24, 2018
Pinal Dave
SQL Tips and Tricks
Execution Plan, SQL CASE, SQL Function, SQL Scripts

IIF in SQL Server is a short way to write IF THEN logic with two outcomes. This post compares it with CASE in a small juice shop table, shows that both produce the same plan, and covers NULL, data types and the WHERE clause.

Read More
Two differently colored shoes fitting the same adjustable shoe stretcher

Names That Differ Only by Case: Finding Them With COLLATE

June 20, 2018
Pinal Dave
SQL Tips and Tricks
Best Practices, SQL CASE, SQL Server

A case-insensitive target can turn two rows into one. This walk-through finds the colliding names first, including the ones with trailing spaces.

Read More
1 2 Next
  • Books
  • Testimonials
  • Privacy Policy

© 2006 – 2026 All rights reserved. pinal @ SQLAuthority.com