Date 日期在一个范围内的问题
鉴于以下SQL,我有一个问题: 根据startdate(@fi)和enddate(@ff),有时返回值,有时不返回值 例如: 1) 如果@fi和@ff为'01/01/2014'->正常Date 日期在一个范围内的问题,date,sql-server-2008-r2,date-range,Date,Sql Server 2008 R2,Date Range,鉴于以下SQL,我有一个问题: 根据startdate(@fi)和enddate(@ff),有时返回值,有时不返回值 例如: 1) 如果@fi和@ff为'01/01/2014'->正常 2)if @fi and @ff are '01/01/2015' -> ok 3)if @fi and @ff are '31/12/2014' -> no rows 4)if @fi = '31/12/2014' and @ff = '01/0
2)if @fi and @ff are '01/01/2015' -> ok
3)if @fi and @ff are '31/12/2014' -> no rows
4)if @fi = '31/12/2014' and @ff = '01/01/2015' -> ok (but returns more rows than the second case, that is, there should be results for the third case).
I've tried with BETWEEN clause, with the same result. I checked (in the 3rd case) if the is included in that range (with an if-else), and yes, it is. Any clue?
提前谢谢。
问候
声明@fi日期,
@ff日期
set@fi='31/12/2014'
设置@ff='01/01/2015'
选择Registro、Area、Grupo、Codigo、Asunto、Año、Total、IdArea、IdGrupo
从…起
(选择顶部(100%)TM2000\u registorasuntos.Registro,
TM2000_Areas.Area,TM2000_Grupos.Grupo,
TM2000_Asuntos.Codigo,TM2000_Asuntos.Asunto,
求和(强制转换)(当FechaDocumento>=@FI时的情况)
和FechaDocumento=@FI
和TM2000_RegistroGeneral.FechaDocumento=DATEADD(年份,-1,@FI)
和TM2000_RegistroGeneral.FechaDocumento=DATEADD(年份,-2,@FI)
和TM2000_RegistroGeneral.FechaDocumento=DATEADD(年份,-3,@FI)
和TM2000_RegistroGeneral.FechaDocumento=DATEADD(年份,-4,@FI)
和TM2000_RegistroGeneral.FechaDocumento用这个替换where子句
WHERE (( TM2000_RegistroGeneral.FechaDocumento >= @fi
AND TM2000_RegistroGeneral.FechaDocumento <= @ff )
OR ( TM2000_RegistroGeneral.FechaDocumento >= DATEADD(YEAR, -1, @fi)
AND TM2000_RegistroGeneral.FechaDocumento <= DATEADD(YEAR, -1, @ff) )
OR ( TM2000_RegistroGeneral.FechaDocumento >= DATEADD(YEAR, -2, @fi)
AND TM2000_RegistroGeneral.FechaDocumento <= DATEADD(YEAR, -2, @ff) )
OR ( TM2000_RegistroGeneral.FechaDocumento >= DATEADD(YEAR, -3, @fi)
AND TM2000_RegistroGeneral.FechaDocumento <= DATEADD(YEAR, -3, @ff) )
OR ( TM2000_RegistroGeneral.FechaDocumento >= DATEADD(YEAR, -4, @fi)
AND TM2000_RegistroGeneral.FechaDocumento <= DATEADD(YEAR, -4, @ff) )
)
AND ( ISNULL(TM2000_Asuntos.CodigoNulo, 0) = 0 )
其中((TM2000_RegistroGeneral.FechaDocumento>=@fi
和TM2000_RegistroGeneral.FechaDocumento=DATEADD(年份,-1,@fi)
和TM2000_RegistroGeneral.FechaDocumento=DATEADD(年份,-2,@fi)
和TM2000_RegistroGeneral.FechaDocumento=DATEADD(年份,-3,@fi)
和TM2000_RegistroGeneral.FechaDocumento=DATEADD(年份,-4,@fi)
和TM2000_RegistroGeneral.FechaDocumento在WHERE子句进行比较时,使用CAST(DATE,TM2000_RegistroGeneral.FechaDocumento)解决的问题
FechaDocumento是一个日期时间,因此需要一个cast
谢谢
WHERE (( TM2000_RegistroGeneral.FechaDocumento >= @fi
AND TM2000_RegistroGeneral.FechaDocumento <= @ff )
OR ( TM2000_RegistroGeneral.FechaDocumento >= DATEADD(YEAR, -1, @fi)
AND TM2000_RegistroGeneral.FechaDocumento <= DATEADD(YEAR, -1, @ff) )
OR ( TM2000_RegistroGeneral.FechaDocumento >= DATEADD(YEAR, -2, @fi)
AND TM2000_RegistroGeneral.FechaDocumento <= DATEADD(YEAR, -2, @ff) )
OR ( TM2000_RegistroGeneral.FechaDocumento >= DATEADD(YEAR, -3, @fi)
AND TM2000_RegistroGeneral.FechaDocumento <= DATEADD(YEAR, -3, @ff) )
OR ( TM2000_RegistroGeneral.FechaDocumento >= DATEADD(YEAR, -4, @fi)
AND TM2000_RegistroGeneral.FechaDocumento <= DATEADD(YEAR, -4, @ff) )
)
AND ( ISNULL(TM2000_Asuntos.CodigoNulo, 0) = 0 )