USE [PATIENT_DATA] GO /****** Object: StoredProcedure [dbo].[sp_Adm_LoadAdmission] Script Date: 07/09/2014 00:51:01 ******/ SET ANSI_NULLS OFF GO SET QUOTED_IDENTIFIER OFF GO ALTER PROCEDURE [dbo].[sp_Adm_LoadAdmission] @StationID AS VarChar(4), @AccomodationID AS VarChar(1), @ServiceID AS VarChar(3), @Lastname AS VarChar(50), @HospNum As VarChar(8), @IDNum AS VarChar(10), @RoomID As VarChar(8), @SearchType as VarChar(1), @CurrentDischarges as varchar(1) = 'N', @CARDNUM as varchar (20) = '', @Type as varchar(1) = '1' AS --Optimized By Angelo 04/15/2006 Declare @PxLastName VarChar(50) ; Declare @PxFirstName VarChar(50) ; Declare @PxMiddleName VarChar(50) ; Declare @PxName VarChar(50) ; Declare @Pos Int; Declare @EndDate as varchar(10); Declare @StartDate as varchar(10); Declare @DateToday as varchar(10); Set @DateToday = (Select convert(varchar(10),getdate(),101)) Set @EndDate = (Select convert(varchar(10),getdate(),101)) Set @StartDate = (select convert(varchar(10), dateadd(month,-1,@EndDate),101)) Set @PxName = '' ; Set @PxLastName = '' ; Set @PxFirstName = '' ; Set @PxMiddleName = '' ; if @Type = '1' begin If Len(@LastName) > 0 BEGIN Set @PxName = @LastName ; Set @Pos = PATINDEX('%,%',@PxName) ; IF @Pos > 0 BEGIN Set @PxLastName = RTrim(SubString( @PxName, 1, @POS - 1 )) ; Set @PxName = LTrim(RTRim(SubString( @PxName, @Pos + 1, 50 ))) ; Set @Pos = PatIndex('%,%',@PxName) ; If @Pos > 0 BEGIN Set @PxFirstName = RTRim(SubString( @PxName, 1, @Pos -1 )) ; Set @PxMiddleName = RTrim(LTrim( SubString( @PxName, @Pos + 1,50 ) )) ; END ; Else BEGIN Set @PxFirstName = @PxName ; END ; END ; ELSE BEGIN Set @PxLastName = RTRim(LTrim(@LastName)) ; END ; END ; IF LEN(@PXMIDDLENAME) > 0 SELECT Case When tbPatient.DcrDate is Null Then '' Else '*' End + Case When tbPatient2.Confidential = 1 Then '!' Else '' End + Case When tbPatient.BillingDate is Null Then '' Else 'B' End , tbPatient.IDNum as [Adm.#], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.LastName) Else tbMaster.LastName End AS [Last Name], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.FirstName) Else tbmaster.FirstName End AS [First Name], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.MiddleName) Else tbmaster.MiddleName End AS [Middle Name], isnull(tbPatient.RoomID,'') AS Room, isnull(tbMaster.BirthDate,'') AS Birth, isnull(tbMaster.Sex,'') as Sex, tbPatient.AdmDate AS [Adm.Date], tbPatient.DcrDate AS [Dcr.Date], tbPatient.HospNum AS [Hosp.#], rTrim(isnull(tbDoctor.LastName,'')) + ', ' + rTrim(isnull(tbDoctor.FirstName,'')) As [Attending Doctor], tbPatient.BillingDate as [Billing Date], Case when isnull(DN.IDNum,'') = '' then '0' else case when NoticeDate is null or DN.RecordStatus = 'R' then '0' else '1' end End MayGoHome FROM tbPatient tbPatient Inner Join tbMaster tbMaster on tbPatient.HospNum = tbMaster.HospNum left outer join Build_File..tbCoDoctor AS tbDoctor ON tbPatient.AttendingDr1 = tbDoctor.DoctorID left outer join Build_File..tbCoRoom Room On tbPatient.RoomID = Room.RoomID LEft Outer Join tbPatient2 tbPatient2 On tbPatient.IDNum = tbPatient2.IDNum LeFt Outer Join Station..tbNurseDischargeNotice DN on tbPatient.IDNum = DN.IDNum WHERE (Room.StationID = @StationID or @StationID = '') and (tbPatient.ServiceID = @ServiceID or @ServiceID = '' ) and (Room.AccomodationID = @AccomodationID or @AccomodationID = '') and ( tbMaster.Lastname like @PxLastName + '%' or tbMaster.Lastname like @PxLastName + 'A%' ) AND ( tbMaster.FirstName like @PxFirstName + '%') AND ( tbMaster.MiddleName like @PxMiddleName + '%') AND ( @SearchType = 'A' OR ( @SearchType = 'B' AND tbPatient.DcrDate IS NULL ) OR ( @SearchType = 'C' AND NOT tbPatient.DcrDate IS NULL and (tbPatient.DcrDate between @DateToday and @DateToday + ' 23:59:59.99' or @CurrentDischarges = 'N') ) ) ORDER BY tbMaster.LastName + tbMaster.FirstName + ISNULL( tbMaster.MiddleName,'') ELSE IF LEN(@PXFIRSTNAME) > 0 SELECT Case When tbPatient.DcrDate is Null Then '' Else '*' End + Case When tbPatient2.Confidential = 1 Then '!' Else '' End + Case When tbPatient.BillingDate is Null Then '' Else 'B' End , tbPatient.IDNum as [Adm.#], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.LastName) Else tbMaster.LastName End AS [Last Name], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.FirstName) Else tbmaster.FirstName End AS [First Name], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.MiddleName) Else tbmaster.MiddleName End AS [Middle Name], tbPatient.RoomID AS Room, tbMaster.BirthDate AS Birth, tbMaster.Sex, tbPatient.AdmDate AS [Adm.Date], tbPatient.DcrDate AS [Dcr.Date], tbPatient.HospNum AS [Hosp.#], rTrim(tbDoctor.LastName) + ', ' + rTrim(tbDoctor.FirstName) As [Attending Doctor], tbPatient.BillingDate as [Billing Date], Case when isnull(DN.IDNum,'') = '' then '0' else case when NoticeDate is null or DN.RecordStatus = 'R' then '0' else '1' end End MayGoHome FROM tbPatient tbPatient Inner Join tbMaster tbMaster on tbPatient.HospNum = tbMaster.HospNum left outer join Build_File..tbCoDoctor AS tbDoctor ON tbPatient.AttendingDr1 = tbDoctor.DoctorID left outer join Build_File..tbCoRoom Room On tbPatient.RoomID = Room.RoomID LEft Outer Join tbPatient2 tbPatient2 On tbPatient.IDNum = tbPatient2.IDNum LeFt Outer Join Station..tbNurseDischargeNotice DN on tbPatient.IDNum = DN.IDNum WHERE (Room.StationID = @StationID or @StationID = '') and (tbPatient.ServiceID = @ServiceID or @ServiceID = '' ) and (Room.AccomodationID = @AccomodationID or @AccomodationID = '') and ( tbMaster.Lastname like @PxLastName + '%' or tbMaster.Lastname like @PxLastName + 'A%' ) AND ( tbMaster.FirstName like @PxFirstName + '%') AND ( @SearchType = 'A' OR ( @SearchType = 'B' AND tbPatient.DcrDate IS NULL ) OR ( @SearchType = 'C' AND NOT tbPatient.DcrDate IS NULL and (tbPatient.DcrDate between @DateToday and @DateToday + ' 23:59:59.99' or @CurrentDischarges = 'N') ) ) ORDER BY tbMaster.LastName + tbMaster.FirstName + ISNULL( tbMaster.MiddleName,'') ELSE IF LEN(@PXLASTNAME) > 0 SELECT Case When tbPatient.DcrDate is Null Then '' Else '*' End + Case When tbPatient2.Confidential = 1 Then '!' Else '' End + Case When tbPatient.BillingDate is Null Then '' Else 'B' End , tbPatient.IDNum as [Adm.#], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.LastName) Else tbMaster.LastName End AS [Last Name], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.FirstName) Else tbmaster.FirstName End AS [First Name], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.MiddleName) Else tbmaster.MiddleName End AS [Middle Name], tbPatient.RoomID AS Room, tbMaster.BirthDate AS Birth, tbMaster.Sex, tbPatient.AdmDate AS [Adm.Date], tbPatient.DcrDate AS [Dcr.Date], tbPatient.HospNum AS [Hosp.#], rTrim(tbDoctor.LastName) + ', ' + rTrim(tbDoctor.FirstName) As [Attending Doctor], tbPatient.BillingDate as [Billing Date], Case when isnull(DN.IDNum,'') = '' then '0' else case when NoticeDate is null or DN.RecordStatus = 'R' then '0' else '1' end End MayGoHome FROM tbPatient tbPatient Inner Join tbMaster tbMaster on tbPatient.HospNum = tbMaster.HospNum left outer join Build_File..tbCoDoctor AS tbDoctor ON tbPatient.AttendingDr1 = tbDoctor.DoctorID left outer join Build_File..tbCoRoom Room On tbPatient.RoomID = Room.RoomID LEft Outer Join tbPatient2 tbPatient2 On tbPatient.IDNum = tbPatient2.IDNum LeFt Outer Join Station..tbNurseDischargeNotice DN on tbPatient.IDNum = DN.IDNum WHERE (Room.StationID = @StationID or @StationID = '') and (tbPatient.ServiceID = @ServiceID or @ServiceID = '' ) and (Room.AccomodationID = @AccomodationID or @AccomodationID = '') and ( tbMaster.Lastname like @PxLastName + '%' or tbMaster.Lastname like @PxLastName + 'A%' ) AND ( @SearchType = 'A' OR ( @SearchType = 'B' AND tbPatient.DcrDate IS NULL ) OR ( @SearchType = 'C' AND NOT tbPatient.DcrDate IS NULL and (tbPatient.DcrDate between @DateToday and @DateToday + ' 23:59:59.99' or @CurrentDischarges = 'N') ) ) ORDER BY tbMaster.LastName + tbMaster.FirstName + ISNULL( tbMaster.MiddleName,'') ELSE IF LEN(@HOSPNUM) > 0 SELECT Case When tbPatient.DcrDate is Null Then '' Else '*' End + Case When tbPatient2.Confidential = 1 Then '!' Else '' End + Case When tbPatient.BillingDate is Null Then '' Else 'B' End , tbPatient.IDNum as [Adm.#], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.LastName) Else tbMaster.LastName End AS [Last Name], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.FirstName) Else tbmaster.FirstName End AS [First Name], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.MiddleName) Else tbmaster.MiddleName End AS [Middle Name], tbPatient.RoomID AS Room, tbMaster.BirthDate AS Birth, tbMaster.Sex, tbPatient.AdmDate AS [Adm.Date], tbPatient.DcrDate AS [Dcr.Date], tbPatient.HospNum AS [Hosp.#], rTrim(tbDoctor.LastName) + ', ' + rTrim(tbDoctor.FirstName) As [Attending Doctor], tbPatient.BillingDate as [Billing Date], Case when isnull(DN.IDNum,'') = '' then '0' else case when NoticeDate is null or DN.RecordStatus = 'R' then '0' else '1' end End MayGoHome FROM tbPatient tbPatient Inner Join tbMaster tbMaster on tbPatient.HospNum = tbMaster.HospNum left outer join Build_File..tbCoDoctor AS tbDoctor ON tbPatient.AttendingDr1 = tbDoctor.DoctorID left outer join Build_File..tbCoRoom Room On tbPatient.RoomID = Room.RoomID LEft Outer Join tbPatient2 tbPatient2 On tbPatient.IDNum = tbPatient2.IDNum LeFt Outer Join Station..tbNurseDischargeNotice DN on tbPatient.IDNum = DN.IDNum WHERE (Room.StationID = @StationID or @StationID = '') and (tbPatient.ServiceID = @ServiceID or @ServiceID = '' ) and (Room.AccomodationID = @AccomodationID or @AccomodationID = '') and (tbPatient.Hospnum = @HospNum) AND ( @SearchType = 'A' OR ( @SearchType = 'B' AND tbPatient.DcrDate IS NULL ) OR ( @SearchType = 'C' AND NOT tbPatient.DcrDate IS NULL and (tbPatient.DcrDate between @DateToday and @DateToday + ' 23:59:59.99' or @CurrentDischarges = 'N') ) ) ORDER BY tbMaster.LastName + tbMaster.FirstName + ISNULL( tbMaster.MiddleName,'') ELSE IF LEN(@IDNUM) > 0 SELECT Case When tbPatient.DcrDate is Null Then '' Else '*' End + Case When tbPatient2.Confidential = 1 Then '!' Else '' End + Case When tbPatient.BillingDate is Null Then '' Else 'B' End , tbPatient.IDNum as [Adm.#], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.LastName) Else tbMaster.LastName End AS [Last Name], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.FirstName) Else tbmaster.FirstName End AS [First Name], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.MiddleName) Else tbmaster.MiddleName End AS [Middle Name], tbPatient.RoomID AS Room, tbMaster.BirthDate AS Birth, tbMaster.Sex, tbPatient.AdmDate AS [Adm.Date], tbPatient.DcrDate AS [Dcr.Date], tbPatient.HospNum AS [Hosp.#], rTrim(tbDoctor.LastName) + ', ' + rTrim(tbDoctor.FirstName) As [Attending Doctor], tbPatient.BillingDate as [Billing Date], Case when isnull(DN.IDNum,'') = '' then '0' else case when NoticeDate is null or DN.RecordStatus = 'R' then '0' else '1' end End MayGoHome FROM tbPatient tbPatient Inner Join tbMaster tbMaster on tbPatient.HospNum = tbMaster.HospNum left outer join Build_File..tbCoDoctor AS tbDoctor ON tbPatient.AttendingDr1 = tbDoctor.DoctorID left outer join Build_File..tbCoRoom Room On tbPatient.RoomID = Room.RoomID LEft Outer Join tbPatient2 tbPatient2 On tbPatient.IDNum = tbPatient2.IDNum LeFt Outer Join Station..tbNurseDischargeNotice DN on tbPatient.IDNum = DN.IDNum WHERE (Room.StationID = @StationID or @StationID = '') and (tbPatient.ServiceID = @ServiceID or @ServiceID = '' ) and (Room.AccomodationID = @AccomodationID or @AccomodationID = '') and (tbPatient.IDNum = @IDNum) AND ( @SearchType = 'A' OR ( @SearchType = 'B' AND tbPatient.DcrDate IS NULL ) OR ( @SearchType = 'C' AND NOT tbPatient.DcrDate IS NULL and (tbPatient.DcrDate between @DateToday and @DateToday + ' 23:59:59.99' or @CurrentDischarges = 'N') ) ) ORDER BY tbMaster.LastName + tbMaster.FirstName + ISNULL( tbMaster.MiddleName,'') ELSE IF LEN(@CARDNUM) > 0 SELECT Case When tbPatient.DcrDate is Null Then '' Else '*' End + Case When tbPatient2.Confidential = 1 Then '!' Else '' End + Case When tbPatient.BillingDate is Null Then '' Else 'B' End , tbPatient.IDNum as [Adm.#], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.LastName) Else tbMaster.LastName End AS [Last Name], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.FirstName) Else tbmaster.FirstName End AS [First Name], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.MiddleName) Else tbmaster.MiddleName End AS [Middle Name], tbPatient.RoomID AS Room, tbMaster.BirthDate AS Birth, tbMaster.Sex, tbPatient.AdmDate AS [Adm.Date], tbPatient.DcrDate AS [Dcr.Date], tbPatient.HospNum AS [Hosp.#], rTrim(tbDoctor.LastName) + ', ' + rTrim(tbDoctor.FirstName) As [Attending Doctor], tbPatient.BillingDate as [Billing Date], Case when isnull(DN.IDNum,'') = '' then '0' else case when NoticeDate is null or DN.RecordStatus = 'R' then '0' else '1' end End MayGoHome FROM tbPatient tbPatient Inner Join tbMaster tbMaster on tbPatient.HospNum = tbMaster.HospNum left outer join Build_File..tbCoDoctor AS tbDoctor ON tbPatient.AttendingDr1 = tbDoctor.DoctorID left outer join Build_File..tbCoRoom Room On tbPatient.RoomID = Room.RoomID LEft Outer Join tbPatient2 tbPatient2 On tbPatient.IDNum = tbPatient2.IDNum LeFt Outer Join Station..tbNurseDischargeNotice DN on tbPatient.IDNum = DN.IDNum WHERE (Room.StationID = @StationID or @StationID = '') and (tbPatient.ServiceID = @ServiceID or @ServiceID = '' ) and (Room.AccomodationID = @AccomodationID or @AccomodationID = '') and (tbMaster.CardNumber = @CARDNUM) AND ( @SearchType = 'A' OR ( @SearchType = 'B' AND tbPatient.DcrDate IS NULL ) OR ( @SearchType = 'C' AND NOT tbPatient.DcrDate IS NULL and (tbPatient.DcrDate between @DateToday and @DateToday + ' 23:59:59.99' or @CurrentDischarges = 'N') ) ) ORDER BY tbMaster.LastName + tbMaster.FirstName + ISNULL( tbMaster.MiddleName,'') ELSE IF LEN(@ROOMID) > 0 SELECT Case When tbPatient.DcrDate is Null Then '' Else '*' End + Case When tbPatient2.Confidential = 1 Then '!' Else '' End + Case When tbPatient.BillingDate is Null Then '' Else 'B' End , tbPatient.IDNum as [Adm.#], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.LastName) Else tbMaster.LastName End AS [Last Name], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.FirstName) Else tbmaster.FirstName End AS [First Name], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.MiddleName) Else tbmaster.MiddleName End AS [Middle Name], tbPatient.RoomID AS Room, tbMaster.BirthDate AS Birth, tbMaster.Sex, tbPatient.AdmDate AS [Adm.Date], tbPatient.DcrDate AS [Dcr.Date], tbPatient.HospNum AS [Hosp.#], rTrim(tbDoctor.LastName) + ', ' + rTrim(tbDoctor.FirstName) As [Attending Doctor], tbPatient.BillingDate as [Billing Date], Case when isnull(DN.IDNum,'') = '' then '0' else case when NoticeDate is null or DN.RecordStatus = 'R' then '0' else '1' end End MayGoHome FROM tbPatient tbPatient Inner Join tbMaster tbMaster on tbPatient.HospNum = tbMaster.HospNum left outer join Build_File..tbCoDoctor AS tbDoctor ON tbPatient.AttendingDr1 = tbDoctor.DoctorID left outer join Build_File..tbCoRoom Room On tbPatient.RoomID = Room.RoomID LEft Outer Join tbPatient2 tbPatient2 On tbPatient.IDNum = tbPatient2.IDNum LeFt Outer Join Station..tbNurseDischargeNotice DN on tbPatient.IDNum = DN.IDNum WHERE (Room.StationID = @StationID or @StationID = '') and (tbPatient.ServiceID = @ServiceID or @ServiceID = '' ) and (Room.AccomodationID = @AccomodationID or @AccomodationID = '') and (tbPatient.RoomID = @RoomID) AND ( @SearchType = 'A' OR ( @SearchType = 'B' AND tbPatient.DcrDate IS NULL ) OR ( @SearchType = 'C' AND NOT tbPatient.DcrDate IS NULL and (tbPatient.DcrDate between @DateToday and @DateToday + ' 23:59:59.99' or @CurrentDischarges = 'N') ) ) ORDER BY tbMaster.LastName + tbMaster.FirstName + ISNULL( tbMaster.MiddleName,'') ELSE --Default SELECT Case When tbPatient.DcrDate is Null Then '' Else '*' End + Case When tbPatient2.Confidential = 1 Then '!' Else '' End + Case When tbPatient.BillingDate is Null Then '' Else 'B' End , tbPatient.IDNum as [Adm.#], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.LastName) Else tbMaster.LastName End AS [Last Name], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.FirstName) Else tbmaster.FirstName End AS [First Name], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.MiddleName) Else tbmaster.MiddleName End AS [Middle Name], tbPatient.RoomID AS Room, tbMaster.BirthDate AS Birth, tbMaster.Sex, tbPatient.AdmDate AS [Adm.Date], tbPatient.DcrDate AS [Dcr.Date], tbPatient.HospNum AS [Hosp.#], rTrim(tbDoctor.LastName) + ', ' + rTrim(tbDoctor.FirstName) As [Attending Doctor], tbPatient.BillingDate as [Billing Date], Case when isnull(DN.IDNum,'') = '' then '0' else case when NoticeDate is null or DN.RecordStatus = 'R' then '0' else '1' end End MayGoHome FROM tbPatient tbPatient Inner Join tbMaster tbMaster on tbPatient.HospNum = tbMaster.HospNum left outer join Build_File..tbCoDoctor AS tbDoctor ON tbPatient.AttendingDr1 = tbDoctor.DoctorID left outer join Build_File..tbCoRoom Room On tbPatient.RoomID = Room.RoomID LEft Outer Join tbPatient2 tbPatient2 On tbPatient.IDNum = tbPatient2.IDNum LeFt Outer Join Station..tbNurseDischargeNotice DN on tbPatient.IDNum = DN.IDNum WHERE (Room.StationID = @StationID or @StationID = '') and (tbPatient.ServiceID = @ServiceID or @ServiceID = '' ) and (Room.AccomodationID = @AccomodationID or @AccomodationID = '') and ( @SearchType = 'A' OR ( @SearchType = 'B' AND tbPatient.DcrDate IS NULL ) OR ( @SearchType = 'C' AND NOT tbPatient.DcrDate IS NULL and (tbPatient.DcrDate between @DateToday and @DateToday + ' 23:59:59.99' or @CurrentDischarges = 'N') ) ) ORDER BY tbMaster.LastName + tbMaster.FirstName + ISNULL( tbMaster.MiddleName,'') end else if @Type = '2' begin If Len(@LastName) > 0 BEGIN Set @PxName = @LastName ; Set @Pos = PATINDEX('%,%',@PxName) ; IF @Pos > 0 BEGIN Set @PxLastName = RTrim(SubString( @PxName, 1, @POS - 1 )) ; Set @PxName = LTrim(RTRim(SubString( @PxName, @Pos + 1, 50 ))) ; Set @Pos = PatIndex('%,%',@PxName) ; If @Pos > 0 BEGIN Set @PxFirstName = RTRim(SubString( @PxName, 1, @Pos -1 )) ; Set @PxMiddleName = RTrim(LTrim( SubString( @PxName, @Pos + 1,50 ) )) ; END ; Else BEGIN Set @PxFirstName = @PxName ; END ; END ; ELSE BEGIN Set @PxLastName = RTRim(LTrim(@LastName)) ; END ; END ; IF LEN(@PXMIDDLENAME) > 0 SELECT Case When tbPatient.DcrDate is Null Then '' Else '*' End + Case When tbPatient2.Confidential = 1 Then '!' Else '' End + Case When tbPatient.BillingDate is Null Then '' Else 'B' End , tbPatient.IDNum as [Adm.#], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.LastName) Else tbMaster.LastName End AS [Last Name], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.FirstName) Else tbmaster.FirstName End AS [First Name], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.MiddleName) Else tbmaster.MiddleName End AS [Middle Name], tbPatient.RoomID AS Room, tbMaster.BirthDate AS Birth, tbMaster.Sex, tbPatient.AdmDate AS [Adm.Date], tbPatient.DcrDate AS [Dcr.Date], tbPatient.HospNum AS [Hosp.#], rTrim(tbDoctor.LastName) + ', ' + rTrim(tbDoctor.FirstName) As [Attending Doctor], tbPatient.BillingDate as [Billing Date], Case when isnull(DN.IDNum,'') = '' then '0' else case when NoticeDate is null or DN.RecordStatus = 'R' then '0' else '1' end End MayGoHome FROM tbPatient tbPatient Inner Join tbMaster tbMaster on tbPatient.HospNum = tbMaster.HospNum left outer join Build_File..tbCoDoctor AS tbDoctor ON tbPatient.AttendingDr1 = tbDoctor.DoctorID left outer join Build_File..tbCoRoom Room On tbPatient.RoomID = Room.RoomID LEft Outer Join tbPatient2 tbPatient2 On tbPatient.IDNum = tbPatient2.IDNum LeFt Outer Join Station..tbNurseDischargeNotice DN on tbPatient.IDNum = DN.IDNum WHERE (Room.StationID = @StationID or @StationID = '') and (tbPatient.ServiceID = @ServiceID or @ServiceID = '' ) and (Room.AccomodationID = @AccomodationID or @AccomodationID = '') and ( tbMaster.Lastname like @PxLastName + '%' or tbMaster.Lastname like @PxLastName + 'A%' ) AND ( tbMaster.FirstName like @PxFirstName + '%') AND ( tbMaster.MiddleName like @PxMiddleName + '%') AND ( @SearchType = 'A' OR ( @SearchType = 'B' AND tbPatient.DcrDate IS NULL ) OR ( @SearchType = 'C' AND NOT tbPatient.DcrDate IS NULL and (tbPatient.DcrDate between @DateToday and @DateToday + ' 23:59:59.99' or @CurrentDischarges = 'N') ) ) ORDER BY tbPatient.IDNum ELSE IF LEN(@PXFIRSTNAME) > 0 SELECT Case When tbPatient.DcrDate is Null Then '' Else '*' End + Case When tbPatient2.Confidential = 1 Then '!' Else '' End + Case When tbPatient.BillingDate is Null Then '' Else 'B' End , tbPatient.IDNum as [Adm.#], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.LastName) Else tbMaster.LastName End AS [Last Name], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.FirstName) Else tbmaster.FirstName End AS [First Name], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.MiddleName) Else tbmaster.MiddleName End AS [Middle Name], tbPatient.RoomID AS Room, tbMaster.BirthDate AS Birth, tbMaster.Sex, tbPatient.AdmDate AS [Adm.Date], tbPatient.DcrDate AS [Dcr.Date], tbPatient.HospNum AS [Hosp.#], rTrim(tbDoctor.LastName) + ', ' + rTrim(tbDoctor.FirstName) As [Attending Doctor], tbPatient.BillingDate as [Billing Date], Case when isnull(DN.IDNum,'') = '' then '0' else case when NoticeDate is null or DN.RecordStatus = 'R' then '0' else '1' end End MayGoHome FROM tbPatient tbPatient Inner Join tbMaster tbMaster on tbPatient.HospNum = tbMaster.HospNum left outer join Build_File..tbCoDoctor AS tbDoctor ON tbPatient.AttendingDr1 = tbDoctor.DoctorID left outer join Build_File..tbCoRoom Room On tbPatient.RoomID = Room.RoomID LEft Outer Join tbPatient2 tbPatient2 On tbPatient.IDNum = tbPatient2.IDNum LeFt Outer Join Station..tbNurseDischargeNotice DN on tbPatient.IDNum = DN.IDNum WHERE (Room.StationID = @StationID or @StationID = '') and (tbPatient.ServiceID = @ServiceID or @ServiceID = '' ) and (Room.AccomodationID = @AccomodationID or @AccomodationID = '') and ( tbMaster.Lastname like @PxLastName + '%' or tbMaster.Lastname like @PxLastName + 'A%' ) AND ( tbMaster.FirstName like @PxFirstName + '%') AND ( @SearchType = 'A' OR ( @SearchType = 'B' AND tbPatient.DcrDate IS NULL ) OR ( @SearchType = 'C' AND NOT tbPatient.DcrDate IS NULL and (tbPatient.DcrDate between @DateToday and @DateToday + ' 23:59:59.99' or @CurrentDischarges = 'N') ) ) ORDER BY tbPatient.IDNum ELSE IF LEN(@PXLASTNAME) > 0 SELECT Case When tbPatient.DcrDate is Null Then '' Else '*' End + Case When tbPatient2.Confidential = 1 Then '!' Else '' End + Case When tbPatient.BillingDate is Null Then '' Else 'B' End , tbPatient.IDNum as [Adm.#], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.LastName) Else tbMaster.LastName End AS [Last Name], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.FirstName) Else tbmaster.FirstName End AS [First Name], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.MiddleName) Else tbmaster.MiddleName End AS [Middle Name], tbPatient.RoomID AS Room, tbMaster.BirthDate AS Birth, tbMaster.Sex, tbPatient.AdmDate AS [Adm.Date], tbPatient.DcrDate AS [Dcr.Date], tbPatient.HospNum AS [Hosp.#], rTrim(tbDoctor.LastName) + ', ' + rTrim(tbDoctor.FirstName) As [Attending Doctor], tbPatient.BillingDate as [Billing Date], Case when isnull(DN.IDNum,'') = '' then '0' else case when NoticeDate is null or DN.RecordStatus = 'R' then '0' else '1' end End MayGoHome FROM tbPatient tbPatient Inner Join tbMaster tbMaster on tbPatient.HospNum = tbMaster.HospNum left outer join Build_File..tbCoDoctor AS tbDoctor ON tbPatient.AttendingDr1 = tbDoctor.DoctorID left outer join Build_File..tbCoRoom Room On tbPatient.RoomID = Room.RoomID LEft Outer Join tbPatient2 tbPatient2 On tbPatient.IDNum = tbPatient2.IDNum LeFt Outer Join Station..tbNurseDischargeNotice DN on tbPatient.IDNum = DN.IDNum WHERE (Room.StationID = @StationID or @StationID = '') and (tbPatient.ServiceID = @ServiceID or @ServiceID = '' ) and (Room.AccomodationID = @AccomodationID or @AccomodationID = '') and ( tbMaster.Lastname like @PxLastName + '%' or tbMaster.Lastname like @PxLastName + 'A%' ) AND ( @SearchType = 'A' OR ( @SearchType = 'B' AND tbPatient.DcrDate IS NULL ) OR ( @SearchType = 'C' AND NOT tbPatient.DcrDate IS NULL and (tbPatient.DcrDate between @DateToday and @DateToday + ' 23:59:59.99' or @CurrentDischarges = 'N') ) ) ORDER BY tbPatient.IDNum ELSE IF LEN(@HOSPNUM) > 0 SELECT Case When tbPatient.DcrDate is Null Then '' Else '*' End + Case When tbPatient2.Confidential = 1 Then '!' Else '' End + Case When tbPatient.BillingDate is Null Then '' Else 'B' End , tbPatient.IDNum as [Adm.#], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.LastName) Else tbMaster.LastName End AS [Last Name], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.FirstName) Else tbmaster.FirstName End AS [First Name], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.MiddleName) Else tbmaster.MiddleName End AS [Middle Name], tbPatient.RoomID AS Room, tbMaster.BirthDate AS Birth, tbMaster.Sex, tbPatient.AdmDate AS [Adm.Date], tbPatient.DcrDate AS [Dcr.Date], tbPatient.HospNum AS [Hosp.#], rTrim(tbDoctor.LastName) + ', ' + rTrim(tbDoctor.FirstName) As [Attending Doctor], tbPatient.BillingDate as [Billing Date], Case when isnull(DN.IDNum,'') = '' then '0' else case when NoticeDate is null or DN.RecordStatus = 'R' then '0' else '1' end End MayGoHome FROM tbPatient tbPatient Inner Join tbMaster tbMaster on tbPatient.HospNum = tbMaster.HospNum left outer join Build_File..tbCoDoctor AS tbDoctor ON tbPatient.AttendingDr1 = tbDoctor.DoctorID left outer join Build_File..tbCoRoom Room On tbPatient.RoomID = Room.RoomID LEft Outer Join tbPatient2 tbPatient2 On tbPatient.IDNum = tbPatient2.IDNum LeFt Outer Join Station..tbNurseDischargeNotice DN on tbPatient.IDNum = DN.IDNum WHERE (Room.StationID = @StationID or @StationID = '') and (tbPatient.ServiceID = @ServiceID or @ServiceID = '' ) and (Room.AccomodationID = @AccomodationID or @AccomodationID = '') and (tbPatient.Hospnum = @HospNum) AND ( @SearchType = 'A' OR ( @SearchType = 'B' AND tbPatient.DcrDate IS NULL ) OR ( @SearchType = 'C' AND NOT tbPatient.DcrDate IS NULL and (tbPatient.DcrDate between @DateToday and @DateToday + ' 23:59:59.99' or @CurrentDischarges = 'N') ) ) ORDER BY tbPatient.IDNum ELSE IF LEN(@IDNUM) > 0 SELECT Case When tbPatient.DcrDate is Null Then '' Else '*' End + Case When tbPatient2.Confidential = 1 Then '!' Else '' End + Case When tbPatient.BillingDate is Null Then '' Else 'B' End , tbPatient.IDNum as [Adm.#], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.LastName) Else tbMaster.LastName End AS [Last Name], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.FirstName) Else tbmaster.FirstName End AS [First Name], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.MiddleName) Else tbmaster.MiddleName End AS [Middle Name], tbPatient.RoomID AS Room, tbMaster.BirthDate AS Birth, tbMaster.Sex, tbPatient.AdmDate AS [Adm.Date], tbPatient.DcrDate AS [Dcr.Date], tbPatient.HospNum AS [Hosp.#], rTrim(tbDoctor.LastName) + ', ' + rTrim(tbDoctor.FirstName) As [Attending Doctor], tbPatient.BillingDate as [Billing Date], Case when isnull(DN.IDNum,'') = '' then '0' else case when NoticeDate is null or DN.RecordStatus = 'R' then '0' else '1' end End MayGoHome FROM tbPatient tbPatient Inner Join tbMaster tbMaster on tbPatient.HospNum = tbMaster.HospNum left outer join Build_File..tbCoDoctor AS tbDoctor ON tbPatient.AttendingDr1 = tbDoctor.DoctorID left outer join Build_File..tbCoRoom Room On tbPatient.RoomID = Room.RoomID LEft Outer Join tbPatient2 tbPatient2 On tbPatient.IDNum = tbPatient2.IDNum LeFt Outer Join Station..tbNurseDischargeNotice DN on tbPatient.IDNum = DN.IDNum WHERE (Room.StationID = @StationID or @StationID = '') and (tbPatient.ServiceID = @ServiceID or @ServiceID = '' ) and (Room.AccomodationID = @AccomodationID or @AccomodationID = '') and (tbPatient.IDNum = @IDNum) AND ( @SearchType = 'A' OR ( @SearchType = 'B' AND tbPatient.DcrDate IS NULL ) OR ( @SearchType = 'C' AND NOT tbPatient.DcrDate IS NULL and (tbPatient.DcrDate between @DateToday and @DateToday + ' 23:59:59.99' or @CurrentDischarges = 'N') ) ) ORDER BY tbPatient.IDNum ELSE IF LEN(@CARDNUM) > 0 SELECT Case When tbPatient.DcrDate is Null Then '' Else '*' End + Case When tbPatient2.Confidential = 1 Then '!' Else '' End + Case When tbPatient.BillingDate is Null Then '' Else 'B' End , tbPatient.IDNum as [Adm.#], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.LastName) Else tbMaster.LastName End AS [Last Name], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.FirstName) Else tbmaster.FirstName End AS [First Name], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.MiddleName) Else tbmaster.MiddleName End AS [Middle Name], tbPatient.RoomID AS Room, tbMaster.BirthDate AS Birth, tbMaster.Sex, tbPatient.AdmDate AS [Adm.Date], tbPatient.DcrDate AS [Dcr.Date], tbPatient.HospNum AS [Hosp.#], rTrim(tbDoctor.LastName) + ', ' + rTrim(tbDoctor.FirstName) As [Attending Doctor], tbPatient.BillingDate as [Billing Date], Case when isnull(DN.IDNum,'') = '' then '0' else case when NoticeDate is null or DN.RecordStatus = 'R' then '0' else '1' end End MayGoHome FROM tbPatient tbPatient Inner Join tbMaster tbMaster on tbPatient.HospNum = tbMaster.HospNum left outer join Build_File..tbCoDoctor AS tbDoctor ON tbPatient.AttendingDr1 = tbDoctor.DoctorID left outer join Build_File..tbCoRoom Room On tbPatient.RoomID = Room.RoomID LEft Outer Join tbPatient2 tbPatient2 On tbPatient.IDNum = tbPatient2.IDNum LeFt Outer Join Station..tbNurseDischargeNotice DN on tbPatient.IDNum = DN.IDNum WHERE (Room.StationID = @StationID or @StationID = '') and (tbPatient.ServiceID = @ServiceID or @ServiceID = '' ) and (Room.AccomodationID = @AccomodationID or @AccomodationID = '') and (tbMaster.CardNumber = @CARDNUM) AND ( @SearchType = 'A' OR ( @SearchType = 'B' AND tbPatient.DcrDate IS NULL ) OR ( @SearchType = 'C' AND NOT tbPatient.DcrDate IS NULL and (tbPatient.DcrDate between @DateToday and @DateToday + ' 23:59:59.99' or @CurrentDischarges = 'N') ) ) ORDER BY tbPatient.IDNum ELSE IF LEN(@ROOMID) > 0 SELECT Case When tbPatient.DcrDate is Null Then '' Else '*' End + Case When tbPatient2.Confidential = 1 Then '!' Else '' End + Case When tbPatient.BillingDate is Null Then '' Else 'B' End , tbPatient.IDNum as [Adm.#], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.LastName) Else tbMaster.LastName End AS [Last Name], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.FirstName) Else tbmaster.FirstName End AS [First Name], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.MiddleName) Else tbmaster.MiddleName End AS [Middle Name], tbPatient.RoomID AS Room, tbMaster.BirthDate AS Birth, tbMaster.Sex, tbPatient.AdmDate AS [Adm.Date], tbPatient.DcrDate AS [Dcr.Date], tbPatient.HospNum AS [Hosp.#], rTrim(tbDoctor.LastName) + ', ' + rTrim(tbDoctor.FirstName) As [Attending Doctor], tbPatient.BillingDate as [Billing Date], Case when isnull(DN.IDNum,'') = '' then '0' else case when NoticeDate is null or DN.RecordStatus = 'R' then '0' else '1' end End MayGoHome FROM tbPatient tbPatient Inner Join tbMaster tbMaster on tbPatient.HospNum = tbMaster.HospNum left outer join Build_File..tbCoDoctor AS tbDoctor ON tbPatient.AttendingDr1 = tbDoctor.DoctorID left outer join Build_File..tbCoRoom Room On tbPatient.RoomID = Room.RoomID LEft Outer Join tbPatient2 tbPatient2 On tbPatient.IDNum = tbPatient2.IDNum LeFt Outer Join Station..tbNurseDischargeNotice DN on tbPatient.IDNum = DN.IDNum WHERE (Room.StationID = @StationID or @StationID = '') and (tbPatient.ServiceID = @ServiceID or @ServiceID = '' ) and (Room.AccomodationID = @AccomodationID or @AccomodationID = '') and (tbPatient.RoomID = @RoomID) AND ( @SearchType = 'A' OR ( @SearchType = 'B' AND tbPatient.DcrDate IS NULL ) OR ( @SearchType = 'C' AND NOT tbPatient.DcrDate IS NULL and (tbPatient.DcrDate between @DateToday and @DateToday + ' 23:59:59.99' or @CurrentDischarges = 'N') ) ) ORDER BY tbPatient.IDNum ELSE --Default SELECT Case When tbPatient.DcrDate is Null Then '' Else '*' End + Case When tbPatient2.Confidential = 1 Then '!' Else '' End + Case When tbPatient.BillingDate is Null Then '' Else 'B' End , tbPatient.IDNum as [Adm.#], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.LastName) Else tbMaster.LastName End AS [Last Name], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.FirstName) Else tbmaster.FirstName End AS [First Name], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.MiddleName) Else tbmaster.MiddleName End AS [Middle Name], tbPatient.RoomID AS Room, tbMaster.BirthDate AS Birth, tbMaster.Sex, tbPatient.AdmDate AS [Adm.Date], tbPatient.DcrDate AS [Dcr.Date], tbPatient.HospNum AS [Hosp.#], rTrim(tbDoctor.LastName) + ', ' + rTrim(tbDoctor.FirstName) As [Attending Doctor], tbPatient.BillingDate as [Billing Date], Case when isnull(DN.IDNum,'') = '' then '0' else case when NoticeDate is null or DN.RecordStatus = 'R' then '0' else '1' end End MayGoHome FROM tbPatient tbPatient Inner Join tbMaster tbMaster on tbPatient.HospNum = tbMaster.HospNum left outer join Build_File..tbCoDoctor AS tbDoctor ON tbPatient.AttendingDr1 = tbDoctor.DoctorID left outer join Build_File..tbCoRoom Room On tbPatient.RoomID = Room.RoomID LEft Outer Join tbPatient2 tbPatient2 On tbPatient.IDNum = tbPatient2.IDNum LeFt Outer Join Station..tbNurseDischargeNotice DN on tbPatient.IDNum = DN.IDNum WHERE (Room.StationID = @StationID or @StationID = '') and (tbPatient.ServiceID = @ServiceID or @ServiceID = '' ) and (Room.AccomodationID = @AccomodationID or @AccomodationID = '') and ( @SearchType = 'A' OR ( @SearchType = 'B' AND tbPatient.DcrDate IS NULL ) OR ( @SearchType = 'C' AND NOT tbPatient.DcrDate IS NULL and (tbPatient.DcrDate between @DateToday and @DateToday + ' 23:59:59.99' or @CurrentDischarges = 'N') ) ) ORDER BY tbPatient.IDNum end else begin If Len(@LastName) > 0 BEGIN Set @PxName = @LastName ; Set @Pos = PATINDEX('%,%',@PxName) ; IF @Pos > 0 BEGIN Set @PxLastName = RTrim(SubString( @PxName, 1, @POS - 1 )) ; Set @PxName = LTrim(RTRim(SubString( @PxName, @Pos + 1, 50 ))) ; Set @Pos = PatIndex('%,%',@PxName) ; If @Pos > 0 BEGIN Set @PxFirstName = RTRim(SubString( @PxName, 1, @Pos -1 )) ; Set @PxMiddleName = RTrim(LTrim( SubString( @PxName, @Pos + 1,50 ) )) ; END ; Else BEGIN Set @PxFirstName = @PxName ; END ; END ; ELSE BEGIN Set @PxLastName = RTRim(LTrim(@LastName)) ; END ; END ; IF LEN(@PXMIDDLENAME) > 0 SELECT Case When tbPatient.DcrDate is Null Then '' Else '*' End + Case When tbPatient2.Confidential = 1 Then '!' Else '' End + Case When tbPatient.BillingDate is Null Then '' Else 'B' End , tbPatient.IDNum as [Adm.#], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.LastName) Else tbMaster.LastName End AS [Last Name], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.FirstName) Else tbmaster.FirstName End AS [First Name], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.MiddleName) Else tbmaster.MiddleName End AS [Middle Name], tbPatient.RoomID AS Room, tbMaster.BirthDate AS Birth, tbMaster.Sex, tbPatient.AdmDate AS [Adm.Date], tbPatient.DcrDate AS [Dcr.Date], tbPatient.HospNum AS [Hosp.#], rTrim(tbDoctor.LastName) + ', ' + rTrim(tbDoctor.FirstName) As [Attending Doctor], tbPatient.BillingDate as [Billing Date], Case when isnull(DN.IDNum,'') = '' then '0' else case when NoticeDate is null or DN.RecordStatus = 'R' then '0' else '1' end End MayGoHome FROM tbPatient tbPatient Inner Join tbMaster tbMaster on tbPatient.HospNum = tbMaster.HospNum left outer join Build_File..tbCoDoctor AS tbDoctor ON tbPatient.AttendingDr1 = tbDoctor.DoctorID left outer join Build_File..tbCoRoom Room On tbPatient.RoomID = Room.RoomID LEft Outer Join tbPatient2 tbPatient2 On tbPatient.IDNum = tbPatient2.IDNum LeFt Outer Join Station..tbNurseDischargeNotice DN on tbPatient.IDNum = DN.IDNum WHERE (Room.StationID = @StationID or @StationID = '') and (tbPatient.ServiceID = @ServiceID or @ServiceID = '' ) and (Room.AccomodationID = @AccomodationID or @AccomodationID = '') and ( tbMaster.Lastname like @PxLastName + '%' or tbMaster.Lastname like @PxLastName + 'A%' ) AND ( tbMaster.FirstName like @PxFirstName + '%') AND ( tbMaster.MiddleName like @PxMiddleName + '%') AND ( @SearchType = 'A' OR ( @SearchType = 'B' AND tbPatient.DcrDate IS NULL ) OR ( @SearchType = 'C' AND NOT tbPatient.DcrDate IS NULL and (tbPatient.DcrDate between @DateToday and @DateToday + ' 23:59:59.99' or @CurrentDischarges = 'N') ) ) ORDER BY tbPatient.RoomID ELSE IF LEN(@PXFIRSTNAME) > 0 SELECT Case When tbPatient.DcrDate is Null Then '' Else '*' End + Case When tbPatient2.Confidential = 1 Then '!' Else '' End + Case When tbPatient.BillingDate is Null Then '' Else 'B' End , tbPatient.IDNum as [Adm.#], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.LastName) Else tbMaster.LastName End AS [Last Name], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.FirstName) Else tbmaster.FirstName End AS [First Name], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.MiddleName) Else tbmaster.MiddleName End AS [Middle Name], tbPatient.RoomID AS Room, tbMaster.BirthDate AS Birth, tbMaster.Sex, tbPatient.AdmDate AS [Adm.Date], tbPatient.DcrDate AS [Dcr.Date], tbPatient.HospNum AS [Hosp.#], rTrim(tbDoctor.LastName) + ', ' + rTrim(tbDoctor.FirstName) As [Attending Doctor], tbPatient.BillingDate as [Billing Date], Case when isnull(DN.IDNum,'') = '' then '0' else case when NoticeDate is null or DN.RecordStatus = 'R' then '0' else '1' end End MayGoHome FROM tbPatient tbPatient Inner Join tbMaster tbMaster on tbPatient.HospNum = tbMaster.HospNum left outer join Build_File..tbCoDoctor AS tbDoctor ON tbPatient.AttendingDr1 = tbDoctor.DoctorID left outer join Build_File..tbCoRoom Room On tbPatient.RoomID = Room.RoomID LEft Outer Join tbPatient2 tbPatient2 On tbPatient.IDNum = tbPatient2.IDNum LeFt Outer Join Station..tbNurseDischargeNotice DN on tbPatient.IDNum = DN.IDNum WHERE (Room.StationID = @StationID or @StationID = '') and (tbPatient.ServiceID = @ServiceID or @ServiceID = '' ) and (Room.AccomodationID = @AccomodationID or @AccomodationID = '') and ( tbMaster.Lastname like @PxLastName + '%' or tbMaster.Lastname like @PxLastName + 'A%' ) AND ( tbMaster.FirstName like @PxFirstName + '%') AND ( @SearchType = 'A' OR ( @SearchType = 'B' AND tbPatient.DcrDate IS NULL ) OR ( @SearchType = 'C' AND NOT tbPatient.DcrDate IS NULL and (tbPatient.DcrDate between @DateToday and @DateToday + ' 23:59:59.99' or @CurrentDischarges = 'N') ) ) ORDER BY tbPatient.RoomID ELSE IF LEN(@PXLASTNAME) > 0 SELECT Case When tbPatient.DcrDate is Null Then '' Else '*' End + Case When tbPatient2.Confidential = 1 Then '!' Else '' End + Case When tbPatient.BillingDate is Null Then '' Else 'B' End , tbPatient.IDNum as [Adm.#], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.LastName) Else tbMaster.LastName End AS [Last Name], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.FirstName) Else tbmaster.FirstName End AS [First Name], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.MiddleName) Else tbmaster.MiddleName End AS [Middle Name], tbPatient.RoomID AS Room, tbMaster.BirthDate AS Birth, tbMaster.Sex, tbPatient.AdmDate AS [Adm.Date], tbPatient.DcrDate AS [Dcr.Date], tbPatient.HospNum AS [Hosp.#], rTrim(tbDoctor.LastName) + ', ' + rTrim(tbDoctor.FirstName) As [Attending Doctor], tbPatient.BillingDate as [Billing Date], Case when isnull(DN.IDNum,'') = '' then '0' else case when NoticeDate is null or DN.RecordStatus = 'R' then '0' else '1' end End MayGoHome FROM tbPatient tbPatient Inner Join tbMaster tbMaster on tbPatient.HospNum = tbMaster.HospNum left outer join Build_File..tbCoDoctor AS tbDoctor ON tbPatient.AttendingDr1 = tbDoctor.DoctorID left outer join Build_File..tbCoRoom Room On tbPatient.RoomID = Room.RoomID LEft Outer Join tbPatient2 tbPatient2 On tbPatient.IDNum = tbPatient2.IDNum LeFt Outer Join Station..tbNurseDischargeNotice DN on tbPatient.IDNum = DN.IDNum WHERE (Room.StationID = @StationID or @StationID = '') and (tbPatient.ServiceID = @ServiceID or @ServiceID = '' ) and (Room.AccomodationID = @AccomodationID or @AccomodationID = '') and ( tbMaster.Lastname like @PxLastName + '%' or tbMaster.Lastname like @PxLastName + 'A%' ) AND ( @SearchType = 'A' OR ( @SearchType = 'B' AND tbPatient.DcrDate IS NULL ) OR ( @SearchType = 'C' AND NOT tbPatient.DcrDate IS NULL and (tbPatient.DcrDate between @DateToday and @DateToday + ' 23:59:59.99' or @CurrentDischarges = 'N') ) ) ORDER BY tbPatient.IDNum ELSE IF LEN(@HOSPNUM) > 0 SELECT Case When tbPatient.DcrDate is Null Then '' Else '*' End + Case When tbPatient2.Confidential = 1 Then '!' Else '' End + Case When tbPatient.BillingDate is Null Then '' Else 'B' End , tbPatient.IDNum as [Adm.#], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.LastName) Else tbMaster.LastName End AS [Last Name], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.FirstName) Else tbmaster.FirstName End AS [First Name], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.MiddleName) Else tbmaster.MiddleName End AS [Middle Name], tbPatient.RoomID AS Room, tbMaster.BirthDate AS Birth, tbMaster.Sex, tbPatient.AdmDate AS [Adm.Date], tbPatient.DcrDate AS [Dcr.Date], tbPatient.HospNum AS [Hosp.#], rTrim(tbDoctor.LastName) + ', ' + rTrim(tbDoctor.FirstName) As [Attending Doctor], tbPatient.BillingDate as [Billing Date], Case when isnull(DN.IDNum,'') = '' then '0' else case when NoticeDate is null or DN.RecordStatus = 'R' then '0' else '1' end End MayGoHome FROM tbPatient tbPatient Inner Join tbMaster tbMaster on tbPatient.HospNum = tbMaster.HospNum left outer join Build_File..tbCoDoctor AS tbDoctor ON tbPatient.AttendingDr1 = tbDoctor.DoctorID left outer join Build_File..tbCoRoom Room On tbPatient.RoomID = Room.RoomID LEft Outer Join tbPatient2 tbPatient2 On tbPatient.IDNum = tbPatient2.IDNum LeFt Outer Join Station..tbNurseDischargeNotice DN on tbPatient.IDNum = DN.IDNum WHERE (Room.StationID = @StationID or @StationID = '') and (tbPatient.ServiceID = @ServiceID or @ServiceID = '' ) and (Room.AccomodationID = @AccomodationID or @AccomodationID = '') and (tbPatient.Hospnum = @HospNum) AND ( @SearchType = 'A' OR ( @SearchType = 'B' AND tbPatient.DcrDate IS NULL ) OR ( @SearchType = 'C' AND NOT tbPatient.DcrDate IS NULL and (tbPatient.DcrDate between @DateToday and @DateToday + ' 23:59:59.99' or @CurrentDischarges = 'N') ) ) ORDER BY tbPatient.RoomID ELSE IF LEN(@IDNUM) > 0 SELECT Case When tbPatient.DcrDate is Null Then '' Else '*' End + Case When tbPatient2.Confidential = 1 Then '!' Else '' End + Case When tbPatient.BillingDate is Null Then '' Else 'B' End , tbPatient.IDNum as [Adm.#], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.LastName) Else tbMaster.LastName End AS [Last Name], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.FirstName) Else tbmaster.FirstName End AS [First Name], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.MiddleName) Else tbmaster.MiddleName End AS [Middle Name], tbPatient.RoomID AS Room, tbMaster.BirthDate AS Birth, tbMaster.Sex, tbPatient.AdmDate AS [Adm.Date], tbPatient.DcrDate AS [Dcr.Date], tbPatient.HospNum AS [Hosp.#], rTrim(tbDoctor.LastName) + ', ' + rTrim(tbDoctor.FirstName) As [Attending Doctor], tbPatient.BillingDate as [Billing Date], Case when isnull(DN.IDNum,'') = '' then '0' else case when NoticeDate is null or DN.RecordStatus = 'R' then '0' else '1' end End MayGoHome FROM tbPatient tbPatient Inner Join tbMaster tbMaster on tbPatient.HospNum = tbMaster.HospNum left outer join Build_File..tbCoDoctor AS tbDoctor ON tbPatient.AttendingDr1 = tbDoctor.DoctorID left outer join Build_File..tbCoRoom Room On tbPatient.RoomID = Room.RoomID LEft Outer Join tbPatient2 tbPatient2 On tbPatient.IDNum = tbPatient2.IDNum LeFt Outer Join Station..tbNurseDischargeNotice DN on tbPatient.IDNum = DN.IDNum WHERE (Room.StationID = @StationID or @StationID = '') and (tbPatient.ServiceID = @ServiceID or @ServiceID = '' ) and (Room.AccomodationID = @AccomodationID or @AccomodationID = '') and (tbPatient.IDNum = @IDNum) AND ( @SearchType = 'A' OR ( @SearchType = 'B' AND tbPatient.DcrDate IS NULL ) OR ( @SearchType = 'C' AND NOT tbPatient.DcrDate IS NULL and (tbPatient.DcrDate between @DateToday and @DateToday + ' 23:59:59.99' or @CurrentDischarges = 'N') ) ) ORDER BY tbPatient.RoomID ELSE IF LEN(@CARDNUM) > 0 SELECT Case When tbPatient.DcrDate is Null Then '' Else '*' End + Case When tbPatient2.Confidential = 1 Then '!' Else '' End + Case When tbPatient.BillingDate is Null Then '' Else 'B' End , tbPatient.IDNum as [Adm.#], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.LastName) Else tbMaster.LastName End AS [Last Name], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.FirstName) Else tbmaster.FirstName End AS [First Name], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.MiddleName) Else tbmaster.MiddleName End AS [Middle Name], tbPatient.RoomID AS Room, tbMaster.BirthDate AS Birth, tbMaster.Sex, tbPatient.AdmDate AS [Adm.Date], tbPatient.DcrDate AS [Dcr.Date], tbPatient.HospNum AS [Hosp.#], rTrim(tbDoctor.LastName) + ', ' + rTrim(tbDoctor.FirstName) As [Attending Doctor], tbPatient.BillingDate as [Billing Date], Case when isnull(DN.IDNum,'') = '' then '0' else case when NoticeDate is null or DN.RecordStatus = 'R' then '0' else '1' end End MayGoHome FROM tbPatient tbPatient Inner Join tbMaster tbMaster on tbPatient.HospNum = tbMaster.HospNum left outer join Build_File..tbCoDoctor AS tbDoctor ON tbPatient.AttendingDr1 = tbDoctor.DoctorID left outer join Build_File..tbCoRoom Room On tbPatient.RoomID = Room.RoomID LEft Outer Join tbPatient2 tbPatient2 On tbPatient.IDNum = tbPatient2.IDNum LeFt Outer Join Station..tbNurseDischargeNotice DN on tbPatient.IDNum = DN.IDNum WHERE (Room.StationID = @StationID or @StationID = '') and (tbPatient.ServiceID = @ServiceID or @ServiceID = '' ) and (Room.AccomodationID = @AccomodationID or @AccomodationID = '') and (tbMaster.CardNumber = @CARDNUM) AND ( @SearchType = 'A' OR ( @SearchType = 'B' AND tbPatient.DcrDate IS NULL ) OR ( @SearchType = 'C' AND NOT tbPatient.DcrDate IS NULL and (tbPatient.DcrDate between @DateToday and @DateToday + ' 23:59:59.99' or @CurrentDischarges = 'N') ) ) ORDER BY tbPatient.RoomID ELSE IF LEN(@ROOMID) > 0 SELECT Case When tbPatient.DcrDate is Null Then '' Else '*' End + Case When tbPatient2.Confidential = 1 Then '!' Else '' End + Case When tbPatient.BillingDate is Null Then '' Else 'B' End , tbPatient.IDNum as [Adm.#], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.LastName) Else tbMaster.LastName End AS [Last Name], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.FirstName) Else tbmaster.FirstName End AS [First Name], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.MiddleName) Else tbmaster.MiddleName End AS [Middle Name], tbPatient.RoomID AS Room, tbMaster.BirthDate AS Birth, tbMaster.Sex, tbPatient.AdmDate AS [Adm.Date], tbPatient.DcrDate AS [Dcr.Date], tbPatient.HospNum AS [Hosp.#], rTrim(tbDoctor.LastName) + ', ' + rTrim(tbDoctor.FirstName) As [Attending Doctor], tbPatient.BillingDate as [Billing Date], Case when isnull(DN.IDNum,'') = '' then '0' else case when NoticeDate is null or DN.RecordStatus = 'R' then '0' else '1' end End MayGoHome FROM tbPatient tbPatient Inner Join tbMaster tbMaster on tbPatient.HospNum = tbMaster.HospNum left outer join Build_File..tbCoDoctor AS tbDoctor ON tbPatient.AttendingDr1 = tbDoctor.DoctorID left outer join Build_File..tbCoRoom Room On tbPatient.RoomID = Room.RoomID LEft Outer Join tbPatient2 tbPatient2 On tbPatient.IDNum = tbPatient2.IDNum LeFt Outer Join Station..tbNurseDischargeNotice DN on tbPatient.IDNum = DN.IDNum WHERE (Room.StationID = @StationID or @StationID = '') and (tbPatient.ServiceID = @ServiceID or @ServiceID = '' ) and (Room.AccomodationID = @AccomodationID or @AccomodationID = '') and (tbPatient.RoomID = @RoomID) AND ( @SearchType = 'A' OR ( @SearchType = 'B' AND tbPatient.DcrDate IS NULL ) OR ( @SearchType = 'C' AND NOT tbPatient.DcrDate IS NULL and (tbPatient.DcrDate between @DateToday and @DateToday + ' 23:59:59.99' or @CurrentDischarges = 'N') ) ) ORDER BY tbPatient.RoomID ELSE --Default SELECT Case When tbPatient.DcrDate is Null Then '' Else '*' End + Case When tbPatient2.Confidential = 1 Then '!' Else '' End + Case When tbPatient.BillingDate is Null Then '' Else 'B' End , tbPatient.IDNum as [Adm.#], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.LastName) Else tbMaster.LastName End AS [Last Name], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.FirstName) Else tbmaster.FirstName End AS [First Name], Case tbPatient2.Confidential When '1' then dbo.ReplaceCharsAsterisks(tbMaster.MiddleName) Else tbmaster.MiddleName End AS [Middle Name], tbPatient.RoomID AS Room, tbMaster.BirthDate AS Birth, tbMaster.Sex, tbPatient.AdmDate AS [Adm.Date], tbPatient.DcrDate AS [Dcr.Date], tbPatient.HospNum AS [Hosp.#], rTrim(tbDoctor.LastName) + ', ' + rTrim(tbDoctor.FirstName) As [Attending Doctor], tbPatient.BillingDate as [Billing Date], Case when isnull(DN.IDNum,'') = '' then '0' else case when NoticeDate is null or DN.RecordStatus = 'R' then '0' else '1' end End MayGoHome FROM tbPatient tbPatient Inner Join tbMaster tbMaster on tbPatient.HospNum = tbMaster.HospNum left outer join Build_File..tbCoDoctor AS tbDoctor ON tbPatient.AttendingDr1 = tbDoctor.DoctorID left outer join Build_File..tbCoRoom Room On tbPatient.RoomID = Room.RoomID LEft Outer Join tbPatient2 tbPatient2 On tbPatient.IDNum = tbPatient2.IDNum LeFt Outer Join Station..tbNurseDischargeNotice DN on tbPatient.IDNum = DN.IDNum WHERE (Room.StationID = @StationID or @StationID = '') and (tbPatient.ServiceID = @ServiceID or @ServiceID = '' ) and (Room.AccomodationID = @AccomodationID or @AccomodationID = '') and ( @SearchType = 'A' OR ( @SearchType = 'B' AND tbPatient.DcrDate IS NULL ) OR ( @SearchType = 'C' AND NOT tbPatient.DcrDate IS NULL and (tbPatient.DcrDate between @DateToday and @DateToday + ' 23:59:59.99' or @CurrentDischarges = 'N') ) ) ORDER BY tbPatient.RoomID end