Help with the query

ASHMITP 141 Reputation points

Hi there,

I have two tables Appointments and NAP.
I need to join them with logic below -

appointment [(appt location starts with 'D&A') & MRN & Appt Date] = NAP [(SU name starts with 'D&A') & MRN & Appt Date]

How to do the first part in SQL Server query -

left outer join NAP
left(app.[Appt Location],3) and left(nap3.[Service Unit Name],3) ='D&A'
appointment .MRN= NAP .MRN and
appointment .[Appt Date] =NAP .[Service Date]


SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,307 questions
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,597 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 114.5K Reputation points

    Try changing the line to:

    . . .
    appointment.[Appt Location] LIKE 'D&A%' and nap.[Service Unit Name] LIKE 'D&A%'
    . . .

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Bert Zhou-msft 3,421 Reputation points


    Welcome to Microsoft T-SQL Q&A Forum!

    Here is a comparison article about the performance of like and where, I believe it will be more helpful to you. Please check this:

    select *  
    from appointment AP left outer join NAP   
    on NAP.MRN =AP.MRN and NAP.[Service Date]=AP.[Appt Date]  
    Where left(AP.[Appt Location],3)='D&A'   
     and left(nap.[Service Unit Name],3) ='D&A'  

    Best regards,
    Bert Zhou

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments