Help with the query

ASHMITP 141 Reputation points
2022-05-09T05:18:06.543+00:00

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 -

appointment
left outer join NAP
on
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]

Thanks

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
Transact-SQL
Transact-SQL
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
    2022-05-09T05:29:18.183+00:00

    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
    2022-05-09T07:24:52.077+00:00

    Hi,@kkran

    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