2012-09-04 57 views
0

我有一個場景,我需要選擇一行並將值連接到單個變量中。問題是如果某個列是空的,那麼該部分必須被排除。但在我的情況下,它給了一個連字符,我用空的空間可以有人幫助我。如何檢查sql中的select語句中的每個值

我想這.....

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奈 - 泰米爾納德邦 - 印度 -

可在一些這方面的一個幫助...

回答

4

使用ISNULL(O.CSS_No+'-', '')代替ISNULL(O.CSS_No, '') + '-'

另外,可能沒有必要每次都將已經是字符串的字段轉換爲varchar。

+0

This worked .... + ISNULL(CONVERT(VARCHAR,STREET.STREET_START_X)+' - ','') –