I have a column of type array<bigint> (say of value [1,2,3,4]), and I want to convert it to string (say "1,2,3,4"), how can I do that?
I tried concat_ws(',' arr), but it complains
Argument 2 of function CONCAT_WS must be "string or array<string>", but "array<bigint>" was found."
Is there a way to cast array<bigint> to array<string>?
Try this:
select xx,concat_ws(',',collect_set(cast(element as string))) as arrystr
from table
lateral view explode(arr) b as element
group by xx
try this:
select
regexp_replace(string_with_brackets,'\\[|\\]','') as final_str
from
(
select
transform(array_of_int) using '/bin/cat' as (string_with_brackets)
)
Related
I want to extract a column A that has values such as W:X:Y:Z.
I am interested to extract Z from Column A.
I tried multiple commands such as SPLIT(Table.A, "[:]"[3] ) but get an error.
What is the best way to do this?
Split function returns array. Array index [3] should be applied to the split function result:
with yourtable as ( -- use your table instead of this
select 'W:X:Y:Z' as A
)
select split(A,'\\:')[3] from yourtable;
Result:
Z
A field is defined to be array(varchar) in modeanlyatic.
I want to search for records with field contains a certain text pattern, says 'ABCD'.
If I run this SQL:
select * from data_table
where top_results like '%ABCD%'
It throws this error:
Query failed (#20190730_021145_23663_dn9fj): line 2:7: Left side of
LIKE expression must evaluate to a varchar (actual: array(varchar)
What is the correct syntax to detect the presence of a certain string?
Use filter(array(T), function(T, boolean)) -> array(T)
it returns array containing elements for which function returns true. Use cardinality function to check if array is not empty:
SELECT cardinality(filter(ARRAY ['123ABCD456', 'DQF', 'ABCD', 'ABC'], x -> x like '%ABCD%'))>0 ;
Returns
true
Check another value:
SELECT cardinality(filter(ARRAY ['123ABCD456', 'DQF', 'ABCD', 'ABC'], x -> x like '%XXX%'))>0 ;
Returns
false
Hope you got the idea.
Convert it to a json string and then search the string
json_format(cast(top_results as JSON))
select * from data_table
where json_format(cast(top_results as JSON)) like '%ABCD%'
I would use any_match for that:
SELECT *
FROM data_table
WHERE any_match(top_results, s -> s like '%ABCD%')
I'm trying to run a query where I combine two columns and separate them with an x in between.
I'm also trying to get some other columns from the same table. However, I get the following error.
Error: No matching signature for function CONCAT for argument types: FLOAT64, FLOAT64. Supported signatures: CONCAT(STRING, [STRING, ...]); CONCAT(BYTES, [BYTES, ...]).
Here is my code:
SELECT
CONCAT(right,'x',left),
position,
numbercreated,
Madefrom
FROM
table
WHERE
Date = "2018-10-07%"
I have tried also putting a cast before but that did not work.
SELECT Concast(cast(right,'x',left)), position,...
SELECT Concast(cast(right,'x',left)as STRING), position,...
Why am I getting this error?
Are there any fixes?
Thanks for the help.
You need to cast each value before the concat():
SELECT CONCAT(CAST(right as string), 'x', CAST(left as string)),
position, numbercreated, Madefrom
FROM table
WHERE Date = '2018-10-07%';
If you want a particular format, then use the FORMAT() function.
I also doubt that your WHERE will match anything. If Date is a string, then you probably want LIKE:
WHERE Date LIKE '2018-10-07%';
More likely, you should use the DATE function or direct comparison:
WHERE DATE(Date) = '2018-10-07'
or:
WHERE Date >= '2018-10-07' AND
Date < '2018-10-08'
Another option to fix your issue with CONCAT is to use FROMAT function as in below example
#standardSQL
WITH `project.dataset.table` AS (
SELECT 1.01 AS `right`, 2.0 AS `left`
)
SELECT FORMAT('%g%s%g', t.right, 'x', t.left)
FROM `project.dataset.table` t
result will be
Row f0_
1 1.01x2
Note: in above specific example - you could use even simpler statement
FORMAT('%gx%g', t.right, t.left)
You can see more for supporting formats
Few recommendations - try not to use keywords as a column names/aliases. If for some reason you do use - wrap such with backtick or prefix it with table name/alias
Yet another comment - looks like you switched your values positions - your right one is on left side and left one is on right - might be exactly what you need but wanted to mention
Try like below by using safe_cast:
SELECT
CONCAT(SAFE_CAST( right as string ),'x',SAFE_CAST(left as string)),
position,
numbercreated,
Madefrom
FROM
table
WHERE
Date = '2018-10-07'
I am trying to convert an array of big ints into string array in hive SQL
I've tried using
concat_ws
but that gave me an error
Argument 2 of function CONCAT_WS must be "string or array<string>", but "array<bigint>" was found.
I tried using
transform(mycolumn) using '/bin/cat' as mycolumn_as_string
It gives an error of
cannot recognize input near 'transform' '(' 'mycolumn' in selection target
How can I convert this bigint array into an array string?
Assuming that your table looks like this:
tb_name mycolumn
------- --------
tblname [111,222,333]
tblname [1234,123456,3423]
Then below query will not work because concat_ws only accepts string or array of string:
select tb_name, concat_ws('-', mycolumn) from mytable;
FAILED: Argument 2 of function CONCAT_WS must be "string or array<string>", but "array<int>" was found.
Based on this SO: How to concatenate the elemets of int array to string in Hive
Incorrect:
select concat_ws('-', transform(mycolumn) using '/bin/cat') as mycolumnstr from mytable;
FAILED: ParseException line 1:22 cannot recognize input near 'transform' '(' 'mycolumn' in select expression
Correct:
with tbl as (select transform(mycolumn, tb_name) using '/bin/cat' as (mycolumnstr, tb_name) from mytable)
select concat_ws('-', tb_name, mycolumnstr) from tbl;
Result:
tblname-[111,222,333]
tblname-[1234,123456,3423]
I have a table with two columns, POLIZA and TEXTO_LIMPIO.
In the TEXTO_LIMPIO column is a large string and I want to replace some characters.
I was doing it this way:
SELECT REPLACE(TEXTO_LIMPIO,' ','Ø') AS P
FROM M_POL
WHERE POLIZA = '6.015.883'
But I get this error:
The text data type of the argument is not valid for argument 1 of the
replace function.
You cannot use the REPLACE-function on columns with text-datatype.
But you can convert it first and then replace:
SELECT REPLACE(CONVERT(varchar(max), TEXTO_LIMPIO),' ','Ø') AS P
FROM M_POL
WHERE POLIZA = '6.015.883'