While I am reading from the Snowflake DB, I need to read columns and values for Varchar fields. Something like the following: select varchar_col1, varchar_col2, numeric_col3 from my_table
varchar_col1 varchar_col2 numeric_col3
test_value1 test_value2 99999
I want to output something like following:
"varchar_col1" "varchar_col2" numeric_col3
"test_value1" "test_value2" 99999
Is it possible to achieve something like this? I tried various ways but was unable to do so. For example, I tried to use this Snowflake: SELECT "COLUMN" with double quotes.
CodePudding user response:
this may work for you.
select '"' || varchar_col1 as varchar_col1 , '"' || varchar_col2 || '"' as varchar_col2, '"' || numeric_col3 || '"' as numeric_col3
from (
select column1 as varchar_col1, column2 as varchar_col2, column3 as numeric_col3 from values
('test_value1', 'test_value2',99999)
);
CodePudding user response:
You can convert the varchar to a variant to add double quotes, or you can concatenate the double quotes with the column values as Himanshu showed.
As I understand, you also want column names in double quotes. For this, you need to surround the column name with double quotes while escaping the double quotes belonging to the name:
varchar_col1 -> """varchar_col1"""
select varchar_col1::variant """varchar_col1""", '"' || varchar_col2 || '"' """varchar_col2""", numeric_col3 "numeric_col3"
from values
('test_value1', 'test_value2',99999) t(varchar_col1, varchar_col2, numeric_col3);
---------------- ---------------- --------------
| "varchar_col1" | "varchar_col2" | numeric_col3 |
---------------- ---------------- --------------
| "test_value1" | "test_value2" | 99999 |
---------------- ---------------- --------------