Convert String to array and validate size on Vertica
NickName:Hard Worker Ask DateTime:2019-03-03T18:10:47

Convert String to array and validate size on Vertica

I need to execute a SQL query, which converts a String column to a Array and then validate the size of that array

I was able to do it easily with postgresql: e.g.

select
cardinality(string_to_array('a$b','$')),
cardinality(string_to_array('a$b$','$')),
cardinality(string_to_array('a$b$$$$$','$')),

But for some reason trying to convert String on vertica to array is not that simple, Saw this links:

https://www.vertica.com/blog/vertica-quick-tip-dynamically-split-string/ https://forum.vertica.com/discussion/239031/how-to-create-an-array-in-vertica

And much more that non of them helped.

I also tried using:

select  REGEXP_COUNT('a$b$$$$$','$')

But i get an incorrect value - 1.

How can i Convert String to array on Vertica and gets his Length ?

Copyright Notice:Content Author:「Hard Worker」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/54967721/convert-string-to-array-and-validate-size-on-vertica

Answers
Gordon Linoff 2019-03-03T12:04:53

$ has a special meaning in a regular expression. It represents the end of the string.\n\nTry escaping it:\n\nselect REGEXP_COUNT('a$b$$$$$', '[$]')\n",


A. Saunders 2019-03-05T17:29:35

You could create a UDx scalar function (UDSF) in Java, C++, R or Python. The input would be a string and the output would be an integer. https://www.vertica.com/docs/9.2.x/HTML/Content/Authoring/ExtendingVertica/UDx/ScalarFunctions/ScalarFunctions.htm\n\nThis will allow you to use language specific array logic on the strings passed in. For example in python, you could include this logic:\n\ninput_list = input.split(\"$\")\nfiltered_input_list = list(filter(None, input_list))\nlist_count = len(filtered_input_list)\n\n\nThese examples are a good starting point for writing UDx's for Vertica. https://github.com/vertica/UDx-Examples",


More about “Convert String to array and validate size on Vertica” related questions

Convert String to array and validate size on Vertica

I need to execute a SQL query, which converts a String column to a Array and then validate the size of that array I was able to do it easily with postgresql: e.g. select cardinality(string_to_arr...

Show Detail

Convert String to comma separated single quote values in vertica database

I have data like this "1,2,3,4" (String). I need to convert this to ('1','2','3','4'), so that i can use like select * from table where ids in ('1','2','3','4').

Show Detail

Fail to convert var-char array to int array in vertica SQL

when trying to convert varchar array to an integer array like that: cast(array['1','2','3'] as int[]) the query works just fine- receiving [1,2,3] but when trying to use the following query I'm get...

Show Detail

Vertica: Convert Bitstrint to integer

Is there a simple way to convert a binary string to an integer using vertica sql? something like this: => SELECT bitstring_to_int('11') as temp temp --- 3 I know there is bitstring_to_binar...

Show Detail

How to convert c string into Vstring vertica?

I'm writing a user define function for Vertica and don't know how to initialize a Vstring of Vertica from a C string (char *) . Thanks for any suggestion?

Show Detail

how to convert a timestamp to int in sql (vertica)

I have a timestamp as 2017-07-19 11:45:01and i want it to convert to int. Query: select cast(max(event_timestamp) as INT) from error_messages where error_level='ERROR' and user_name='git' Error:...

Show Detail

Vertica convert date format

In MSSQL we can convert the date format into the format we wanted. for example : convert(char(10),column_name , 120) in vertica database can we do the same? SELECT CONVERT(CHAR(10),CURRENT_TIMESTA...

Show Detail

does anyone know the format of an odbc connection string for vertica?

I'm using the following: DRIVER={Vertica ODBC Driver 4.1}; SERVER=lnxtabdb01.xxxx.com; PORT=5433; DATABASE=vertica; USER=dbadmin; PASSWORD=vertica; OPTION=3; i'm getting this error and I just wan...

Show Detail

How to convert address in IPv6 to number using Vertica

I have a table in Vertica db with column ip of type string that holds both formats of ipv4 and ipv6. I need to transform the string representation of the ip to a number as it is done for example he...

Show Detail

How to convert Hive array<Int> type to Vertica varchar on exporting data

I have a table in Hive db with array type column which I want to copy it with content to Vertica db. The Version of Vertica db is v9.0.1-0 and I cant create table with array type. To copy table I t...

Show Detail