0

I'm trying to lateral flatten a varchar to an array to explode the element within the array

For example [trJGrODW4n4iLRN2A4WeraV740vH4L81ikd7KnRfsD bE578vR4L81isq7YnRchM]

I tried the following:

  1. I tried to replace the varchar space with ," (doesn't work).
  2. Used strtok_to_array --> gets unicode start of string
 Select s.value, replace(varchar_col,' ',',"') as to_array_format,
strtok_to_array(varchar_col,'')as strtok_to_array,* from table , lateral flatten(input => strtok_to_array(varchar_col,''))s

Actual result:

1. trJGrODW4n4iLRN2A4WeraV740vH4L81ikd7KnRfsD bE578vR4L81isq7YnRchM
2. [ "trJGrODW4n4iLRN2A4Wer\u0002aV740vH4L81ikd7KnRfsD\u0002bE578vR4L81isq7YnRchM" ]

Expected Result:

1. ["trJGrODW4n4iLRN2A4Wer","aV740vH4L81ikd7KnRfsD","bE578vR4L81isq7YnRchM"]
2. ["trJGrODW4n4iLRN2A4Wer","aV740vH4L81ikd7KnRfsD","bE578vR4L81isq7YnRchM"]

1 Answer 1

1

From the output you're getting, it looks like your data contains null bytes. If you want to split the string into an array using the null bytes as the boundary, you can use

strtok_to_array(varchar_col,hex_decode_string('00'))

hex_decode_string takes a hex-encoded string as input, and passing '00' returns a null byte, which we then use as the separator.

2
  • Thanks for the response, that still dint resolve the issue it still gets me [ "trJGrODW4n4iLRN2A4Wer\u0002aV740vH4L81ikd7KnRfsD\u0002bE578vR4L81isq7YnRchM" ] Commented Oct 12, 2019 at 17:00
  • 2
    the \u0002 is telling you the "value" you "don't want" is 0002 which suggests to me strtok_to_array(varchar_col,hex_decode_string('02')) should cover you use case. Commented Oct 13, 2019 at 6:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.