Change the column name according to the date of the request (month)

I get a commission for the seller every month. I need to change the column name according to the month in which I run the query.

for example, when I run a query on 30/1/2016, then the name of the cube commission for month 1orcommission for month January

as when running a query on 30/3/2016, then the name of the cube commission for month 3orcommission for month March

I tried this code:

Select T0.commission as 'Commission for month month(getdate())'

he does not work

+4
source share
2 answers

This query will return some dummy data (table names from the system view). The column will have a name with a dynamically generated row:

DECLARE @cmd VARCHAR(MAX)='SELECT TABLE_NAME AS [' + 'placeYourColHere_' + CAST(MONTH(GETDATE()) AS VARCHAR(10)) + '] FROM INFORMATION_SCHEMA.TABLES;';
EXEC(@cmd);
+3
source

Try this request

select 'Commission for month ' + CONVERT(NVARCHAR(2),DATEPART(MM,GETDATE())) AS 'Date'
+1
source

All Articles