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 assigning that column name to a variable.

    create or replace temporary table t (col1 varchar); --example table
    

    set col_name = 'col1'; --assign column name to a variable
    
    select identifier($col_name) --reference it using identifier()
    from t;