真的在这里摸不着头脑。我有一个具有日期过滤器的查询。但是,我不断得到超出我的日期过滤期间的结果!我已经检查过我的列确实是一个日期列,我已经尝试使用Between和> =< =并且这些似乎都没有任何影响。
以下是我的代码示例。
Declare @PARAM_REPORTING_START_DATE as datetime
Declare @PARAM_REPORTING_END_DATE as datetime
Declare @PARAM_CRDTR_ID as integer
Set @PARAM_REPORTING_START_DATE = '2015/06/12'-- 05:39:44 AM'
Set @PARAM_REPORTING_END_DATE = '2015/06/24'-- 05:39:44 AM'
Set @PARAM_CRDTR_ID = 1005
--select @PARAM_REPORTING_START_DATE, @PARAM_REPORTING_END_DATE
Select distinct
capj.cnsmr_accnt_pymnt_jrnl_id,
c.cnsmr_nm_prfx_txt,
c.cnsmr_nm_frst_txt,
c.cnsmr_nm_lst_txt,
capj.cnsmr_accnt_pymnt_pstd_dt,
isdate(capj.cnsmr_accnt_pymnt_pstd_dt) as is_date,
capj.bckt_trnsctn_typ_cd as capj_bckt_trnsctn_typ_cd ,
pm.pymnt_memo_nm
from
cnsmr_accnt_pymnt_jrnl capj
inner join cnsmr_pymnt_jrnl cpj
on capj.cnsmr_pymnt_jrnl_id = cpj.cnsmr_pymnt_jrnl_id
inner join pymnt_memo pm
on pm.pymnt_memo_id = cpj.pymnt_memo_id
inner join crdtr cr
on capj.crdtr_id = cr.crdtr_id
inner join cnsmr_accnt ca
on ca.cnsmr_accnt_id = capj.cnsmr_accnt_id
inner join cnsmr c on c.cnsmr_id = ca.cnsmr_id
Where
capj.cnsmr_accnt_pymnt_pstd_dt>=@PARAM_REPORTING_START_DATE
and
capj.cnsmr_accnt_pymnt_pstd_dt<=@PARAM_REPORTING_END_DATE
and
( -1 IN ( @PARAM_CRDTR_ID ) ) OR ( cr.crdtr_ID in (@PARAM_CRDTR_ID) )
答案 0 :(得分:1)
您将获得满足
的所有结果OR ( cr.crdtr_ID in (@PARAM_CRDTR_ID) )
所以日期过滤器并不重要。
您应该重新排列WHERE
子句中的括号/大括号,例如
WHERE
capj.cnsmr_accnt_pymnt_pstd_dt
BETWEEN @PARAM_REPORTING_START_DATE AND @PARAM_REPORTING_END_DATE
AND
(
@PARAM_CRDTR_ID = -1
OR
@PARAM_CRDTR_ID = cr.crdtr_ID
);
答案 1 :(得分:1)
有大括号错位
Where
capj.cnsmr_accnt_pymnt_pstd_dt>=@PARAM_REPORTING_START_DATE
and
capj.cnsmr_accnt_pymnt_pstd_dt<=@PARAM_REPORTING_END_DATE
and
( -1 IN ( @PARAM_CRDTR_ID ) OR cr.crdtr_ID in (@PARAM_CRDTR_ID) )