TSQL获取数据而不考虑空值
我有一个存储过程,它从employee表中获取信息并返回数据 使用了3列:TSQL获取数据而不考虑空值,sql,sql-server,tsql,Sql,Sql Server,Tsql,我有一个存储过程,它从employee表中获取信息并返回数据 使用了3列: B.[SiloDesc] + ' (' + B.[TitleDesc] + ') ' + B.[SkillSetDesc] as SkillSetDesc, 我的问题是,如果其中一个恰好为空,它将不会显示任何数据。无论其中一个字段是否为空,让它包含数据的最佳方式是什么。您可以使用或为每个单独的列。。。或者你可以简单地使用 isnull(B.[SiloDesc], '') + ' (' + isnull(B.
B.[SiloDesc] + ' (' + B.[TitleDesc] + ') ' + B.[SkillSetDesc] as SkillSetDesc,
我的问题是,如果其中一个恰好为空,它将不会显示任何数据。无论其中一个字段是否为空,让它包含数据的最佳方式是什么。您可以使用或为每个单独的列。。。或者你可以简单地使用
isnull(B.[SiloDesc], '')
+ ' (' + isnull(B.[TitleDesc], '') + ') '
+ isnull(B.[SkillSetDesc], '') as SkillSetDesc,
(string\u value1,string\u value2[,string\u valueN])
获取数量可变的字符串参数,并将它们连接到单个字符串中。它至少需要两个输入值;否则,将引发错误。所有参数都隐式转换为字符串类型,然后连接空值隐式转换为空字符串。
A.:当TitleDesc
为null
时,删除括号:
select concat(B.[SiloDesc], ' (' + B.[TitleDesc] + ')', ' ' + B.[SkillSetDesc])
由于sql中处理null
的方式,表达式'('+null+')
会导致null
,而concat()
会将其视为空字符串。。。这有点好,因为如果值为null
,它可以有效地删除括号
B.:保留括号:
select concat(B.[SiloDesc], ' (', B.[TitleDesc], ') ', B.[SkillSetDesc])
样本:
select concat('john', ' (' + null + ')', ' adams') -- john adams
select concat('john', ' (test)', ' ' + null) -- john (test)
select concat('john', ' (the man)', ' adams') -- john (the man) adams
您可以为每个单独的列使用或。。。或者你可以简单地使用
(string\u value1,string\u value2[,string\u valueN])
获取数量可变的字符串参数,并将它们连接到单个字符串中。它至少需要两个输入值;否则,将引发错误。所有参数都隐式转换为字符串类型,然后连接空值隐式转换为空字符串。
A.:当TitleDesc
为null
时,删除括号:
select concat(B.[SiloDesc], ' (' + B.[TitleDesc] + ')', ' ' + B.[SkillSetDesc])
由于sql中处理null
的方式,表达式'('+null+')
会导致null
,而concat()
会将其视为空字符串。。。这有点好,因为如果值为null
,它可以有效地删除括号
B.:保留括号:
select concat(B.[SiloDesc], ' (', B.[TitleDesc], ') ', B.[SkillSetDesc])
样本:
select concat('john', ' (' + null + ')', ' adams') -- john adams
select concat('john', ' (test)', ' ' + null) -- john (test)
select concat('john', ' (the man)', ' adams') -- john (the man) adams
您需要为每个可空列添加聚结,例如,聚结(B.[SiloDesc],'')您觉得有用吗?您需要为每个可空列添加聚结,例如,聚结(B.[SiloDesc],'')您觉得有用吗?