如何处理字符串连接中的Null值 [英] How to treat Null values in string concatenation
本文介绍了如何处理字符串连接中的Null值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好朋友
我有sql语句执行无例外但结果不正确。
我知道fname,mname和sname形成full_name但是当我运行脚本时,具有空值的记录为full_name返回Null,即使这些记录存在fname和sname。
请在下面找到我的脚本:
选择 emp_id,
CASE mname WHEN NULL THEN emp_id + ' - ' + upper(sname) COLLATE DATABASE_DEFAULT + ' ,' + fname + N < span class =code-string>' '
ELSE
emp_id + ' - ' + upper(sname) COLLATE DATABASE_DEFAULT + ' ,' + fname + ' ' + mname end as full_name
来自 \\ temp
提前感谢您的帮助
解决方案
检查您的逻辑并考虑使用COALESCE(mname,'')
用空字符串替换null。
另请参阅,例如: http://stackoverflow.com/questions/8233746/concatenate-with-null-values-in-sql [ ^ ](目前是最佳答案)。
-SA
有两种方法可以完成任务。
1.)正如上面SA解决方案中提到的使用COALESCE(mname,'')
2.)使用ISNULL(mname,'')例如:
选择IsNull(ColName,'')作为TableName的ColName
您可以使用这些方法中的任何一种来检查脚本中的空值。
谢谢
Sisir Patro
Hello friends
I have sql statement that executes without exception but the result is not correct.
I concontenated fname, mname and and sname to form full_name but when I run the script, records with null values return Null for the full_name even though fname and sname exist for those records.
Please find my script below:
select emp_id,
CASE mname WHEN NULL THEN emp_id + ' - ' + upper(sname) COLLATE DATABASE_DEFAULT + ', ' + fname + N''
ELSE
emp_id + ' - ' + upper(sname) COLLATE DATABASE_DEFAULT + ', ' + fname + ' ' + mname end as full_name
from emp
Thanks in advance for your assistance
解决方案
Check up your logic and consider usingCOALESCE(mname, '')
to replace null with empty string.
See also, for example: http://stackoverflow.com/questions/8233746/concatenate-with-null-values-in-sql[^] (presently, a top answer).
—SA
Hi,
There are 2 ways to achieve the task.
1.) As mentioned in the solution by SA above using COALESCE(mname, '')
2.) Using ISNULL(mname, '') for example:
Select IsNull(ColName, '') As ColName From TableName
You can use any one of these methods to check for the null values in your script.
Thanks
Sisir Patro
这篇关于如何处理字符串连接中的Null值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文