Use variable parameter in Execute SQL Task multiple times SSIS - sql

hey guys so i created two variables: startdate and todate and I am attempting to pass them through my SQL query in SSIS and I have added them in my Parameter Mapping but how does SSIS which variable to use after if it sees a third question mark?
for example: so how would SSIS know to use startdate when it inserts into #multileg and not use the todate variable?
e
--Declare #StartDate date
--declare #ToDate date
--set #startdate = dateadd(dd,-10, cast(getdate() as date))
--set #ToDate = dateadd(dd,-9,cast(getdate() as date))
---SSR Table with passenger info, both APAY and PET
create table #SSRData
([ssrfl] int, [ssrcode] char(4), [ssrsequenceid] smallint, [ssrstatuscode]
char(2), [servicestartcitycode] varchar(5),
[ssrstartdate] date, [databasetimestamp] datetime, [pnrlocator] char(8),
[pnrcreatedate] date, [passengersequenceid] smallint,
[namefirst] varchar(250), [namelast] varchar(250), [frequenttravelernumber]
varchar(25)
)
insert into #ssrdata
select distinct ssrfl,
s.ssrcode,s.ssrsequenceid,s.ssrstatuscode,s.servicestartcitycode,
s.ssrstartdate, s.databasetimestamp, s.pnrlocator, s.pnrcreatedate
,s.passengersequenceid, namefirst, namelast,frequenttravelernumber
--into #SSRData
from
(select cast(ssrflightnumber as int)ssrfl,
ssrcode,ssrsequenceid,ssrstatuscode,servicestartcitycode,
ssrstartdate, pnrlocator, pnrcreatedate
,passengersequenceid,databasetimestamp from dwjetblue2.dw.resssr
where SSRCode in ('APAY', 'PETC') and PNRLocator <>'purged'
and ssrstartdate >= ?
and ssrstartdate < ?)s
inner join dw.dw.ResPassenger p
on p.pnrcreatedate=s.pnrcreatedate
and p.pnrlocator=s.pnrlocator
and p.passengersequenceid=s.passengersequenceid
inner join dwjetblue2.dw.ResPassengerFT ft
on ft.pnrcreatedate=s.pnrcreatedate
and ft.pnrlocator=s.pnrlocator
and ft.passengersequenceid=s.passengersequenceid
--MultiLeg
create table #multi
(
[pnrlocator] char(8), [pnrcreatedate] date
,[segmentnumber] tinyint, [marketingflightnumber] char(5)
,[servicestartcity] char(3), [serviceendcity] char(3)
,[servicestartdate] date
)
insert into #multi
select distinct
pnrlocator p, pnrcreatedate d ,segmentnumber s,
marketingflightnumber fl,
servicestartcity sc, serviceendcity ec, servicestartdate sd
--into #multi
from dw2.dw.resflight
where servicestartdate >= ?

Brad's answer is a great way to do it. Another way is to simply add the parameter to your Parameter Mapping a second time.
A third way is to build your SQL statement in a variable with expressions. Then in your Execute SQL Task, your SQLSourceType would be Variable, and then you select the variable that contains your query. This can be an easy way to do it so you avoid messing around with picking the right data types for your parameters.

