Given this data:
col1
----
foo
bar
I want concatenate the rows together, and end up with 'foo','bar'.
Using collect_set gets me an array, concat_ws gets me a comma separated string.
select
concat_ws(',',collect_set(col1))
I cannot figure out how to get the single quotes in there.
concat('''',col1,'''') just returns the value of col1.
What am I doing wrong?
You need to escape the quotes.
concat('\'',col1,'\'')
Related
Using snowflake, I have a column named 'column_1'. The datatype is TEXT.
An example of a value in this column is here:
["Apple", "Pear","Chicken"]
I say:
select to_array(column_1) from fake_table; and I get:
[ "[\"Apple\",\"Pear\",\"Chicken\"]" ]
So it put my text into it. But I want to convert the datatype. Seems like it should be simple.
I try strtok_to_array(column_1, ',') and get the same situation.
How can snowflake convert strings to an array?
Using PARSE_JSON:
SELECT PARSE_JSON('["Apple", "Pear","Chicken"]')::ARRAY;
DESC RESULT LAST_QUERY_ID();
Output:
Since that's valid JSON, you can use the PARSE_JSON function:
select parse_json('["Apple", "Pear","Chicken"]');
select parse_json('["Apple", "Pear","Chicken"]')[0]; -- Get first one
select parse_json('["Apple", "Pear","Chicken"]')[0]::string; -- Cast to string
I'd say parse_json is the way to go, but if you're concerned some values might not be a valid json, you could get rid of the double quotes and square brackets and split the resulting comma separated string to array
select split(translate(col,$$"[]$$,''),',')
Note : Encapsulating in $$ makes escaping quotes and any other special character easier
I have string column "49b8b35e-b62c-4a42-9d73-192d131d127a,03c8a7e0-5153-11ec-873a-0242ac11000a,eec8aee4-0500-4940-b319-15924cc2d248"
this string column has 3 values separate by ",". (value1,value2,value3). there is no guarantees that vaule2 and value3 need to be present always. but value1 does always.
so i need to split this single column into 3 different columns keeping the above conditions of their existence in mind and they need to be separated by
before "," into a new column.
i have so far wrote as
select regexp_extract('49b8b35e-b62c-4a42-9d73-192d131d127a,03c8a7e0-5153-11ec-873a-0242ac11000a,eec8aee4-0500-4940-b319-15924cc2d248', '^(.+?),') as value1
but after that my logic and thinking giving errors and no luck to me.
You can use split() function, it returns array and you can access elements using array index []:
select split("49b8b35e-b62c-4a42-9d73-192d131d127a,03c8a7e0-5153-11ec-873a-0242ac11000a,eec8aee4-0500-4940-b319-15924cc2d248",',')[0] --to get first element
Use [0] for first element, [1] for second element and [2] for third, If some elements are absent and no commas (for example split() returned array of size=1 and you want 2nd element) it will return NULL, if commas exist but empty string in between them, it will return empty, so adjust your logic accordingly.
And if you prefer regexp_extract function:
regexp_extract('49b8b35e-b62c-4a42-9d73-192d131d127a,03c8a7e0-5153-11ec-873a-0242ac11000a,eec8aee4-0500-4940-b319-15924cc2d248', '^([^,]+),*([^,]*),*([^,]*)',1) as value1,
regexp_extract('49b8b35e-b62c-4a42-9d73-192d131d127a,03c8a7e0-5153-11ec-873a-0242ac11000a,eec8aee4-0500-4940-b319-15924cc2d248', '^([^,]+),*([^,]*),*([^,]*)',2) as value2,
regexp_extract('49b8b35e-b62c-4a42-9d73-192d131d127a,03c8a7e0-5153-11ec-873a-0242ac11000a,eec8aee4-0500-4940-b319-15924cc2d248', '^([^,]+),*([^,]*),*([^,]*)',3) as value3
The same pattern is used, only the group number is different. If element is absent, regexp_extract returns empty string.
I have a field in a database table in the format:
111_2222_33333,222_444_3,aaa_bbb_ccc
This is format is uniform to the entire field. Three underscore separated numeric values, a comma, three more underscore separated numeric values, another comma and then three underscore separated text values. No spaces in between
I want to extract the middle value from the second numeric sequence, in the example above I want to get 444
In a SQL query I inherited, the regex used is ^.,(\d+)_.$ but this doesn't seem to do anything.
I've tried to identify the first comma, first number after and the following underscore ,222_ to use as a starting point and from there get the next number without the _ after it
This (,\d*_)(\d+[^_]) selects ,222_444 and is the closest I've gotten
We can try using REGEXP_REPLACE with a capture group:
SELECT
REGEXP_REPLACE(
'111_2222_33333,222_444_3,aaa_bbb_ccc',
'^[^,]+,[^_]+_(.*?)_[^_]+,.*$',
'\1') AS num
FROM yourTable;
Here is a demo showing that the above regex' first capture group contains the quantity you want.
Demo
I am trying to identify a value that is nested in a string using Snowflakes regexp_substr()
The value that I want to access is in quotes:
...
Type:
value: "CategoryA"
...
Edit: This text is nested in a much larger portion of text.
I want to extract CategoryA for all columns using regexp_substr. But I am unsure how.
I have tried:
regexp_substr(col, 'Type\\W+(\\w+)\\W+\\w.+')
and while that gives the portion of the string, I just want what is in quotes and can't figure out how to do so.
You could use regexp_replace() instead:
regexp_replace(col, '(^[^"]*")|("[^"]*$)", '')
The regexp matches on both following conditions, and replaces matching parts with the empty string:
^[^"]*": everything from the beginning of the string to the first double quote
("[^"]*$)": everything from the last double quote to the end of the string
I have 10 columns and their values could be either null, or a name of a fruit.
I would like to add another column with all the fruits that every row has. I have used Concat(column1 , column2,..., column10) as name.
Issue : There are no commas coming on the result and if I add the comma before concatenating, we are having them together, the last word is also a comma.
Any ideas?
Thanks!
You can use the standard concatenation (||) in conjunciton with COALESCE function, which returns the value of the first non-null argument.
Example:
select coalesce(column1||',', '')||coalesce(column2||',', '')|| ... ||coalesce(column10||, '');