I am trying to create an SSIS package which pulls a set of records from the database by checking a date field (MODIFYDATE) in the table against a package variable (User::LastUpdate). Records where MODIFYDATE is equal to or after LastUpdate get selected.
I have tried this two ways, using DateDiff and a UDF that returns a bit (both are using Second as the interval), and have run into the same problem both ways. The date matching seems to be off by two days.
If LastUpdate is set at 1/22/2013 0:00, two records should be returned, one with a MODIFYDATE of 1/22/2013 14:47 and another with a MODIFYDATE of 1/22/2013 15:34. But neither of those gets returned unless LastUpdate is set no later than 1/20/2013 12:00. If it set earlier than that, they both get returned. If later, neither get returned.
The rows are being pulled in an OLE DB Source component in the SSIS package. If I go into the Query Builder and run the query from there, supplying the date manually, the correct rows are returned. I have inserted a breakpoint and confirmed that the LastUpdate contains the correct date when the rows are pulled (the value of LastUpdate is supplied by a stored procedure earlier in the package).
Here is the query as it stands now.
SELECT ACTIVITIES.*
FROM ACTIVITIES LEFT OUTER JOIN
ArchivedEvents ON ACTIVITIES.FBC_EVENTSID =
ArchivedEvents.FBC_EVENTSID
WHERE (dbo.IsDateInOrder(?, ACTIVITIES.MODIFYDATE) = 1) OR
(dbo.IsDateInOrder(?, ArchivedEvents.MODIFYDATE) = 1)
And the function I created
CREATE FUNCTION [dbo].[IsDateInOrder]
(
@Date1 DateTime,
@Date2 DateTime
)
RETURNS bit
AS
BEGIN
DECLARE @retval bit
if DATEDIFF(s, @Date1, @Date2) >= 0
SET @retval = 1
else
SET @retval = 0
RETURN @retval
END
GO