Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2014-01-03
5
Medium Priority
?
280 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 26

Expert Comment

by:tigin44
ID: 39753863
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
ID: 39753866
what about the date filter?
0
 
LVL 26

Expert Comment

by:tigin44
ID: 39753921
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
ID: 39753945
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
ID: 39764673
I figure out the answer and it wasnt even close to the sugguestion I recieved.
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

609 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