SQL 服务器:WHERE 子句大小写介于两者之间

SQL Server : WHERE clause case with between

我有一个脚本,我想从数据库中提取相应日期的值。

我有一个函数,其中 return 一个日期,这个日期可以是 currentDate - 1 个月,也可以是 curentDate -2 个月。函数是

set @syssDate = DATEADD(month, -1, @syss_date);

我已经宣布开始日期:

declare @start_quarter datetime
set @start_quarter = '2015-07-01';

我只想在我的 WHERE 子句中添加一个

case
   when syssDate = start_quarter 
      then bu.date between '2015-01-01' and '2015-10-01'
       else   -- another date

我的查询如下:

    declare @start_quarter datetime
    set @start_quarter = '2015-01-01';

    declare @actual_Date datetime
    set @actual_Date = DATEADD(month, -1, @rollover_date);


        select sum(t.Revenue) as  Revenue  from (
             select sum(bu.value) as Revenue
                  from bus_category_for bu
                      join bus_category buc on buc.id=bu.bus_category_id
                      join bus bu on  bu.id=buc.bus_id
                   where buc.type_id=18 and bu.id=21 and 
--Here I want to add a case statement
bu.date between '2015-01-01' and '2015-10-01' 

我猜你需要这样的东西

SELECT Sum(t.Revenue) AS Revenue
FROM   (SELECT Sum(bu.value) AS Revenue
        FROM   bus_category_for bu
               JOIN bus_category buc
                 ON buc.id = bu.bus_category_id
               JOIN bus bu
                 ON bu.id = buc.bus_id
        WHERE  buc.type_id = 18
               AND bu.id = 21
               AND ( ( @syssDate = @start_quarter
                       AND bu.date BETWEEN '2015-01-01' AND '2015-10-01' )
                      OR ( another date ) ))a 

试试

WHERE   (syssDate = start_quarter AND bu.date between '2015-01-01' and '2015-10-01')
    OR   (yssDate <> start_quarter AND bu.date between '2016-01-01' and '2016-10-01')

一般来说,不建议使用 between 作为日期。最好使用bu.date >= '2015-01-01' AND bu.date < '2015-10-02'。参见 here