Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am searching few records based on the search criteria. I am getting the data one day before. (say if I take data for 8th , records of 7th is being shown). Filtering date fields all are in datetime data type which is being converted to date. It worked in one server taking the same date set for filtering but here taken previous date. Stored procedure are same in both servers.

What I have tried:

Sample SP for the same

-- EXEC [TestReport]  '2019-04-08', '2019-04-08'   

ALTER PROCEDURE [dbo].[TestReport]

@FromDate datetime,
@ToDate datetime 

AS
BEGIN

select * from test 
where
 
CONVERT(date,settlementDate)
between
 CONVERT(date,@FromDate) and  CONVERT(date,@ToDate)

 END
Posted
Updated 22-Apr-19 18:51pm
Comments
Richard Deeming 24-Apr-19 17:50pm    
1) Declare your parameters as date, rather than declaring them as datetime and CONVERTing them.

2) What data type is the settlementDate column? If it's datetime or datetime2, and you're just trying to ignore the time part, then change your filter to:
WHERE settlementDate >= @FromDate And settlementDate < DateAdd(day, 1, @ToDate)

That way, SQL will be able to use an index on the column (if any) to speed up the query.

If it's a datetimeoffset column, then you'll need to look at the time zones as Christian suggested.

If it's a string, then you're doing it wrong! :)

1 solution

If you're getting data from a different date to the one in your query, I'd suggest your issue has to do with time zones. Somewhere a time zone conversion is happening of the date you provide, moving it back a day
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900