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]
Related
I am working with a database with key-value store, and the data is in a form of an byte array.
I have used this example to decode a byte array
select convert_from(decode('121412432d3134323932342e3132325a', 'hex'), 'UTF8');
Which works fine.
But when I try to get the values to decode with a Select statement:
select convert_from(decode(select columnvalue from stats limit 1), 'hex'), 'UTF8');
I am getting
No function matches the given name and argument types. You might need to add explicit type casts.
From what I understand, the decode function expects a byte array, but from the select statement is getting a String.
Is that assumption correct?
How would I go resolving it?
I have tried to encode the result, and then decode it, but I am getting the same problem.
SELECT convert_from(decode(encode(convert_to((select columnvalue from stats limit 1),'UTF-8'),'base64'),'hex'),'UTF8');
Full query with error:
SELECT convert_from(decode(encode(convert_to((select columnvalue from stats limit 1),'UTF8'),'base64'),'hex'),'UTF8');
ERROR: function convert_to(bytea, unknown) does not exist
LINE 1: SELECT convert_from(decode(encode(convert_to((select columnv...
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
you have to convert the bytea to character varying and replace the \x with '' after that you will get
create table dobytea(columnvalue bytea);
insert into dobytea values (decode('121412432d3134323932342e3132325a', 'hex'));
select convert_from(decode('121412432d3134323932342e3132325a', 'hex'), 'UTF8');
select columnvalue from dobytea limit 1;
select convert_from(decode(
(select replace (columnvalue::CHARACTER VARYINg,'\x','') from dobytea limit 1)
, 'hex'), 'UTF8') as conv;
OUTPUT :
convert_from
C-142924.122Z
columnvalue
\x121412432d3134323932342e3132325a
conv
C-142924.122Z
I'm using try_cast in snowflake to convert any long values in sql to NULL.
Here is my code:
When I try running the above code, I'm getting the error as below:
I'm flattening a JSON array and using try_cast to make any large values to NULL because I was getting an error Failed to cast variant value {numberLong: -8301085358432}
SELECT try_cast(item.value:price) as item_price,
try_cast(item.value:total_price_bill) as items_total_price
FROM table, LATERAL FLATTEN(input => products) item
Error:
SQL compilation error error at line 1 at position ')'.
I don't understand where I'm doing wrong
you are using wrong syntax for try_cast. according to snowflake documentations the syntax is :
TRY_CAST( <source_string_expr> AS <target_data_type> )
and also note:
Only works for string expressions.
target_data_type must be one of the following:
VARCHAR (or any of its synonyms)
NUMBER (or any of its synonyms)
DOUBLE
BOOLEAN
DATE
TIME
TIMESTAMP, TIMESTAMP_LTZ, TIMESTAMP_NTZ, or TIMESTAMP_TZ
so for example you have to have something like this if item.value:price is string:
select try_cast(item.value:price as NUMBER) as item_price,
....
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 get a substring from the value of a column and I'm getting the following error Argument data type varchar is invalid for argument 2 of substring function.
The column type is NvarChar(50) and is a system column for an application, so I can't modify it.
Ideally I'd just be able to select the substring as part of the query without having to alter the table, or create a view or another table.
Here's my query
SELECT SUBSTRING(INVOICE__, ':', 1)
FROM dwsystem.dbo.DWGroup
Im trying to select only everything in the string after a specific character. In this case the : character.
Use charindex with : as the first argument
select substring(invoice__,charindex(':',invoice__)+1,len(invoice__))
from dwsystem.dbo.dwgroup
SUBSTRING parameter is start position and end position so both parameter will be number like below
SELECT SUBSTRING(INVOICE__, 1, 1)
FROM dwsystem.dbo.DWGroup
you can use SUBSTRING_INDEX as you used mysql
SELECT SUBSTRING_INDEX(INVOICE__,':',-1);
example
SELECT SUBSTRING_INDEX('mytestpage:info',':',-1); it will return
info
I am trying to get the first part of a Guid Field with the TSQL substring function as follows
SELECT SUBSTRING(Guid, 1, 8) AS Gu FROM MyTable
but all i get is this error.
Argument data type uniqueidentifier is invalid for argument 1 of substring function.
So what is going on here? Should i treat the Guid as pure string first or...?
Thanks in advance!
Try this:
SELECT SUBSTRING(CAST(Guid AS varchar(38)), 1, 8) AS Gu FROM MyTable
You can't perform SUBSTRING directly on a uniqueidentifier; you need to cast it to a string type (varchar) first.