snowflake-cloud-data-platform

Get column name from a variable in snowflake


How to get a column name from a variable?

I was trying this but did not work

set a = 'aaa';
select 1 as $a ;

Solution

  • I suspect what you're really after is a way to select a specific column by storing that column name to a variable. If so...here's what that would look like. If not, let me know, I'll delete the answer

    create or replace temporary table t (col1 varchar);
    
    set col_name = 'col1';
    select identifier($col_name)
    from t;