I want to convert an array to string in hive. I want to collect_set array values to convert to string without [[""]].
select actor, collect_set(date) as grpdate from actor_table group by actor;
so that [["2016-07-01", "2016-07-02"]] would become 2016-07-01, 2016-07-02
Use concat_ws(string delimiter, array<string>) function to concatenate array:
select actor, concat_ws(',',collect_set(date)) as grpdate from actor_table group by actor;
If the date field is not string, then convert it to string:
concat_ws(',',collect_set(cast(date as string)))
Read also this answer about alternative ways if you already have an array (of int) and do not want to explode it to convert element type to string: How to concatenate the elements of int array to string in Hive
Sometimes, you may need a JSON formatted list, so you can simply use:
SELECT CAST(COLLECT_SET(date) AS STRING) AS dates FROM actor_table
PS: I needed this but found only your question about array to string conversion.
Related
I have string column in my hive table and I would the output to be in the following way:
Column A Column B
ddjj3332 jjn32212 334334 (3332, 32212, 334334)
I have tried to do regex replace in the string to remove the alphabets. But I am unable to convert it into an array.
Let me know if this is possible.
You can use split to make an array after you perform the replace:
split(regexp_replace(column_a, '[^0-9 ]', ''), ' ')
I am concatenating but it is showing error
UPPER(SUBSTRING( [a],1,1)) + FINDSTRING( [a]," ",1)
The [a] value looks like:
snoffy ab
FINDSTRING() Function will return a DT_I4 value which cannot be concateneted with a string, just cast it:
UPPER(SUBSTRING([a],1,1)) + (DT_WSTR,10)FINDSTRING([a]," ",1)
You have to pass the 1st list from your array value.
Array of string into a variable in SSIS
I have 2 arrays. Array 1 is a string array that contains the names of types.
So the contents of Array 1 looks like this:
["nvarchar";"nvarchar";"date";"nvarchar"] //these are all strings
Array 2 is another array of strings and the contents look like this:
["Jackson";"Sentzke";"1991-04-19T00:00:00";"Jackson Sentske"]
My problem is that all the values in Array 2 are strings, and I want them to be of the types in Array 1. Is there a way that I can type cast the strings in Array 2 using the strings in Array 1?
As #s952163 points out, it looks like you're attempting to read data from a database, in which case there are better options available than trying to do it yourself. Still, just to sketch out a possible solution if taking the OP at face value, here's one way to go about it.
Since the types listed aren't .NET types, perhaps it's better to define a custom type to hold such values:
open System
type DbType = NVarChar of string | DT of DateTime
You can add more cases to DbType if you'd like.
Using active patterns, you can write a function to convert a single candidate:
// string * string -> string option
let (|NVarChar|_|) = function
| "nvarchar", (x : string) -> Some x
| _ -> None
// string * string -> DateTime option
let (|DT|_|) (typeHint, value) =
match (typeHint, DateTime.TryParse value) with
| "date", (true, dt) -> Some dt
| _ -> None
// string * string -> DbType option
let convertPair = function
| NVarChar x -> Some (NVarChar x)
| DT x -> Some (DT x)
| _ -> None
The use of active patterns isn't strictly necessary, but I thought it enabled me to decompose the problem in a good way.
Now you can declare a list of types and one of values, zip them together to get a list of interpreted values:
> let types = ["nvarchar"; "nvarchar"; "date"; "nvarchar"];;
val types : string list = ["nvarchar"; "nvarchar"; "date"; "nvarchar"]
> let values = ["Jackson"; "Sentzke"; "1991-04-19T00:00:00"; "Jackson Sentske"];;
val values : string list =
["Jackson"; "Sentzke"; "1991-04-19T00:00:00"; "Jackson Sentske"]
> let converted = List.zip types values |> List.choose convertPair;;
val converted : DbType list =
[NVarChar "Jackson"; NVarChar "Sentzke"; DT 19.04.1991 00:00:00;
NVarChar "Jackson Sentske"]
Note that both types and values have the type string list. In the OP, they were (string * string * string * string) list, which I assume was a mistake.
Actually you have a few issues here.
Commas create Tuples
your code
["nvarchar","nvarchar","date","nvarchar"]
is not a string list but (string * string * string * string) list
If you want to create a string list you would have to use a semicolon ;.
Now if you know that your code has always the same exact number of tuple elements with the exact types you want than you could do it like this
type RowItems = string * string * DateTime * string
type Rows = RowItem list
Now if your problem includes rows of different sizes and you want convert from or to SQL data types than maybe an approach using Discriminate Unions could be helpful.
type SQLDataType =
| VarChar of string
| Date of DateTime
type Row = SQLDataType list
type Rows = RowItem list
The usage then would be like this
let row =
[
VarChar "Jackson";
VarChar "Sentzke";
Date DateTime.parse("1991-04-19T00:00:00");
VarChar "Jackson Sentske"
]
finally you should write then some conversion functions that convert that from and to your SQL data
let fromSQL (s:string) : SQLDataType = ...
let toSQL (s:SQLDataType) : string = ...
I hope this helps.
This is a bit of a low-level solution as it's not very obvious where all this is headed, but if all that is you need is for your strings to be of the db types, then T-SQL has Cast and Convert and also this for you. So you can just say CAST ("1991-04-19T00:00:00" AS date).
I need to validate in my query if the value of a string (the first part) is equal to a definited value, for instance:
String
----------
F11-EDEDED
F1-SAFSDA
F455-ADADD
F11-ASDA-FAFA
And validate when the string is F11, i was searching something like split on vba, but i can't find it.
Im working with :
Case when ("splitted string") =F11 then X)
Use a Left() and Charindex() to grab the beginning of your strings.
Declare #str varchar(100)='F11-ASDA-FAFA'
Select #str,Case When left(#str,charindex('-',#str)-1)='F11' Then 1 Else 0 End
I have some sql function that returns character varying type. The output is something like this:
'TTFFFFNN'. I need to get this characters by index. How to convert character varying to array?
Use string_to_array() with NULL as delimiter (pg 9.1+):
SELECT string_to_array('TTFFFFNN'::text, NULL) AS arr;
Per documentation:
In string_to_array, if the delimiter parameter is NULL, each character
in the input string will become a separate element in the resulting array.
In older versions (pg 9.0-), the call with NULL returned NULL. (Fiddle.)
To get the 2nd position (example):
SELECT (string_to_array('TTFFFFNN'::text, NULL))[2] AS item2;
Alternatives
For single characters I would use substring() directly, like #a_horse commented:
SELECT substring('TTFFFFNN'::text, 2, 1) AS item2;
SQL Fiddle showing both.
For strings with actual delimiters, I suggest split_part():
Split comma separated column data into additional columns
Only use regexp_split_to_array() if you must. Regular expression processing is more expensive.
In addition to the solutions outlined by Erwin and a horse, you can use regexp_split_to_array() with an empty regexp instead:
select regexp_split_to_array('TTFFFFNN'::text, '');
With an index, that becomes:
select (regexp_split_to_array('TTFFFFNN'::text, ''))[2];