COALESCE and CONCAT Address Line 1 and Address Line 2 separated by a comma

Bobby P 231 Reputation points
2024-05-09T18:55:45.0266667+00:00

We are trying to COALESCE and CONCAT Address Line 1 and Address Line 2 separated by a comma "," from potentially two data sources.

So like...123 Main St., Apt. 101

The first part is easy enough...

CONCAT (

COALESCE (

LTRIM (RTRIM ([Temp_Table_State_Funded_NonMedicaid_EDW_Member_Subset].[EDW_Address_AddressLine1])),

LTRIM (RTRIM ([#Temp_Table_State_Funded_NonMedicaid_EDW_Eligibility_Span].[EDW_MemberEligibility_CardAddress1])),

''

),

So how do we do the second part and COALESE and CONCAT based on the Address Line 2 being valued and preceded by a comma "," if Address Line 2 is from two different sources?

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
14,223 questions
SQL Server Transact-SQL
SQL Server Transact-SQL
SQL Server: A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.Transact-SQL: A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
108 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,672 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Naveen Kumar M 75 Reputation points
    2024-12-24T03:25:12.4866667+00:00

    Hi @Bobby P

    To achieve this, you can use COALESCE and CONCAT in combination with a conditional CASE or IIF statement to determine whether Address Line 2 exists and then prepend the comma if needed.

    This approach ensures that the second part (Address Line 2) is only included if it has a value, and it properly formats the comma when necessary.

    Query:

    CONCAT(
        -- First, handle Address Line 1
        COALESCE(
            LTRIM(RTRIM([Temp_Table_State_Funded_NonMedicaid_EDW_Member_Subset].[EDW_Address_AddressLine1])),
            LTRIM(RTRIM([#Temp_Table_State_Funded_NonMedicaid_EDW_Eligibility_Span].[EDW_MemberEligibility_CardAddress1])),
            ''
        ),
    
        -- Then, handle Address Line 2 with a conditional comma
        CASE
            WHEN COALESCE(
                LTRIM(RTRIM([Temp_Table_State_Funded_NonMedicaid_EDW_Member_Subset].[EDW_Address_AddressLine2])),
                LTRIM(RTRIM([#Temp_Table_State_Funded_NonMedicaid_EDW_Eligibility_Span].[EDW_MemberEligibility_CardAddress2]))
            ) <> '' THEN
                CONCAT(
                    ', ',
                    COALESCE(
                        LTRIM(RTRIM([Temp_Table_State_Funded_NonMedicaid_EDW_Member_Subset].[EDW_Address_AddressLine2])),
                        LTRIM(RTRIM([#Temp_Table_State_Funded_NonMedicaid_EDW_Eligibility_Span].[EDW_MemberEligibility_CardAddress2]))
                    )
                )
            ELSE
                '' -- If no Address Line 2, don't add anything     END )
    
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.