hexOffender
Programmer
I wrote this function to calculate the turnaround time for a dictation. When I run just the query, the turnaround time gets calculated just fine. But when I try to call the function, with the same parameters, it returns NULL.
Here is the Function:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[Turnaround](@VisitID varchar(15),@QueryID1 varchar(10),@QueryID2 varchar(10),@ReportName varchar(20))
RETURNS int
AS
BEGIN
--Declare @Turnaround minute
return(SELECT distinct DATEDIFF(minute,(SELECT distinct Response
FROM NurQueryResults AS NQR
INNER JOIN AbstractData AS AD
ON NQR.SourceID = AD.SourceID AND
NQR.VisitID = AD.VisitID AND
--AD.UnitNumber = @UnitNum
NQR.VisitID=@VisitID
WHERE NQR.QueryID = @QueryID1)+ ' '+ --Parameter QueryID1 -NUR.BDAY or NUR.ORPDTE
(SELECT STUFF((SELECT distinct Response
FROM NurQueryResults AS NQR
INNER JOIN AbstractData AS AD
ON NQR.SourceID = AD.SourceID AND
NQR.VisitID = AD.VisitID AND
--AD.UnitNumber = @UnitNum
NQR.VisitID=@VisitID
WHERE NQR.QueryID = @QueryID2),3,0,':')+':00.000'),OeResults1.DictDateTime)--Parameter QueryID2
--NUR.TIME or NUR.ORTRR
AS Turnaround --BirthDateTime , OeResults1.DictDateTime
From NurQueryResults AS NQR
Inner Join AbstractData as AD on NQR.SourceID = AD.SourceID and NQR.VisitID = AD.VisitID
Inner Join OeResults ON AD.VisitID=OeResults.PatientVisitID
Inner Join OeResults1 ON OeResults1.ResultID=OeResults.ResultID
WHERE (NQR.VisitID=@VisitID)and OeResults.ReportName=@ReportName)
END
Here is the main part of the function:
SELECT distinct DATEDIFF(minute,(SELECT distinct Response
FROM NurQueryResults AS NQR
INNER JOIN AbstractData AS AD
ON NQR.SourceID = AD.SourceID AND
NQR.VisitID = AD.VisitID AND
--AD.UnitNumber = @UnitNum
NQR.VisitID='V1002108511'
WHERE NQR.QueryID = 'NUR.BDAY')+ ' '+ --Parameter QueryID1 -NUR.BDAY or NUR.ORPDTE
(SELECT STUFF((SELECT distinct Response
FROM NurQueryResults AS NQR
INNER JOIN AbstractData AS AD
ON NQR.SourceID = AD.SourceID AND
NQR.VisitID = AD.VisitID AND
--AD.UnitNumber = @UnitNum
NQR.VisitID='V1002108511'
WHERE NQR.QueryID ='NUR.TIME'),3,0,':')+':00.000'),OeResults1.DictDateTime)--Parameter QueryID2
--NUR.TIME or NUR.ORTRR
AS Turnaround --BirthDateTime , OeResults1.DictDateTime
From NurQueryResults AS NQR
Inner Join AbstractData as AD on NQR.SourceID = AD.SourceID and NQR.VisitID = AD.VisitID
Inner Join OeResults ON AD.VisitID=OeResults.PatientVisitID
Inner Join OeResults1 ON OeResults1.ResultID=OeResults.ResultID
WHERE (NQR.VisitID='V1002108511')and OeResults.ReportName='*NEWBORN EXAMINATION*'
If I call the function say
" Select dbo.Turnaround('V1002108511','NUR.BDAY','NUR.TIME','*NEWBORN EXAMINATION*')" it will return NULL. The query for the Function works, but when I try to use the function, it only returns NULLs.
Here is the Function:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[Turnaround](@VisitID varchar(15),@QueryID1 varchar(10),@QueryID2 varchar(10),@ReportName varchar(20))
RETURNS int
AS
BEGIN
--Declare @Turnaround minute
return(SELECT distinct DATEDIFF(minute,(SELECT distinct Response
FROM NurQueryResults AS NQR
INNER JOIN AbstractData AS AD
ON NQR.SourceID = AD.SourceID AND
NQR.VisitID = AD.VisitID AND
--AD.UnitNumber = @UnitNum
NQR.VisitID=@VisitID
WHERE NQR.QueryID = @QueryID1)+ ' '+ --Parameter QueryID1 -NUR.BDAY or NUR.ORPDTE
(SELECT STUFF((SELECT distinct Response
FROM NurQueryResults AS NQR
INNER JOIN AbstractData AS AD
ON NQR.SourceID = AD.SourceID AND
NQR.VisitID = AD.VisitID AND
--AD.UnitNumber = @UnitNum
NQR.VisitID=@VisitID
WHERE NQR.QueryID = @QueryID2),3,0,':')+':00.000'),OeResults1.DictDateTime)--Parameter QueryID2
--NUR.TIME or NUR.ORTRR
AS Turnaround --BirthDateTime , OeResults1.DictDateTime
From NurQueryResults AS NQR
Inner Join AbstractData as AD on NQR.SourceID = AD.SourceID and NQR.VisitID = AD.VisitID
Inner Join OeResults ON AD.VisitID=OeResults.PatientVisitID
Inner Join OeResults1 ON OeResults1.ResultID=OeResults.ResultID
WHERE (NQR.VisitID=@VisitID)and OeResults.ReportName=@ReportName)
END
Here is the main part of the function:
SELECT distinct DATEDIFF(minute,(SELECT distinct Response
FROM NurQueryResults AS NQR
INNER JOIN AbstractData AS AD
ON NQR.SourceID = AD.SourceID AND
NQR.VisitID = AD.VisitID AND
--AD.UnitNumber = @UnitNum
NQR.VisitID='V1002108511'
WHERE NQR.QueryID = 'NUR.BDAY')+ ' '+ --Parameter QueryID1 -NUR.BDAY or NUR.ORPDTE
(SELECT STUFF((SELECT distinct Response
FROM NurQueryResults AS NQR
INNER JOIN AbstractData AS AD
ON NQR.SourceID = AD.SourceID AND
NQR.VisitID = AD.VisitID AND
--AD.UnitNumber = @UnitNum
NQR.VisitID='V1002108511'
WHERE NQR.QueryID ='NUR.TIME'),3,0,':')+':00.000'),OeResults1.DictDateTime)--Parameter QueryID2
--NUR.TIME or NUR.ORTRR
AS Turnaround --BirthDateTime , OeResults1.DictDateTime
From NurQueryResults AS NQR
Inner Join AbstractData as AD on NQR.SourceID = AD.SourceID and NQR.VisitID = AD.VisitID
Inner Join OeResults ON AD.VisitID=OeResults.PatientVisitID
Inner Join OeResults1 ON OeResults1.ResultID=OeResults.ResultID
WHERE (NQR.VisitID='V1002108511')and OeResults.ReportName='*NEWBORN EXAMINATION*'
If I call the function say
" Select dbo.Turnaround('V1002108511','NUR.BDAY','NUR.TIME','*NEWBORN EXAMINATION*')" it will return NULL. The query for the Function works, but when I try to use the function, it only returns NULLs.