Select column from table that column name contains specific keyword

Copper Contributor

Hi,

 

I need help on writing SQL query to select only column names that contains specific year. For ex , I have a table with 100+ columns where I have 4 columns for each quarter for every year, like below

 

Select value_2022q1, value_2022q2, value_2022q3, value_2022q4, value_2023q1, value_2023q2, value_2023q3, value_2023q4, 

... And so on till 2030 year. 

 

I would like to show only column names that has 2023 or 2024 based on my need. How to write SQL commands to achieve this scenario.

 

Could you please help me writing this query.. thanks

 

Regards,

Fazlu

 

 

1 Reply
It's not possible with a simple SQL statement, you would have to use dynamic SQL, see https://www.sommarskog.se/dynamic_sql.html