You wouldnt at the top of your code declare variables for the dates passed and set the values from the paramaters/? makrs to those and just use the variables in your code below
DECLARE #StartDate as Date
DECLARE #ToDate as DATE
SET #StartDate = ?
SET #ToDate = ?
-- so do it like this:
Select * from your table where MyDateColumn Between #StartDate and #ToDate
Then use those in your code below where you need them.
This lets you use the variables more than once and is cleaner and easier to see that your using parameters and how/where.
*** Updated to use your code:
DECLARE #StartDate as Date
DECLARE #ToDate as DATE
SET #StartDate = ?
SET #ToDate = ?
---SSR Table with passenger info, both APAY and PET
create table #SSRData
([ssrfl] int, [ssrcode] char(4), [ssrsequenceid] smallint, [ssrstatuscode]
char(2), [servicestartcitycode] varchar(5),
[ssrstartdate] date, [databasetimestamp] datetime, [pnrlocator] char(8),
[pnrcreatedate] date, [passengersequenceid] smallint,
[namefirst] varchar(250), [namelast] varchar(250), [frequenttravelernumber]
varchar(25)
)
insert into #ssrdata
select distinct ssrfl,
s.ssrcode,s.ssrsequenceid,s.ssrstatuscode,s.servicestartcitycode,
s.ssrstartdate, s.databasetimestamp, s.pnrlocator, s.pnrcreatedate
,s.passengersequenceid, namefirst, namelast,frequenttravelernumber
--into #SSRData
from
(select cast(ssrflightnumber as int)ssrfl,
ssrcode,ssrsequenceid,ssrstatuscode,servicestartcitycode,
ssrstartdate, pnrlocator, pnrcreatedate
,passengersequenceid,databasetimestamp from dwjetblue2.dw.resssr
where SSRCode in ('APAY', 'PETC') and PNRLocator <>'purged'
and ssrstartdate >= #StartDate --?
and ssrstartdate < #ToDate)s --?
inner join dw.dw.ResPassenger p
on p.pnrcreatedate=s.pnrcreatedate
and p.pnrlocator=s.pnrlocator
and p.passengersequenceid=s.passengersequenceid
inner join dwjetblue2.dw.ResPassengerFT ft
on ft.pnrcreatedate=s.pnrcreatedate
and ft.pnrlocator=s.pnrlocator
and ft.passengersequenceid=s.passengersequenceid
--MultiLeg
create table #multi
(
[pnrlocator] char(8), [pnrcreatedate] date
,[segmentnumber] tinyint, [marketingflightnumber] char(5)
,[servicestartcity] char(3), [serviceendcity] char(3)
,[servicestartdate] date
)
insert into #multi
select distinct
pnrlocator p, pnrcreatedate d ,segmentnumber s,
marketingflightnumber fl,
servicestartcity sc, serviceendcity ec, servicestartdate sd
--into #multi
from dw2.dw.resflight
where servicestartdate >= #StartDate

Related

Subquery - Incorrect Syntax near the keyword 'declare'

Having an issue debugging this, hoping someone can help me clear this up. This is part of a much longer query, but the subquery "B" is the only part that is causing a problem, not sure why... I'm obviously missing something. I'm using sql-server.
The sub query runs fine on its own, just not with the rest of the query.
DECLARE #dstrt AS DATETIME
SET #dstrt = '2020-09-01 00:00:00'
DECLARE #dend AS DATETIME
SET #dend = '2020-09-30 23:59:59'
DECLARE #UnpaidChgsTot table
(
SiteID INT,
Period nvarchar(30),
dcDlqntTot money,
iDelUnits int,
dcPctUnits money,
dcPctGrossPot money,
dcPctActOcc money,
iDatePeriod int
)
DECLARE #sLanguageTermColName nvarchar(20)
SET #sLanguageTermColName = 'English'
-- DECLARE some period counters
DECLARE #StartDayNumber int
DECLARE #EndDayNumber int
SET #StartDayNumber = 0
SET #EndDayNumber = -1
--Hold the original date value
DECLARE #dEndORig datetime
SET #dEndORig = #dEnd
DECLARE #dcGrossPotDenom money
DECLARE #dcActOccDenom money
DECLARE #iTotUnitsDenom int
SET #dcGrossPotDenom = 1
SET #dcActOccDenom = 1
SET #iTotUnitsDenom = 1
--Define a holding table for charge balances by ChargeID
DECLARE #BalT table
(
SiteID int,
ChargeID int,
LedgerID int,
ChargeDescID int,
sChgCategory nvarchar(20),
sDefAcctCode nvarchar(20),
dChgStrt datetime,
dcBalAmt money,
dcBalTax1 money,
dcBalTax2 money
)
/*
* Updated. Delete was taking too long to run, added to WHERE to get rid
* Date: 10192009
* Josh
*/
--Fill the #BalT with charge balances for charges <=dEND
INSERT INTO #BalT
SELECT
B.SiteID,
B.ChargeID,
B.LedgerID,
B.ChargeDescID,
CD.sChgCategory,
CA.sDefAcctCode,
B.dChgStrt,
dcAmt,
dcTax1,
dcTax2
FROM -- PROBLEM STARTS HERE
(
DECLARE #ChargesT2 table
(
SiteID int,
ChargeID int,
ChargeDescID int,
sChgCategory nvarchar(50),
sDefAcctCode nvarchar(5),
dChgStrt datetime,
LedgerID int,
dcAmt money,
dcTax1 money,
dcTax2 money,
sChargeTag nvarchar(20)
)
DECLARE #fnPmtSumByChargeT Table
(
SiteID INT,
ChargeID int,
dcPmtSum money
)
DECLARE #fnPmtSumByChargeT2 Table
(
SiteID INT,
ChargeID int,
dcPmtSum money
)
DECLARE #ChargesTempT table
(
SiteID INT,
ChargeID int,
dcAmt money,
dcTax1 money,
dcTax2 money
)
Declare #ChargesT table
(
SiteID INT,
ChargeID int,
ChargeDescID int,
sChgCategory nvarchar(50),
sDefAcctCode nvarchar(5),
dChgStrt datetime,
LedgerID int,
dcAmt money,
dcTax1 money,
dcTax2 money,
sChargeTag nvarchar(20)
)
INSERT INTO #ChargesT2
SELECT
C.SiteID,
C.ChargeID,
C.ChargeDescID,
sChgCategory,
sDefAcctCode,
C.dChgStrt,
C.LedgerID,
Coalesce(C.dcAmt,0.0) AS dcBalAmt,
Coalesce(C.dcTax1,0.0) AS dcBalTax1,
Coalesce(C.dcTax2,0.0) AS dcBalTax2,
CASE
WHEN CAST(CA.sDefAcctCode AS INT) = 4000 THEN N'Rent'
WHEN CAST(CA.sDefAcctCode AS INT) = 4042 THEN N'LateFee'
WHEN CAST(CA.sDefAcctCode AS INT) = 4041 THEN N'AdminFee'
WHEN CAST(CA.sDefAcctCode AS INT) = 4070 THEN N'Insurance'
WHEN CAST(CA.sDefAcctCode AS INT) BETWEEN 4060 AND 4068 OR CAST(CA.sDefAcctCode AS INT) BETWEEN 4071 AND 4079 THEN N'POS'
--WHEN CAST(CA.sDefAcctCode AS INT) = 2020 THEN 'SecDep'
ELSE N'Others'
END as sChargeTag
FROM Charges AS C
INNER JOIN ChargeDesc AS CD ON C.ChargeDescID = CD.ChargeDescID
LEFT OUTER JOIN ChartOfAccts AS CA ON CD.ChartofAcctID = CA.ChartOfAcctID
WHERE
C.dDeleted IS NULL
AND C.dChgStrt <= #dend
--2016-01-12 - Case # 237424 - C158, L005, Unit 10478, Tenant - Zach Reese.
--Commented out dCreated evaluation. This is because A/R does not prevent backdating charges from changing historical reports. There is no way these two reports
--can tie out in the same period if we do not allow backdating charges that we created in a future period. Ex. Late fee created on January 2, 2016, but dChgStrt of 12/29/2015.
--AND dCreated <= #dEnd --This is to catch a case where NSF charges were added (backdated) after the report end date, affecting historical reporting. 02102011 Josh
AND (bNSF = 0 OR (bNSF = 1 AND dCreated <= #dend))--updated the logic to be different for NSF charges to not change reports historically; this loigc is consistant with A/R. Case # 269909
GROUP BY C.siteID, C.ChargeID, C.ChargeDescID, sChgCategory, sDefAcctCode, C.dChgStrt, C.LedgerID, C.dcAmt, C.dcTax1, C.dcTax2, CA.sDefAcctCode
INSERT INTO #fnPmtSumByChargeT2
SELECT
SiteID,
ChargeID,
dcPmtSum
FROM
(
SELECT
pay.SiteID,
Pay.ChargeID,
SUM(pay.dcPmtAmt) AS dcPmtSum
FROM
(
SELECT
SiteID,
ChargeID,
dcPmtAmt
FROM Payments
WHERE
(dDeleted Is Null)
--AND (bNSF = 0) --this fn must return ALL payments to calculate balances correctly
AND (dPmt <= #dend)
) AS Pay
GROUP BY SiteID, ChargeID
) AS P
INSERT INTO #fnPmtSumByChargeT2
SELECT
C.SiteID,
C.ChargeID,
0
FROM #ChargesT2 C
INSERT INTO #fnPmtSumByChargeT
SELECT
P.SiteID,
P.ChargeID,
SUM(P.dcPmtSum)
FROM #fnPmtSumByChargeT2 P
GROUP BY P.SiteID, P.ChargeID
INSERT INTO #ChargesTempT
SELECT
P.SiteID,
P.ChargeID,
C.dcAmt - Coalesce(dbo.fnPartNonTax(C.dcAmt, C.dcTax1, C.dcTax2, P.dcPmtSum, 2),0.0),
C.dcTax1 - Coalesce(dbo.fnPartTax1(C.dcAmt, C.dcTax1, C.dcTax2, P.dcPmtSum, 2,2),0.0),
C.dcTax2 - Coalesce(dbo.fnPartTax2(C.dcAmt, C.dcTax1, C.dcTax2, P.dcPmtSum,2,2),0.0)
FROM #ChargesT2 C
INNER JOIN #fnPmtSumByChargeT P ON C.ChargeID = P.ChargeID
GROUP BY P.SiteId, P.ChargeID, C.dcAmt, dcTax1, dcTax2, dcPmtSum
HAVING (dcAmt + dcTax1 + dcTax2 - dcPmtSum) > 0 -- Removed 0 sum columns: This was a major bottleneck for this function 10202009 J
INSERT INTO #ChargesT
SELECT
C.SiteID,
C.ChargeID,
C.ChargeDescID,
C.sChgCategory,
C.sDefAcctCode,
C.dChgStrt,
C.LedgerID,
CT.dcAmt,
CT.dcTax1,
CT.dcTax2,
C.sChargeTag
FROM #ChargesT2 C
INNER JOIN #ChargesTempT CT ON C.ChargeID = CT.ChargeID
SELECT *
FROM #ChargesT
AS ChargeT
) AS B -- PROBLEM ENDS HERE
INNER JOIN ChargeDesc AS CD ON B.ChargeDescID = CD.ChargeDescID AND B.Siteid = CD.SiteID
I indicated "Problem starts here" and "problem ends here" in the notes, if that helps.
Thanks in advance.
Inside a subquery the only thing you is write a select statement. You cannot write anything you want like it is a code block.
In your case, you may accomplish what you want by moving all those table variables to outside the parentheses and populating them, then inside the parentheses you just do the select. Like I said, it is not a code block, there are no local variables. There's no prettier way. SQL is a simple language for querying information. That's just how it is.
Alternatively, you may like to use Common Table Expressions (CTEs) to give each subquery a name. The subqueries do not exist as any temp variable or table, just as helper names available during the main query. That may solve what you want do to.

How to add an additional column to the result set returned by a SP without modifying the SP?

I have a Stored Procedure (SP), named myStoredProcedure, returning me such output based on startDate and endDate user-defined parameters:
PrimaryName SecondaryName Volume
A B 20
C D 30
A D 50
...
So, Volume represents the sum of all the cases between the dates defined.
In another SP, named mySecondStoredProcedure, I am using the first SP to get the result there. However, my problem is that I need an additional attribute in my output, which is year, I want to see year based volumes. Therefore, the output I would like to see is something like that
assume startDate: 2014, endDate: 2015:
PrimaryName SecondaryName Volume Year
A B 12 2014
C D 14 2014
A D 20 2014
A B 8 2015
C D 16 2015
A D 30 2015
...
I am not allowed to modify myStoredProcedure. Therefore I build a while loop in the second SP to receive it. My code is like:
declare #temp_table table
(
PrimaryGroup varchar(10),
SecondaryGroup varchar(10),
Volume int
)
while #startDate < #endDate
begin
insert into #temp_table
exec myStoredProcedure #startDate #endDate
set #startDate = DATEADD(YEAR,1,#startDate)
end
select * from #temp_table
This is giving me the result without the year column. I need a year column like I showed in my example output above. I could not find a way to add it. There is no primary key in the result set returned by myStoredProcedure. Also, SQL Server 2008 does not let me add a year column in #temp_table, saying that fields are not matching. How can I add the year column properly? Any help would be appreciated!
EDIT: When I add year column in the definition of #temp_table, the error I receive: Column name or number of supplied values does not match table definition.
You're close with the syntax you currently have, you'll just need to add the year to the temp table and supply it after calling the stored procedure. In addition, you will also need to specify the columns being inserted (a practice well worth getting in the habit of) as your procedure doesn't return the same number of columns.
declare #temp_table table
(
PrimaryGroup varchar(10),
SecondaryGroup varchar(10),
Volume int,
Year int
)
while #startDate < #endDate
begin
insert into #temp_table (PrimaryGroup, SecondaryGroup, Volume)
exec myStoredProcedure #startDate #endDate
Update #temp_table
Set Year = #StartDate
Where Year Is Null
set #startDate = DATEADD(YEAR,1,#startDate)
end
select * from #temp_table
Add a Year column to your temp table, and apply the structured insert
declare #temp_table table
(
PrimaryGroup varchar(10),
SecondaryGroup varchar(10),
Volume int,
Year int
)
while #startDate < #endDate
begin
insert into #temp_table (PrimaryName,SecondaryName,Volume)
exec myStoredProcedure #startDate #endDate
Update #temp_table set Year = #startDate where Year is Null
set #startDate = DATEADD(YEAR,1,#startDate)
end
select * from #temp
Create a second table variable that will hold the result:
declare #result_table table
(
Year int,
PrimaryGroup varchar(10),
SecondaryGroup varchar(10),
Volume int
)
Then in the while loop after fetching the result into #temp_table:
insert into #result_table
select <year>, PrimaryGroup, SecondaryGroup, Volume from #temp_table;
truncate #temp_table;

How to return a list of courses between 2 months in SQL Server 2012 Management Studio?

How to create a stored procedure in SQL Server 2012 that returns a list of courses running between 2 months?
I've written code something like this:
create procedure final_RTrainerqualification
(#TrainerID char(10),
#Coursecode char(4) OUTPUT,
#qualcode nvarchar(30),
#coursedate datetime output)
DECLARE #MinDate DATE = '20160401',
#MaxDate DATE = '20160601';
SELECT coursedate
FROM dbo.RTrainerqualification
WHERE coursedate >= #MinDate
AND coursedate < #MaxDate;`
It should be returning the list of courses that run between these 2 dates mentioned but I am new to stored procedure so my question is how do I assign coursedates to the courses and make it return the list?
Edit- used T-SQL to recreate the code
#Coursecount smallint
declare #Coursedatebeg datetime, #Coursedateend datetime, #CourseCode char(4),#TrainerID char(10);
select #Coursedatebeg = '2015-04-20'
select #Coursedateend = '2015-06-20';
while #Coursedatebeg <= #Coursedateend
begin
select #CourseCode = #Coursedateend;
select #Coursecount = count(*) from RCourseInstance
where CourseCode between 'R222' and 'R224';
if #CourseCount <> 0
begin
Print 'Courses running between April and June 2015 ' ;
select Coursedate,CourseCode from RCourseInstance as t
inner join Coursedate as d on t.Coursedate = d.Coursedate
inner join CourseCode as c on c.CourseCode = t.CourseCode
where CourseCode between 'R222' and 'R224';
end
else
print 'No courses are running between these dates ' ;
set #Coursedatebeg = #Coursedatebeg + 2;
end
It is returning the print statement but also declaring that invalid object name Coursedate
what have I done incorrectly here?
Firstly, use ISO date strings to avoid regional settings issues. You are only returning the coursedate in the SELECT. If you want further information such as the coursecode then add it into the SELECT. i.e.
DECLARE #MinDate DATE = '2016-04-01',
#MaxDate DATE = '2016-06-01'
SELECT coursedate, coursecode, *
FROM dbo.RTrainerqualification
WHERE coursedate >= #MinDate
AND coursedate < #MaxDate;
The OUTPUT variables are not required - these are needed when you want to return a single value rather than a list.
This is fundamental SQL stuff so I would strongly recommend you do some reading on this. There is a simple stored procedure tutorial here to start off with.

SQL where clause not getting filtered

I have the following query, but it is not giving any regard to the in the p.created_by =#searchBy where clause, how to correct it so that the results would be filtered according #searchBy too.
ALTER PROC [dbo].[Rptcashcollectionouter] #branchId INT,
#searchBy INT,
#strDate DATETIME=NULL,
#endDate DATETIME=NULL
AS
BEGIN
SELECT DISTINCT p.created_on AS paid_date
FROM reading re
JOIN billing_gen bg ON re.id = bg.reading_id
JOIN customer_registration cr ON bg.account_number = cr.account_number
JOIN payment p ON bg.bill_number = p.bill_number
JOIN customer_category cc ON cr.customer_category_id = cc.id
WHERE p.created_by = #searchBy
AND ( ( #strDate IS NULL )
OR Cast(Floor(Cast(p.created_on AS FLOAT)) AS DATETIME) >=
Cast(Floor(Cast(#strDate AS FLOAT)) AS DATETIME) )
AND ( ( #endDate IS NULL )
OR Cast(Floor(Cast(p.created_on AS FLOAT)) AS DATETIME) <=
Cast(Floor(Cast(#endDate AS FLOAT)) AS DATETIME) )
AND cr.branch_id = #branchId
ORDER BY p.created_on ASC;
END;
Check the value inside your procedure as below.
SELECT #branchId, #searchBy, #strDate,#endDate
And, then try to run the SQL manually with the same value. Also, make sure you have data in your table for your criteria.
Also, what exactly you are trying here ?
Cast(Floor(Cast(p.created_on AS FLOAT)) AS DATETIME)
While executing procedure, make sure you are passing properly value.
Print out all values that are coming (Just for testing).
#searchBy INT is of integer type. But i think "p.created_by =#searchBy" is a type of datetime or date , so it may also conflicts here, or display wrong result. In below line. p.created_by is treating as a datetime or date and #searchby in integer.
WHERE p.created_by = #searchBy

SQL Server function error - conversion failed when converting date and/or time from character string

CREATE FUNCTION [dbo].[fn_InventoryPositionSet]
(
#PointInTime DATETIME,
#EOD BIT,
#AccountList Varchar(max)
)
RETURNS #OffersTable TABLE
(
Account VARCHAR(10),
PositionId VARCHAR(150),
VersionDate DATETIME,
ProductType VARCHAR(50),
XRef VARCHAR(50),
XRefType VARCHAR(20),
Desk VARCHAR(50)
)
AS
BEGIN
IF #EOD = 1
SET #PointInTime = dbo.COBTimestamp(#PointInTime)
IF #AccountList is not NULL
INSERT INTO #OffersTable
SELECT
P.Account,
P.UniquePositionId AS [PositionId],
P.Desk,
P.VersionEffective AS [VersionDate],
P.ProductType,
P.XRef,
P.XRefType
FROM MarkingInventory AS P WITH(nolock)
LEFT JOIN
Spirit.dbo.GetTableOfInputs(#AccountList) A
ON
(
(P.Account = A.Input)
AND
#AccountList IS NOT NULL
)
WHERE
#PointInTime >= P.VersionEffective AND #PointInTime < P.VersionExpiration and P.Account = A.Input
ELSE
INSERT INTO #OffersTable
SELECT
P.Account,
P.UniquePositionId AS [PositionId],
P.Desk,
P.VersionEffective AS [VersionDate],
P.ProductType,
P.XRef,
P.XRefType
FROM MarkingInventory AS P WITH(nolock)
WHERE
#PointInTime >= P.VersionEffective AND #PointInTime < P.VersionExpiration
RETURN
END
I have created this function which takes in three parameters. I have couple of questions regarding execution of this function.
Conversion failed when converting date and/or time from character string
This is the error I receive when I try to execute this function in following manner
select * from [dbo].[fn_InventoryPositionSet] (GETDATE(), 1, null)
It fails even when I pass 2012-03-10 instead of getdate()
I also want to learn how to pass parameters in the form of list. As one of the parameter is in form of a list. Let me know if you want some more information about the function.
This is a function which is used in the function written above:
CREATE FUNCTION [dbo].[COBTimestamp]
(
#COBDate DATETIME
)
RETURNS DATETIME
AS
BEGIN
RETURN DATEADD(day, DATEDIFF(day, 0, #COBDate), '23:59:59')
END
GO
Look at your table definition:
1. Account VARCHAR(10),
2. PositionId VARCHAR(150),
*** 3. VersionDate DATETIME, ***
4. ProductType VARCHAR(50),
5. XRef VARCHAR(50),
6. XRefType VARCHAR(20),
7. Desk VARCHAR(50)
And your SELECT:
SELECT
1. P.Account,
2. P.UniquePositionId AS [PositionId],
*** 3. P.Desk, ***
4. P.VersionEffective AS [VersionDate],
5. P.ProductType,
6. P.XRef,
7. P.XRefType
The problem not in [dbo].[fn_InventoryPositionSet] it is in [dbo].[COBTimestamp].
RETURN DATEADD(day, DATEDIFF(day, 0, #COBDate), '23:59:59')
^__________ Must be converted to `DATETIME`
DATEADD (Transact-SQL)