Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
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: 953 | Views in the last 30 days: 8
 
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

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones
Editor, SQLServerCentral.com

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones