Click here to monitor SSC
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.

Enjoy!

Total article views: 1044 | Views in the last 30 days: 2
 
Related Articles
FORUM

Included Columns

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

ARTICLE

Covering Index using Included Columns

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

ARTICLE

Include Column Headers From SSMS

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

FORUM

Including extra columns in a grouping

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

ARTICLE

Stairway to SQL Server Indexes: Level 5, Included Columns

Included columns enable nonclustered indexes to become covering indexes for a variety of queries, im...

Tags
date    
group    
pivot    
 
Contribute