我有一个场景,我需要选择一行并将值连接到单个变量。问题是如果某个列为null,则必须省略该部分。但是在我的场景中它给出了一个连字符,我用空空间可以帮助我。
我试过这个......
SELECT @Location_Details = CONVERT(VARCHAR, ISNULL(O.CSS_No, ''))
+ '-' + CONVERT(VARCHAR, ISNULL(STREET.STREET_START_X, ''))
+ '-' + CONVERT(VARCHAR, ISNULL(STREET.STREET_START_Y, ''))
+ '-' + CONVERT(VARCHAR, ISNULL(STREETDES.STREET_DESCRIPTOR,
'')) + '-'
+ CONVERT(VARCHAR, ISNULL(STREETDES.LOCALITY_NAME, '')) + '-'
+ CONVERT(VARCHAR, ISNULL(STREETDES.TOWN_NAME, '')) + '-'
+ CONVERT(VARCHAR, ISNULL(HW.Description, '')) + '-'
+ CONVERT(VARCHAR, ISNULL(Add_stree.Additional_Street_Location_Text,
''))
FROM dbo.[order] O WITH ( NOLOCK )
LEFT JOIN dbo.Order_USRN_Header USRN WITH ( NOLOCK ) ON USRN.Order_No = O.Order_No
LEFT JOIN DatabaseName.dbo.STREET STREET WITH ( NOLOCK ) ON STREET.USRN_NO = USRN.USRN_No
LEFT JOIN DatabaseName.dbo.STREET_DESCRIPTOR STREETDES WITH ( NOLOCK ) ON STREETDES.USRN_NO = USRN.USRN_No
LEFT JOIN dbo.High_Way_Authority HW WITH ( NOLOCK ) ON HW.HW_Authority_ID = STREET.SWA_ORG_REF
LEFT JOIN [DatabaseName].dbo.additional_STREET Add_stree WITH ( NOLOCK ) ON USRN.USRN_No = Add_stree.USRN
WHERE O.Order_No ='123456'
这给出了......的结果。
-565654-48798-MAIN STREET-Chennai-Tamilnadu-India -
有人可以帮助...
答案 0 :(得分:4)
使用ISNULL(O.CSS_No+'-', '')
代替ISNULL(O.CSS_No, '') + '-'
等
此外,可能不需要每次都将已经是字符串的字段转换为varchar。