question

ASHMITP-0361 avatar image
0 Votes"
ASHMITP-0361 asked BertZhoumsft-7490 edited

Help with the query

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-generalsql-server-transact-sql
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Viorel-1 avatar image
1 Vote"
Viorel-1 answered

Try changing the line to:

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

BertZhoumsft-7490 avatar image
0 Votes"
BertZhoumsft-7490 answered BertZhoumsft-7490 edited

Hi,@kkran-5951

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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.