简体   繁体   中英

How to check Each values in select statment in sql

I Have a scenario that i need to select a row and concatenate the values into the single variable. The problem is if certain column is null then the part has to be left out. But in my scenario it gives a Hyphen which i used with the empty space can some one help me with this.

I tried this.....

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'

this gives the result like....

-565654-48798-MAIN STREET-Chennai-Tamilnadu-India-

Can some one help in this...

Use ISNULL(O.CSS_No+'-', '') instead of ISNULL(O.CSS_No, '') + '-' , etc

Also, there's probably no need to convert a field which is already a string to a varchar each time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM