Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 带有日期的where子句中的Case语句_Sql_Date_Case - Fatal编程技术网

Sql 带有日期的where子句中的Case语句

Sql 带有日期的where子句中的Case语句,sql,date,case,Sql,Date,Case,我正在努力避免硬编码的付费日期。财政年度为10/1至9/30 如果有人在7月1日至12月31日期间加入,则付费截止日期为下一个日历年,例如,2013年11月,有人已付费并获得2014年9月30日的付费截止日期。我需要2013年和2014年的付款方式 如果有人在1月1日至6月30日期间加入,则付费通过日期为当前日历年,例如,今年1月将是2014年,他们的付费通过日期为2014年9月30日,我不再需要2013年付费通过组 以下是我所拥有的: 如果getdate月介于1和6之间,那么我需要在当前年度将

我正在努力避免硬编码的付费日期。财政年度为10/1至9/30

如果有人在7月1日至12月31日期间加入,则付费截止日期为下一个日历年,例如,2013年11月,有人已付费并获得2014年9月30日的付费截止日期。我需要2013年和2014年的付款方式

如果有人在1月1日至6月30日期间加入,则付费通过日期为当前日历年,例如,今年1月将是2014年,他们的付费通过日期为2014年9月30日,我不再需要2013年付费通过组

以下是我所拥有的: 如果getdate月介于1和6之间,那么我需要在当前年度将付费通过率拉至9/30/+的人。如果getdate月在7到12之间,那么我需要选择以当前年9月30日+下一年9月30日的付费方式吸引用户

select id, paid_thru, getdate()as today
from name
where datepart(mm,getdate())between 1 and 6 and datepart (yyyy,paid_thru)+1 = datepart(yyyy,getdate())
or 
datepart(mm,getdate()) between 7 and 12 and datepart (yyyy,paid_thru) = datepart(yyyy,getdate())
此查询仅提供2013年9月30日,因为该月是11月(11日),我需要2013年9月30日和2014年9月30日的付款截止日期


谢谢

您需要对您的或条款进行说明

select id,
    paid_thru,
    getdate() as today
from name
where
    (
        datepart( mm,getdate() ) between 1 and 6
        and datepart(yyyy,paid_thru) + 1 = datepart(yyyy,getdate())
    )
or
    (
        datepart(mm,getdate()) between 7 and 12
        and datepart (yyyy,paid_thru) = datepart(yyyy,getdate())
    )

由于您没有任何parens,因此即使第一个or子句的另一部分没有通过,您也能满足其中一部分的要求。

您的or子句周围需要parens

select id,
    paid_thru,
    getdate() as today
from name
where
    (
        datepart( mm,getdate() ) between 1 and 6
        and datepart(yyyy,paid_thru) + 1 = datepart(yyyy,getdate())
    )
or
    (
        datepart(mm,getdate()) between 7 and 12
        and datepart (yyyy,paid_thru) = datepart(yyyy,getdate())
    )

由于您没有任何参数,因此即使第一个or子句的另一部分未通过,也满足了其中一部分。

您使用的是哪一个DBMS?@user2984972您解决了这个问题吗?您使用的是哪一个DBMS?@user2984972您解决了这个问题吗?