Skip to main content

Concat more than two columns or values

  • July 22, 2014
  • 5 replies
  • 9 views

Adrian_1
Hello,

in the SQL documentation you write that the concat-statement is able to concatenate two OR MORE columns. Unfortunately, it does not work on "my" Vertica.

The following statement creates an error:
select concat('a','b','c') from a_test;

Error message: SQL Error [3457] [42883]: [Vertica][VJDBC](3457) ERROR: Function concat(unknown, unknown, unknown) does not exist, or permission is denied for concat(unknown, unknown, unknown)

This works fine:
select concat('a','b') from a_test;

(The test of the concatenation of different columns created to same errors)

Using Vertica 7. I'm able to nest the concats but if I want to nest ~200 columns, this will be very, very complicated.

Additional question: Is there anything equal to concat_ws() for vertica?

Thank you for your reply!

5 replies

Siddarth
Forum|alt.badge.img+1
  • Participating Frequently
  • July 22, 2014
You can concatenate columns as shown below instead of using concat function.

SELECT a||b||c from test;

Adrian_1
  • Author
  • New Participant
  • July 22, 2014
Works great, thanks!

Navin_C
Forum|alt.badge.img+2
  • Participating Frequently
  • July 23, 2014

Hi Adrian,

Siddharth gave the best and easiest approach to concat the columns

one more alternative for this use case
1. Using nested concats
nnani=> select concat(concat('a','b'),'c');
 concat
--------
 abc
(1 row)
The other question by you was regarding concat_ws function, This function does not currently exist in Vertica.
I tried to mock the function using SQL UDF, this might help you, but this is again limited to only two columns.
create function CONCAT_WS(sep varchar, column1 varchar, column2 varchar)      return varchar
as
begin
return 
case when sep IS NULL then 'No seperator provided'
     when sep IS NOT NULL then CONCAT(CONCAT(column1,sep),column2)
     else 'NULL' 
END;
end;
Examples:
nnani=>SELECT CONCAT_WS(' ','a=apple','b=banana');
    CONCAT_WS
------------------
 a=apple b=banana
(1 row)

nnani=>SELECT CONCAT_WS('&','a=apple','b=banana');
    CONCAT_WS
------------------
 a=apple&b=banana
(1 row)
Hope this helps.

ahaldar1106
  • New Participant
  • July 14, 2020

what to do when one of the columns is null?


Jim_Knicely
Forum|alt.badge.img+2
  • Participating Frequently
  • July 14, 2020