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.
Query Configuration
Generated T-SQL
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
- Enter your Table Name including schema prefix
- List the ID columns to keep as-is — these are the row identifiers (product name, region, etc.)
- List the columns to unpivot — the repeated column groups (Jan, Feb, Mar, or Q1_Sales, Q2_Sales, etc.)
- Name the Label column (what the old column name becomes) and the Value column
- Choose the pattern: native UNPIVOT or CROSS APPLY VALUES
- Click Generate SQL and copy the output
Native UNPIVOT vs CROSS APPLY VALUES
| Feature | Native UNPIVOT | CROSS APPLY VALUES |
|---|---|---|
| NULL handling | Removes NULL rows silently | Preserves NULLs |
| Multi-column unpivot | One value column only | Multiple value columns in one step |
| Syntax complexity | Simpler and shorter | More verbose |
| Performance | Comparable | Comparable |
| Data type requirement | All unpivoted columns must share a type | Can CAST individually per column |
| Best for | Simple single-value normalization | Complex 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
| Problem | Cause | Fix |
|---|---|---|
| The type of column X conflicts with the type of other columns | Columns in the unpivot list have different data types | CAST all columns to the same type in a subquery before UNPIVOT |
| NULL rows disappear unexpectedly | Native UNPIVOT removes NULL values automatically | Switch to CROSS APPLY VALUES pattern to preserve NULLs |
| Column name not found | Column name contains spaces or special characters | Wrap in square brackets: [Column Name] |
| Cannot unpivot more than 1000 columns | SQL Server limit on the IN list | Process 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:
- PIVOT generator — go the other direction, from rows to columns
- Visual Pivot Builder — explore data structure before writing PIVOT or UNPIVOT queries
- Deduplicate after unpivoting — unpivoting can create duplicate rows
- Format the UNPIVOT query for code review
