简体   繁体   English

sql中的concat()将如何添加两个或多个包含特殊字符的参数?

[英]How will a concat() in sql will add two or more arguements including special characters?

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

This is a query to display the address and the city with an alias name as Address from a 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 the coma and space between the city and address? 我怎样才能在城市和地址之间添加昏迷和空间? when i use the third string as ', ' it is giving an invalid number of arguments error. 当我使用第三个字符串作为','时,它给出了无效数量的参数错误。

CONCAT versions: CONCAT版本:

I would recommend CONCAT since it will protect you from returning NULL when one of arguments is null - at least in MSSQL. 我建议使用CONCAT,因为当其中一个参数为null时,它将保护您不返回NULL - 至少在MSSQL中是这样。

MSSQL & MySQL & PostgresSQL & Teradata: MSSQL和MySQL&PostgresSQL&Teradata:

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

MySQL version 2: MySQL版本2:

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

Oracle & IBM DB2 & IBM Informix: Oracle&IBM DB2和IBM Informix:

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

IBM DB2: IBM DB2:

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

Non-CONCAT versions: 非CONCAT版本:

|| || is ANSI standard, however not all systems use it. 是ANSI标准,但并非所有系统都使用它。

MSSQL & Sybase IQ: MSSQL和Sybase IQ:

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

MySQL (so called proximity operator, default behavior): MySQL(所谓的邻近运算符,默认行为):

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

PostgresSQL & Oracle & MySQL (with SET sql_mode='PIPES_AS_CONCAT'; ) & IBM Informix & Sybase IQ: PostgresSQL和Oracle&MySQL(使用SET sql_mode='PIPES_AS_CONCAT'; )和IBM Informix和Sybase IQ:

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

In addition to the other answers, I'd like to mention that the standard (ISO 9075) concatenation operator in SQL is || 除了其他答案,我想提一下SQL中的标准(ISO 9075)连接运算符是|| but it isn't supported by all DB engines. 但并非所有数据库引擎都支持它。

select address || ', ' || city

Oracle uses this one Oracle使用这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM