SQL 难题 - 日期过滤器不起作用

SQL conundrum - date filter doesn't work

这里真的让我摸不着头脑。我有一个具有日期过滤器的查询。但是,我不断得到超出我的日期过滤期的结果!我已经检查过我的列确实是一个日期列,我尝试使用 Between 和 >= <= 和 none 这些似乎有最小的影响。

这是我的代码示例。

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) )

您将获得满足

的所有结果
OR ( cr.crdtr_ID in (@PARAM_CRDTR_ID) )

所以日期过滤器无关紧要。

您应该重新排列 WHERE 子句中的 brackets/braces,例如

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
        );

大括号错位

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) )