SQL Clone
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

PivotByDate clr stored procedure

By Louis Nardozi,

This clr stored procedure will pivot any temp table by date.

The temp table must contain 1 to n NVarchar,Varchar or char columns, which the output will be grouped by.

The temp table must contain 1 to N numeric (not amy type char or any type of date) columns.

The temp table must contain exactly one column of date type. Probably any date type would work but I've only tested it with datetime.

Just include the .cs file in your existing clr library and deploy.

I haven't included a project because  I figure most people will just include it in their library.

To run:

exec PivotByDate '#temp'

Note the quotes around the table name.


Total article views: 1055 | Views in the last 30 days: 1
Related Articles

Included Columns

Why not include every table field in the included columns list for every index?


Covering Index using Included Columns

This article from Josef Richberg details the benefits of included columns for use in creating a cove...


Include Column Headers From SSMS

Save time when copying data from SSMS to Excel by configuring the options to Include Column Headers....


Including extra columns in a grouping

How to include extra columns from the same table in a recordset based on the grouping.


A column contains both CHAR and INTEGER

A CHAR column contains both CHAR and INTEGER, how to handle them differently ?