Snowflake; convert strings to an array - sql

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

Related

Remove template text on regexp_replace in Oracle's SQL

I am trying to remove template text like &#x; or &#xx; or &#xxx; from long string
Note: x / xx / xxx - is number, The length of the number is unknown, The cell type is CLOB
for example:
SELECT 'H'ello wor±ld' FROM dual
A desirable result:
Hello world
I know that regexp_replace should be used, But how do you use this function to remove this text?
You can use
SELECT REGEXP_REPLACE(col,'&&#\d+;')
FROM t
where
& is put twice to provide escaping for the substitution character
\d represents digits and the following + provides the multiple occurrences of them
ending the pattern with ;
or just use a single ampersand ('&#\d+;') for the pattern as in the case of Demo , since an ampersand has a special meaning for Oracle, a usage is a bit problematic.
In case you wanted to remove the entities because you don't know how to replace them by their character values, here is a solution:
UTL_I18N.UNESCAPE_REFERENCE( xmlquery( 'the_double_quoted_original_string' RETURNING content).getStringVal() )
In other words, the original 'H'ello wor±ld' should be passed to XMLQUERY as '"H'ello wor±ld"'.
And the result will be 'H'ello wo±ld'

Keep substring that precedes an expression in SQLite

I want to split a varchar column on a certain expression and keep the left hand side of the result.
My column looks as follows:
varchar_col
keep_this__discard_this
keep_this_too__discard_this
I want to split all the strings on the double underscore ('__') and keep whatever comes before it. How can this be done in SQLite?
You can use:
select substr(varchar_col, 1, instr(varchar_col, '__') - 1)
Here is a db<>fiddle.

Cut string after first occurrence of a character

I have strings like 'keepme:cutme' or 'string-without-separator' which should become respectively 'keepme' and 'string-without-separator'. Can this be done in PostgreSQL? I tried:
select substring('first:last' from '.+:')
But this leaves the : in and won't work if there is no : in the string.
Use split_part():
SELECT split_part('first:last', ':', 1) AS first_part
Returns the whole string if the delimiter is not there. And it's simple to get the 2nd or 3rd part etc.
Substantially faster than functions using regular expression matching. And since we have a fixed delimiter we don't need the magic of regular expressions.
Related:
Split comma separated column data into additional columns
regexp_replace() may be overload for what you need, but it also gives the additional benefit of regex. For instance, if strings use multiple delimiters.
Example use:
select regexp_replace( 'first:last', E':.*', '');
SQL Select to pick everything after the last occurrence of a character
select right('first:last', charindex(':', reverse('first:last')) - 1)

Concatenate strings in Oracle SQL without a space in between?

I am trying to concatenate strings in oracle.
The following is my query:
insert into dummy values('c'||to_char(10000,'99999'));
The expected result is:
c10000
But the output I get is with a space in between 'c' and the value 10000:
c 10000
How to concat without spaces?
This is not an issue with the concatenation operator but with the function to_char(). Try instead:
to_char(10000,'FM99999')
I quote the manual here:
FM ..
Returns a value with no leading or trailing blanks.
There are two solutions:
Fill Mode ('FM') formatting prefix that suppresses the additional blank character prefix for the to_char number conversion. I suggest this one is preferred, because it is integrated with the to_char format and does not require an additional function call;
LTRIM of the returned value from the to_char number conversion.
The code below shows the results of both solutions:
Select concat('NTA', to_char(1,'FM0000000000000')),
concat('NTA', ltrim(to_char(1,'0000000000000'))),
concat('NTA', to_char(1,'0000000000000'))
from dual;
"CONCAT('NTA',TO_CHAR(1,'FM0000000000000'))": "NTA0000000000001"
"CONCAT('NTA',LTRIM(TO_CHAR(1,'0000000000000')))": "NTA0000000000001"
"CONCAT('NTA',TO_CHAR(1,'0000000000000'))": "NTA 0000000000001"

Can anyone help me write a sql query

jkdfhdjfhjh&name=ijkjkjkjkjkjk&id=kdjkjkjkjkjkjjjd&class=kdfjjfjdhfjhf
The above string has some characters starting with & and ending with =
for example we have &name= and I just need this from the above string.
similarly I need &id=, &class=
I need the output under a single column.
Final Extract
----------------------
&id=, &class=, &name=
can anyone help me out in writing a query for this.
You could try this :
select regexp_replace('jkdfhdjfhjh&name=ijkjkjkjkjkjk&id=kdjkjkjkjkjkjjjd&class=kdfjjfjdhfjhf', '\\w*?(&.*?=)\\w+((?=&)|$)', '\\1, ', 'g');
result:
regexp_replace
-------------------------
&name=, &id=, &class=,
Then it's up to you to remove the last ,.
The regexp_replace function is available in version 8.1 and after.
If you want the values along with each variable, I would implement this by splitting on "&" into an array and then taking a slice of the desired elements:
SELECT (string_to_array('jkdfhdjfhjh&name=ijkjkjkjkjkjk&id=kdjkjkjkjkjkjjjd&class=kdfjjfjdhfjhf','&'))[2:4];
Output in PostgreSQL 8.4 (array type):
{name=ijkjkjkjkjkjk,id=kdjkjkjkjkjkjjjd,class=kdfjjfjdhfjhf}
The example string is very wide so here's the general form to show the array slicing more clearly:
SELECT ((string_to_array(input_field,'&'))[2:4];
NOTE: You must have the extra parentheses around the string_to_array() call in order for the array slicing to work--you'll get an error otherwise.