简体   繁体   English

在SQL CASE where子句中使用BETWEEN

[英]Using BETWEEN in SQL CASE where clause

I Have a form, where the user pulls a report. 我有一个表单,用户在其中提取报告。 In the form they can choose a start date and an end date, or pass through a null for both values. 在表单中,他们可以选择开始日期和结束日期,或者为两个值传递null。 If they choose null, it returns all records where effectivedate < GETDATE() The CASE statement doesn't seem to like between, nor does it like '<' operators 如果他们选择null,则返回所有记录,其中effectivedate <GETDATE()CASE语句似乎不喜欢它们,也不喜欢'<'运算符

Here is my script 这是我的剧本

SELECT * FROM tbReport
WHERE 
    EffectiveDate 
    CASE 
        WHEN (@StartDate IS NOT NULL AND @EndDate IS NOT NULL) 
        THEN BETWEEN (@StartDate AND @EndDate)          
         ELSE 
                  THEN < GETDATE()

    END

You could rewrite it without a case, like: 您可以在没有案例的情况下重写它,例如:

SELECT  * 
FROM   tbReport
WHERE   (
            @StartDate is not null 
            and 
            @EndDate is not null
            and 
            EffectiveDate between @StartDate AND @EndDate
        )
        or
        (
            (
                @StartDate is null 
                or 
                @EndDate is null
            )
            and 
            EffectiveDate < getdate()
        )

Assuming this is SQL Server, you can simplify this by using isnull: 假设这是SQL Server,您可以使用isnull简化此操作:

select *
from tbReport
where EffectiveDate between isnull(@StartDate, '1 Jan 1990')
      and isnull(@EndDate, getdate())

Something like this should work, I havent checked the syntax is correct though. 像这样的东西应该工作,我没有检查语法是否正确。

SELECT * FROM tbReport
WHERE EffectiveDate < IsNull(@EndDate,GetDate())
AND EffectiveDate > IsNull(@StartDate,'01/01/1979')

Your syntax for CASE is all wrong here.. Try this instead: CASE的语法在这里都是错误的。试试这个:

SELECT * FROM tbReport
WHERE 
    (@StartDate is null and @EndDate is null and EffectiveDate < GetDate()) or 
    (@StartDate is not null and @EndDate is not null and 
     EffectiveDate >= @StartDate and EffectiveDate <= @EndDate)

the SQL statement is not written correct. SQL语句写得不正确。 Case will not conditionally chose the evaluated code. Case不会有条件地选择评估的代码。 You can look to the case as a value, so you have to put an operator between the case and the other operand. 您可以将大小写视为一个值,因此您必须在大小写和另一个操作数之间放置一个运算符。

One of the multiple ways of writing this would be: 写这个的多种方式之一是:

where 
case when @StartDate IS NOT NULL AND @EndDate IS NOT NULL and EffectiveDate BETWEEN (@StartDate AND @EndDate)   then 1
case when (@StartDate IS NULL OR @EndDate IS NULL) and EffectiveDate <getdate() then 1
else 0 end = 1

If you do not want to write it with case you can chose one of the solutions before, but they use a discrete starting date. 如果您不想用大写字母书写它,您可以选择其中一个解决方案,但它们使用离散的开始日期。

WHERE 

(SR.RecCreateTS BETWEEN  ( CASE WHEN  @SubmittedBegining IS NOT NULL  THEN @SubmittedBegining ELSE SR.RecCreateTS END )
                            AND ( CASE WHEN  @SubmittedEnding IS NOT NULL   THEN @SubmittedEnding ELSE SR.RecCreateTS END )
                            )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM