I apologize in advance if this question is too long but I wanted to make sure I included all the steps I followed to get to this point.
I have the following table in my SQL Server 2008 database:
CREATE TABLE [VSPRRecalc](
[VSPRDate] [datetimeoffset](7) NOT NULL,
[CalcType] [int] NOT NULL,
CONSTRAINT [PK_VSPRRecalc] PRIMARY KEY CLUSTERED ([VSPRDate] ASC)
It has some rows in it that look like this:
INSERT [vsprrecalc](VSPRDate,CalcType) VALUES('2010-12-15 10:17:49.5780000 -05:00','3')
INSERT [vsprrecalc](VSPRDate,CalcType) VALUES('2010-12-16 07:44:03.3750000 -05:00','1')
INSERT [vsprrecalc](VSPRDate,CalcType) VALUES('2010-12-17 07:40:40.1090000 -05:00','1')
INSERT [vsprrecalc](VSPRDate,CalcType) VALUES('2010-12-18 16:29:02.2203744 -05:00','2')
INSERT [vsprrecalc](VSPRDate,CalcType) VALUES('2010-12-20 09:58:50.1250000 -05:00','1')
INSERT [vsprrecalc](VSPRDate,CalcType) VALUES('2010-12-29 19:21:26.8120000 -05:00','1')
I'm using linq to check and see if a given date already exists in this table:
var recalc = (from re in VSPRRecalcs
where re.VSPRDate.Date == oDate.Value.Date
select re).SingleOrDefault();
Currently recalc
returns null whenever the date is within 5 hours of midnight (like the 12-29 case in the insert statements above). I checked and the following sql is being executed:
exec sp_executesql N'SELECT [t0].[VSPRDate], [t0].[CalcType]
FROM [dbo].[VSPRRecalc] AS [t0]
WHERE
CONVERT(DATE, [t0].[VSPRDate]) = @p0',N'@p0 datetime',@p0='2010-12-29'
Which returns 0 records. I modified the query to make the test easier to play with and came up with the following:
declare @t as date
set @t = '2010-12-29'
select *,
case when CONVERT(DATE, [VSPRDate]) = @t then 'true' else 'false' end
from VSPRRecalc where
CONVERT(DATE, [VSPRDate]) = @t
That query works for any other date in the table but not for any date that is within 5 hours of midnight (again see 12-29 above). If I run the above query without the where clause the 12-29 row does have 'true' displayed so clearly the boolean is evaluating the way I expect in the select
statement but not in the where
clause. Why does that happen?