SQL UNPIVOT Generator — Normalize Wide Tables to Rows

Generate SQL Server UNPIVOT queries that convert wide tables with repeated column groups into normalized long-format tables. Choose between the native UNPIVOT operator or CROSS APPLY VALUES for NULL preservation and multi-column unpivoting.

SQL Server / Azure SQL
📚 Guide & Tutorial: SQL UNPIVOT: How to Rotate Columns Back into Rows — step-by-step walkthrough with examples.

Examples

Example: Normalize monthly columns into rows (native UNPIVOT)

Wide sales table with Jan–Dec columns. Produce a normalized table with Month and Revenue columns.

SELECT ProductName, Region, Month, Revenue
FROM dbo.SalesWide
UNPIVOT (
    Revenue FOR Month IN ([Jan],[Feb],[Mar],[Apr],
                          [May],[Jun],[Jul],[Aug],
                          [Sep],[Oct],[Nov],[Dec])
) AS upvt
ORDER BY ProductName, Month;

Rows where the monthly column is NULL are automatically excluded by the native UNPIVOT operator.

Example: CROSS APPLY — preserves NULLs, multi-column unpivot

Same wide table but you want to keep NULL revenue rows and also unpivot Units alongside Revenue.

SELECT ProductName, Region, m.Month, m.Revenue, m.Units
FROM dbo.SalesWide
CROSS APPLY (VALUES
    ('Jan', [Jan_Revenue], [Jan_Units]),
    ('Feb', [Feb_Revenue], [Feb_Units]),
    ('Mar', [Mar_Revenue], [Mar_Units])
) AS m(Month, Revenue, Units)
ORDER BY ProductName, m.Month;

CROSS APPLY VALUES handles multi-column unpivoting in a single step and preserves NULL cells.

What This Tool Does

The UNPIVOT Generator produces SQL Server queries that normalize a wide table — one with many repeated column groups — into a taller, narrower table with a type column and a value column. It supports two patterns: the native UNPIVOT operator (concise, automatic NULL removal) and CROSS APPLY VALUES (more flexible, preserves NULLs, supports multiple value columns).

Example: a sales table with Jan, Feb, Mar columns becomes a table with a Month column and a Revenue column — one row per product per month.

How to Use It

Native UNPIVOT vs CROSS APPLY VALUES

FeatureNative UNPIVOTCROSS APPLY VALUES
NULL handlingRemoves NULL rows silentlyPreserves NULLs
Multi-column unpivotOne value column onlyMultiple value columns in one step
Syntax complexitySimpler and shorterMore verbose
PerformanceComparableComparable
Data type requirementAll unpivoted columns must share a typeCan CAST individually per column
Best forSimple single-value normalizationComplex reshaping, NULL preservation, multi-value

Why UNPIVOT?

Data is often stored or exported in wide format: one row per entity with repeated column groups for each time period or category. This format is convenient for humans to read in a spreadsheet but problematic for SQL analysis — you cannot easily filter by month, aggregate across months, or join to a date dimension without first normalizing.

UNPIVOT (or its CROSS APPLY equivalent) converts wide format to long format, which is the correct shape for SQL aggregations, time-series analysis, and joins to reference tables.

Privacy & Security

All SQL generation runs in your browser using JavaScript. Your table names, column names, and schema details are never sent to any server.

Common Problems & Fixes

ProblemCauseFix
The type of column X conflicts with the type of other columnsColumns in the unpivot list have different data typesCAST all columns to the same type in a subquery before UNPIVOT
NULL rows disappear unexpectedlyNative UNPIVOT removes NULL values automaticallySwitch to CROSS APPLY VALUES pattern to preserve NULLs
Column name not foundColumn name contains spaces or special charactersWrap in square brackets: [Column Name]
Cannot unpivot more than 1000 columnsSQL Server limit on the IN listProcess in batches or use dynamic UNPIVOT pattern

💡 UNPIVOT is the reverse of PIVOT — if you need to go the other direction and spread row values into columns, use the PIVOT Generator for known column values or the Dynamic PIVOT Generator for values that change at runtime. For generating a complete date series to join your unpivoted data against, the Date Spine Generator produces gap-free date sequences.

UNPIVOT Workflow

UNPIVOT connects to the complete SQL transformation toolkit:

Frequently Asked Questions

What does UNPIVOT do in SQL Server?
UNPIVOT rotates columns into rows. It takes a set of columns you specify and stacks them into two columns: one for the column name (as a label) and one for the column value. It is the reverse operation of PIVOT.
When should I use CROSS APPLY VALUES instead of UNPIVOT?
CROSS APPLY VALUES is more flexible — it handles NULL values (UNPIVOT removes rows where the unpivoted value is NULL), supports multiple value columns simultaneously, and works better with mixed data types. Use UNPIVOT for simple cases; CROSS APPLY for complex ones.
Does UNPIVOT remove NULL rows?
Yes. The native UNPIVOT operator silently excludes rows where the unpivoted column value is NULL. If you need to preserve NULLs, use the CROSS APPLY VALUES pattern instead.
Can I UNPIVOT multiple value columns at once?
Not with the native UNPIVOT operator. Each UNPIVOT handles one value column. For multiple value columns (e.g. unpivoting both a quantity and a price column together), use CROSS APPLY VALUES which supports multi-column unpivoting in one step.
Can I filter which columns get unpivoted?
Yes — simply list only the columns you want to unpivot in the column list. Columns not listed remain in their original position in the SELECT.

Related SQL Tools