Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Month as column name sql

Author  Topic 

tp
Starting Member

5 Posts

Posted - 2009-07-14 : 12:10:46
Hi There,

I have months as column names in my table. I have all the months each as a different column starting from Jan thru Dec. I am trying to get only three months (three columns) depending on the current month. For ex since the current month is July I want to select only July, Aug and Sep from this table. Since I don't want to hard code this, I am using a sql statement as below, but it returns me the month names itself instead of the data under that month column. Please advice!
SELECT DATENAME(MONTH,GETDATE()),DATENAME(MONTH,DATEADD(month, 1, GETDATE())),DATENAME(MONTH,DATEADD(month, 2, GETDATE())) ,product_id,product_title from product_tbl

Results as below:
1) July August September 1 ABC
2) July August September 2 EFG

Instead I want the result to be
1) data under the column july, aug, sep and the product id, title

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-07-14 : 13:07:10
look for PIVOT syntax in books online
Go to Top of Page
   

- Advertisement -