July 8, 2015 at 5:22 am
The keyword AS normally creates a column name for an alias.
Can someone please tell me how to do the following as a column name:
month1: + convert( varchar(10),GETDATE(),110)
so that the column name displays "month1:" + today's date?
I've tried to declare a varchar, but "AS @varchar" gives me an error.
It should be displayed like the following example:
Select 1 AS Month1: 07/07/2015 where the part after the AS keyword now becomes the column name.
----------------------|
Month1: 07/07/2015|
----------------------|
1 |
----------------------|
July 8, 2015 at 5:38 am
i wasn't sure of your use case, so here's three examples:
alias = {calculation}
{calculation}AS Alias
CalculatedColumn As {calculation}
SELECT Month1 = convert( varchar(10),GETDATE(),110)
SELECT convert( varchar(10),GETDATE(),110) AS Month1
create table #temp (ID int identity(1,1) not null primary key,
SomeData varchar(30),
Month1 AS convert( varchar(10),GETDATE(),110) )
Lowell
July 8, 2015 at 5:49 am
Hi
Thank you for your response.
I most probably didn't make myself clear, my apologies.
It should be something like the following:
Select 1 AS Month1: 07/07/2015 where the part after the AS keyword now becomes the column name.
----------------------|
Month1: 07/07/2015|
----------------------|
1 |
----------------------|
Kind regards
Fred
July 8, 2015 at 7:03 am
for that, where the column name is dynamic, i think you have to switch to dynamic SQL, wher eyou build a commadn and execute it instead.
a rough example:
DECLARE @cmd varchar(max) = 'Select 1 AS [Month1:' + convert( varchar(10),GETDATE(),110)+']' +',name from sys.tables'
exec(@cmd)
Lowell
July 8, 2015 at 7:08 am
Maybe I misunderstand but if you simply want 'Month1: 07/07/2015' as a column name did you try...
SELECT 1 AS [Month1: 07/07/2015]
Just a simple example but as you can see the Column name appears as (I understand) you want.
July 8, 2015 at 7:39 am
As mentioned above by Lowell you have to achieve the desired result executing dynamic SQL.
July 8, 2015 at 7:41 am
Hi
No, it should use the getdate function, so that I can advance it to the next month without hard coding the date
July 8, 2015 at 7:44 am
Ahhh...now I understand. Yes, you'll need to use Dynamic SQL for that.
July 8, 2015 at 8:00 am
Thanks. I will look into it
July 8, 2015 at 9:50 am
If you're using something else for the front-end, such as SSRS or any other reporting tool, you should format the label on that tool. It's a lot easier to maintain as dynamic sql can be easily abused and get over complicated.
July 9, 2015 at 12:15 am
Thanks Louis, I will see if there is something on SSIS/SSAS that I can use.
July 9, 2015 at 8:32 am
frdrckmitchell7 (7/9/2015)
Thanks Louis, I will see if there is something on SSIS/SSAS that I can use.
Using a column that might vary its name from one run to the other is painful for SSIS and SSAS as they both expect static column names. As time passes, you shouldn't add more columns, you should add more rows.
July 10, 2015 at 11:41 am
Luis Cazares (7/8/2015)
If you're using something else for the front-end, such as SSRS or any other reporting tool, you should format the label on that tool. It's a lot easier to maintain as dynamic sql can be easily abused and get over complicated.
+1
Don Simpson
Viewing 13 posts - 1 through 12 (of 12 total)
You must be logged in to reply to this topic. Login to reply