<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>question Re: How do I use Column Names of table in CASE Statement to assign Values and create another column in QUERY in Support Questions</title>
    <link>https://community.cloudera.com/t5/Support-Questions/How-do-I-use-Column-Names-of-table-in-CASE-Statement-to/m-p/336584#M232311</link>
    <description>&lt;P&gt;let table hwctest has 2 columns as below,&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;+--------------------------+------------+----------+
|         col_name         | data_type  | comment  |
+--------------------------+------------+----------+
| col1                     | string     |          |
| col2                     | string     |          |
| dt                       | date       |          |
|                          | NULL       | NULL     |
| # Partition Information  | NULL       | NULL     |
| # col_name               | data_type  | comment  |
| dt                       | date       |          |
+--------------------------+------------+----------+&lt;/LI-CODE&gt;&lt;P&gt;getting column name from metadata and stored in temp var,&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;beeline -u "jdbcurl" --showHeader=false --outputformat=csv2 -e "SELECT COLUMN_NAME FROM sys.COLUMNS_V2 c JOIN sys.TBLS a ON c.CD_ID=a.TBL_ID where a.TBL_NAME='hwctest'" | tr "\n" "," &amp;gt; tmp

a=$(cat tmp)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;Passing col names as variable,&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;beeline -u "jdbcurl" --hivevar colName=${a%?}
.....
0: jdbc:hive2://c2757-node3.coelab.cloudera.c&amp;gt; select case when array_contains(split('${colName}',','),'col1') then 1 else 0
. . . . . . . . . . . . . . . . . . . . . . .&amp;gt; end as type
.....
INFO  : Executing command(queryId=hive_20220218082727_46d036f4-7667-485a-a2c9-c5be158292d4): select case when array_contains(split('col1,col2',','),'col1') then 1 else 0
end as type
+-------+
| type  |
+-------+
| 1     |
+-------+&lt;/LI-CODE&gt;&lt;P&gt;replace the value col1 in your case.&lt;/P&gt;</description>
    <pubDate>Fri, 18 Feb 2022 09:16:23 GMT</pubDate>
    <dc:creator>marudhu_ramaiah</dc:creator>
    <dc:date>2022-02-18T09:16:23Z</dc:date>
    <item>
      <title>How do I use Column Names of table in CASE Statement to assign Values and create another column in QUERY</title>
      <link>https://community.cloudera.com/t5/Support-Questions/How-do-I-use-Column-Names-of-table-in-CASE-Statement-to/m-p/332977#M231327</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I did enough google before coming to this forum.&amp;nbsp; I have table&amp;nbsp; Employee with following column Names (there are 40 columns)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Employee_ID,&amp;nbsp; Employee_Type, Employee_Tenure&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to use these column names in CASE Statement as part of HIVE Query to assign value.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;E.g.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Case when &lt;EM&gt;&lt;STRONG&gt;Column Name&lt;/STRONG&gt; &lt;/EM&gt;= 'Employee_Type' then 1 else 0&lt;/P&gt;
&lt;P&gt;when&amp;nbsp;&lt;EM&gt;&lt;STRONG&gt;Column Name&lt;/STRONG&gt; &lt;/EM&gt;= 'Employee_Tenure' then 2 else 0&lt;/P&gt;
&lt;P&gt;end as Rank&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I know I can "&lt;EM&gt;&lt;STRONG&gt;show columns in Employee&lt;/STRONG&gt;&lt;/EM&gt;" to get all column names. But I don't know how to refer output of this query in CASE statement&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I also try to save results of Query into variable ( I could be wrong doing this )&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;set hivevar:col_names=(show columns in employee)&lt;/P&gt;
&lt;P&gt;select&amp;nbsp;'${hivevar:col_names}'&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;but I get below error :&amp;nbsp;&lt;EM&gt;Error while processing statement: Variable substitution depth is deeper than 40 for expression&lt;/EM&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any help appreciated. If my question is too naive or stupid - I apologize.&amp;nbsp; &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Apr 2026 08:02:28 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/How-do-I-use-Column-Names-of-table-in-CASE-Statement-to/m-p/332977#M231327</guid>
      <dc:creator>Tinu</dc:creator>
      <dc:date>2026-04-21T08:02:28Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use Column Names of table in CASE Statement to assign Values and create another column in QUERY</title>
      <link>https://community.cloudera.com/t5/Support-Questions/How-do-I-use-Column-Names-of-table-in-CASE-Statement-to/m-p/336584#M232311</link>
      <description>&lt;P&gt;let table hwctest has 2 columns as below,&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;+--------------------------+------------+----------+
|         col_name         | data_type  | comment  |
+--------------------------+------------+----------+
| col1                     | string     |          |
| col2                     | string     |          |
| dt                       | date       |          |
|                          | NULL       | NULL     |
| # Partition Information  | NULL       | NULL     |
| # col_name               | data_type  | comment  |
| dt                       | date       |          |
+--------------------------+------------+----------+&lt;/LI-CODE&gt;&lt;P&gt;getting column name from metadata and stored in temp var,&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;beeline -u "jdbcurl" --showHeader=false --outputformat=csv2 -e "SELECT COLUMN_NAME FROM sys.COLUMNS_V2 c JOIN sys.TBLS a ON c.CD_ID=a.TBL_ID where a.TBL_NAME='hwctest'" | tr "\n" "," &amp;gt; tmp

a=$(cat tmp)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;Passing col names as variable,&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;beeline -u "jdbcurl" --hivevar colName=${a%?}
.....
0: jdbc:hive2://c2757-node3.coelab.cloudera.c&amp;gt; select case when array_contains(split('${colName}',','),'col1') then 1 else 0
. . . . . . . . . . . . . . . . . . . . . . .&amp;gt; end as type
.....
INFO  : Executing command(queryId=hive_20220218082727_46d036f4-7667-485a-a2c9-c5be158292d4): select case when array_contains(split('col1,col2',','),'col1') then 1 else 0
end as type
+-------+
| type  |
+-------+
| 1     |
+-------+&lt;/LI-CODE&gt;&lt;P&gt;replace the value col1 in your case.&lt;/P&gt;</description>
      <pubDate>Fri, 18 Feb 2022 09:16:23 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/How-do-I-use-Column-Names-of-table-in-CASE-Statement-to/m-p/336584#M232311</guid>
      <dc:creator>marudhu_ramaiah</dc:creator>
      <dc:date>2022-02-18T09:16:23Z</dc:date>
    </item>
  </channel>
</rss>

