user3201163 user3201163 - 29 days ago 11
SQL Question

How to concat string and table column?

I'm concatenate two columns as shown below

String col2 = KEY_ENGLISH + " || ' ' || '-' || ' ' || " + KEY_NEPALI;


which gives output as

KEY_ENGLISH - KEY_NEPALI

But I need to display as

[KEY_ENGLISH][KEY_NEPALI]

Using

String col2 = " || '[' || " + KEY_ENGLISH + " || ']' || " + " || '[' || " + KEY_NEPALI + " || ']' || ";


I'm not getting desired output. What is wrong with this?

Answer

|| is a concatenation operator. What your string will contain is this;

 || '[' || KEY_ENGLISH || ']' ||  || '[' || KEY_NEPALI || ']' || 

...which has a lot of extraneous operators.

What you want is instead just || between your strings as in;

 '[' || KEY_ENGLISH || '][' || KEY_NEPALI || ']'

...which would be written as;

String col2 = "'[' || " + KEY_ENGLISH + " || '][' || " + KEY_NEPALI + " || ']'";