Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql字符串添加问题_Sql_Sql Server_Tsql - Fatal编程技术网

Sql字符串添加问题

Sql字符串添加问题,sql,sql-server,tsql,Sql,Sql Server,Tsql,问题是,当一个为null时,整个字符串为null,是否有某种技巧可以绕过这个问题 msSQL 2005您正在寻找以下功能: SELECT a.one + ' test ' +b.two from table1 a right join table1 on a.id =b.id 如果ISNULL函数的第一个参数为null,则提供第二个参数。 这样,所有连接的字段都不会返回null,您将得到一个字符串而不是null。这取决于当一个或两个输入都为null时,您希望结果是什么。如果只希望每个部分折叠

问题是,当一个为null时,整个字符串为null,是否有某种技巧可以绕过这个问题 msSQL 2005

您正在寻找以下功能:

SELECT a.one + ' test ' +b.two from table1 a right join table1 on a.id =b.id 
如果
ISNULL
函数的第一个参数为null,则提供第二个参数。
这样,所有连接的字段都不会返回null,您将得到一个字符串而不是null。

这取决于当一个或两个输入都为null时,您希望结果是什么。如果只希望每个部分折叠为空字符串,请使用ISNULL:

SELECT ISNULL(a.one,'') + ' test ' + ISNULL(b.two , '')
from table1 a 
right join table1 b 
  on a.id =b.id 

否则,您将不得不巧妙地使用CASE表达式。

根据您想要的输出,有几种变化

ISNULL(a.one, '') + ' test ' + ISNULL(b.two, '')
副本
-- leading/trailing spaces on test
SELECT ISNULL(a.one,'') + ' test ' + ISNULL(b.two , '')

-- no spacing around test
SELECT ISNULL(a.one,' ') + 'test' + ISNULL(' ' + b.two, '')

-- do you want the word test at all if one is blank?
SELECT ISNULL(a.one + ' test','') + ISNULL(' ' + b.two, '')