【问题标题】:In SQL Where if date then compare dateColumn1 else compare with dateColumn2在 SQL Where if date then compare date Column1 else 与 date Column 2 比较
【发布时间】:2014-08-27 05:19:35
【问题描述】:

对于我的问题,真的不知道什么是最好的标题。 我什至不知道这是否可能。我会抓住一切机会。

让我们来解决我的问题。 我有@dateStart 和@dateEnd 然后我有 2 列日期。 让我们将其命名为例如 dateCol1 和 dateCol2 是否可以创建条件

Select * From table Where

案例 1 如果 @dateStart 大于采样日期 '1/01/2014' 那么它将推送到条件

dateCol1 between @dateStart and @dateEnd

否则,如果@dateStart 小于该日期,我可以有条件

dateCol2 between @dateStart and @dateEnd

我不太擅长 SQL,所以如果有人可以在这里帮助我,我将非常感激。 顺便说一句,我使用的是 Microsoft SQL

【问题讨论】:

    标签: sql sql-server case where


    【解决方案1】:
    Select * From table Where case when @dateStart > '1/01/2014' then dateCol1 else dateCol2 end
    between @dateStart and @dateEnd
    

    试试这个解决方案

    【讨论】:

      【解决方案2】:

      使用 CASE WHEN... ELSE

       CASE
         WHEN Boolean_expression THEN.       
       result_expression [ ...n ] 
         [ ELSE else_result_expression ] 
      END
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-10-04
        • 1970-01-01
        • 2016-10-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多