Skip to content
Advertisement

Display date values as column

Below is my code to display date values as column name. But it won’t get the new data of the next month unless encoded in Pivot values. How should I do it dynamically?

Below is the sample output.

Advertisement

Answer

You could use “real” dynamic SQL: First get your list of months and store it into a nvarchar variable using XML path. Then store your pivot query into a new variable, using the built month string and execute it. See Fiddle for details:

SQL Fiddle

MS SQL Server 2017 Schema Setup:

Query 1:

Results:

Query 2:

Results:

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement