Search code examples
couchbasesql++

N1QL: Couchbase: How to use concat function with mixed typed


I am using couchbase and trying to concat mixed types

Example

SELECT CONCAT("abc",1, "ghi") AS concat;

RETURNS

[
  {
    "concat": null
  }
]

I am expecting

[
  {
    "concat": abc1ghi
  }
]

How do I achieve this?


Solution

  • Concat() can be done on strings only, use explicit cast with type conversion functions https://docs.couchbase.com/server/current/n1ql/n1ql-language-reference/typefun.html#type-conversion-functions

    For string TO_STR(), TO_STRING(), TOSTR(), TOSTRING() (synonms)

    SELECT CONCAT("abc",TO_STR(1), "ghi") AS concat;
    

    With separator and flatten the ARRAY of strings before concat

    SELECT CONCAT2("-","abc",TO_STR(1), ["ghi", "xyz"] ) AS concat;