Share via

Array to String in CosmosDB using sql query

Tomar, Abhishek 1 Reputation point
2022-04-21T11:33:15.55+00:00

Hi,

It is possible to convert “service_code” array to a normal comma-separated string using cosmos dB SQL API query?

Select c.tin, c.service_code from c

[
{"tin": "111-121212","service_code": [ “01",03"]},
{"tin": "222-121212","service_code": [ “01",03"]}
{"tin": "3333-121212","service_code": [ “01",03"]}

]

Expected output

[
{"tin": "111-121212","service_code": “01,03”},
{"tin": "222-121212","service_code": “01,03”},
{"tin": "3333-121212","service_code": “01,03”}
]

Azure Cosmos DB
Azure Cosmos DB

An Azure NoSQL database service for app development.

0 comments No comments

2 answers

Sort by: Most helpful
  1. Hasan Savran 331 Reputation points MVP
    2022-04-21T16:00:04.533+00:00

    Easiest way to create one will be from the portal.
    In the following example, I gave Array_ToString name to this function.
    All it does is, it takes the input and returns it as string.

    195272-ans.png

    In the following picture, you can see how to run and see its results.

    195291-ans2.png

    Was this answer helpful?


  2. Hasan Savran 331 Reputation points MVP
    2022-04-21T13:09:38.727+00:00

    ToString() function can handle this.

    SELECT ToString(c.service_code) from c

    If you like to change delimeter, you can create your own UDF function to handle it too.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.