Solved

How do I show null reconds on a left join with a filter

Posted on 2014-01-03
5
257 Views
Last Modified: 2014-01-08
I know this easy, but my brain is still on vacation.  Anyway, I have the following query:

select m.[Patient Name],
         c.episode_no,
          convert(varchar(10),CAST( m.DOB as DATE), 101) as DOB,
         m.[Appt Date],
       CONVERT(varchar(10),c.start_dtime,  101)  + ' ' + CONVERT(varchar(10),c.start_dtime,  108) as DOS,
        o.orgz_short_name as Entity
     
         
from Customer.MammoGramPHSFinal as m
left outer join smsmir.mir_pms_case as c
    on m.[Patient Name] = REPLACE(c.rpt_name, ' ,', ', ')
   and CONVERT(varchar(10),c.birth_dtime,101) =  convert(varchar(10),CAST( m.DOB as DATE), 101)
Inner join smsmir.mir_orgz as o  on     c.src_sys_id = o.src_sys_id
where (year(c.start_dtime) = year(m.[Appt Date])
       and Month(c.start_dtime) = Month(m.[Appt Date]))
     o.orgz_short_name = 'BOS'
   order by [Appt Date],
                [Patient Name]

I need the filters but I need to show the nulls as well.
0
Comment
Question by:Butterfly2
  • 3
  • 2
5 Comments
 
LVL 26

Expert Comment

by:tigin44
Comment Utility
you may replace
   and  o.orgz_short_name = 'BOS'

with
    and  (o.orgz_short_name = 'BOS' OR ISNULL(o.orgz_short_name, '-') = '-')
0
 

Author Comment

by:Butterfly2
Comment Utility
what about the date filter?
0
 
LVL 26

Expert Comment

by:tigin44
Comment Utility
so try this

(year(ISNULL(c.start_dtime,GETDATE())) = year(ISNULL(m.[Appt Date], GETDATE()))
    and Month(ISNULL(c.start_dtime,GETDATE())) = Month(ISNULL(m.[Appt Date], GETDATE())))
    and  (o.orgz_short_name = 'BOS' OR ISNULL(o.orgz_short_name, '-') = '-')
0
 

Accepted Solution

by:
Butterfly2 earned 0 total points
Comment Utility
I figured it out:

select m.[Patient Name],
         c.episode_no,
          convert(varchar(10),CAST( m.DOB as DATE), 101) as DOB,
         m.[Appt Date],
       CONVERT(varchar(10),c.start_dtime,  101)  + ' ' + CONVERT(varchar(10),c.start_dtime,  108) as DOS,
        o.orgz_short_name as Entity
     
         
from Customer.MammoGramPHSFinal as m
left outer join smsmir.mir_pms_case as c
    on m.[Patient Name] = REPLACE(c.rpt_name, ' ,', ', ')
   and CONVERT(varchar(10),c.birth_dtime,101) =  convert(varchar(10),CAST( m.DOB as DATE), 101)
  and  (c.start_dtime is null
           or(year(c.start_dtime) = year(m.[Appt Date])
            and Month(c.start_dtime) = Month(m.[Appt Date])))
left join smsmir.mir_orgz as o  
     on   c.src_sys_id = o.src_sys_id
     and o.orgz_short_name = 'BOS'  

   order by [Appt Date],
                [Patient Name]
0
 

Author Closing Comment

by:Butterfly2
Comment Utility
I figure out the answer and it wasnt even close to the sugguestion I recieved.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now