January 13, 2023 at 10:55 am
Hello all. I hope everyone's ok
I have this query:
SELECT cont, type, month01, month02, month03...
FROM acum
Where year=2022
I'd like that for each month it shows a text and the year, like Jan-2022, Feb-2022 and so on.
Something like
SELECT cont, type, month01 AS CONCAT( 'Jan-',year) , month02 AS CONCAT( 'Feb-',year) ...
FROM acum
Where year=2022
I tried with concat, with the '+' but it didn't work.
Can any of you help?
Thanks in advance
January 13, 2023 at 11:19 am
You'll have to use dynamic SQL for this.
😎
January 13, 2023 at 11:33 am
I'm sorry. I don't even know what that means. Can you give an example?
What I want is a column name with text and field name like my example above
January 13, 2023 at 12:11 pm
Here is some dynamic SQL to give you an idea. You cannot 'inject' variables into column aliases using 'normal' T-SQL.
DECLARE @Yr SMALLINT = 2022;
DECLARE @Sql VARCHAR(MAX)
= 'SELECT cont, type, month01 AS [Jan-@year] , month02 AS [Feb-@year] FROM acum Where year = @year';
SET @Sql = REPLACE (@Sql, '@year', @Yr);
EXEC(@Sql);
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
January 13, 2023 at 12:15 pm
Thank you so much for your help Phil
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply