How will concat () in sql add two or more arguments including special characters?

select CONCAT(address,city) as Address from student order by Address desc;

      

This is a query to display an address and city with an alias in the form Address from the student table

--- Program output ---

ADDRESS
----------------------------------------------------------------------
LMCCoimbatore
FFFVilupuram
BBBAgra
ABCSalem
AAAPondichery


--- Expected output (text)---
ADDRESS
------------------------------------------------------------------------
LMC, Coimbatore
FFF, Vilupuram
BBB, Agra
ABC, Salem
AAA, Pondichery

      

How can I add to whom and the space between the city and the address? when i use the third line as ',' it gives the wrong number of arguments.

+3


source to share


2 answers


CONCAT versions:

I would recommend CONCAT, as it will protect you from returning NULL when one of the arguments is null - at least in MSSQL.

MSSQL and MySQL and PostgresSQL and Teradata:

select CONCAT(address,', ',city) as Address from student order by Address desc;

      

MySQL version 2:

select CONCAT_WS(', ', address,city) as Address from student order by Address desc;

      

Oracle and IBM DB2 and IBM Informix:

select CONCAT(address,CONCAT(', ',city)) as Address from student order by Address desc;

      

IBM DB2:



select address CONCAT ', ' CONCAT city as Address from student order by Address desc;

      

versions other than CONCAT:

|| is an ANSI standard, but not everyone uses it.

MSSQL and Sybase IQ:

select address + ', ' + city as Address from student order by Address desc;

      

MySQL (aka proximity operator, default behavior):

select address ', ' city as Address from student order by Address desc;

      

PostgresSQL and Oracle and MySQL ( SET sql_mode='PIPES_AS_CONCAT';

) and IBM Informix and Sybase IQ:

select address || ', ' || city as Address from student order by Address desc;

      

+3


source


In addition to the other answers, I would like to mention that the standard concatenation operator (ISO 9075) in SQL is this ||

, but it is not supported by all DB engines.

select address || ', ' || city

      



Oracle uses this

+2


source







All Articles