日期范围之间的SQL不符合预期

时间:2012-01-25 23:13:52

标签: sql-server-2008 between date-range

如果我有DateStart = 2012-03-19 00:00:00.000和DateEnd of 2012-03-24 23:59:59.000那么我试试

SELECT EventGuid,
       Title,
       DESCRIPTION,
       CONVERT(DATETIME, FLOOR(CONVERT(FLOAT, DateStart)))            AS
       DateStart,
       CONVERT(DATETIME, FLOOR(CONVERT(FLOAT, DateEnd))) + '23:59:59' AS DateEnd
       ,
       Address1,
       Address2,
       City,
       PostalCode,
       Country,
       Latitude,
       Longitude,
       SeatCount,
       SeatCost,
       Active,
       DateCreated,
       DATEPART(HH, DateStart)                                        AS
       StartHour,
       LastModified
FROM   Event
WHERE  ( Active = 1 )
       AND ( EventTypeGuid <> '00000000-0000-0000-0000-000000000000' )
       AND ( CONVERT(DATETIME, '3/19/2012') <= DateEnd )
       AND ( CONVERT(DATETIME, '3/19/2012') >= DateStart )
ORDER  BY DateStart  

它没有拿起那一行。

如果我改为在syntaxt之间

SELECT EventGuid,
       Title,
       DESCRIPTION,
       CONVERT(DATETIME, FLOOR(CONVERT(FLOAT, DateStart)))            AS
       DateStart,
       CONVERT(DATETIME, FLOOR(CONVERT(FLOAT, DateEnd))) + '23:59:59' AS DateEnd
       ,
       Address1,
       Address2,
       City,
       PostalCode,
       Country,
       Latitude,
       Longitude,
       SeatCount,
       SeatCost,
       Active,
       DateCreated,
       DATEPART(HH, DateStart)                                        AS
       StartHour,
       LastModified
FROM   Event
WHERE  ( Active = 1 )
       AND ( EventTypeGuid <> '00000000-0000-0000-0000-000000000000' )
       AND ( CONVERT(DATETIME, '3/19/2012') BETWEEN DateStart AND DateEnd )
ORDER  BY DateStart  

它也得到3/19日期

所以我随后修改为下面的SQl,DateStart = 2012-06-03 23:59:51.000 DateStart2(我需要显示)2012-06-04 00:00:00.000和DateEnd 2012-06-09 23 :59:59.000但是它确实拿到了6/4的日期......

SELECT EventGuid,
       Title,
       DESCRIPTION,
       CONVERT(DATETIME, FLOOR(CONVERT(FLOAT, DateStart))) - '00:00:09' AS
       DateStart,
       CONVERT(DATETIME, FLOOR(CONVERT(FLOAT, DateStart)))              AS
       DateStart2,
       CONVERT(DATETIME, FLOOR(CONVERT(FLOAT, DateEnd))) + '23:59:59'   AS
       DateEnd,
       Address1,
       Address2,
       City,
       PostalCode,
       Country,
       Latitude,
       Longitude,
       SeatCount,
       SeatCost,
       Active,
       DateCreated,
       DATEPART(HH, DateStart)                                          AS
       StartHour,
       LastModified
FROM   Event
WHERE  ( Active = 1 )
       AND ( EventTypeGuid <> '00000000-0000-0000-0000-000000000000' )
       AND ( CONVERT(DATETIME, '6/4/2012') BETWEEN DateStart AND DateEnd )
ORDER  BY DateStart  

1 个答案:

答案 0 :(得分:0)

在我愿意投入问题的时候,我无法解决SQL查询问题。

我使我的Between select查询更加慷慨:

BETWEEN DATEADD(DAY, DATEDIFF(DAY, 0, DateStart), -1) AND DATEADD(DAY, DATEDIFF(DAY, 0, DateEnd), +1))

然后在我的vb.net代码中处理过滤,以便稳定。

If (e.DayRenderInfo.Date >= dt.Rows(i).Item("DateStart")) And (e.DayRenderInfo.Date <= dt.Rows(i).Item("DateEnd")) Then