【问题标题】:If then operator in where clause of sql statement [duplicate]sql语句的where子句中的if then运算符[重复]
【发布时间】:2018-03-24 20:18:59
【问题描述】:

我有一个场景,我需要从可以传递最小生日和最大生日的表中选择员工列表。 所以我需要的是这样的:-

Select all from employees where active=1 and 

if (not @min_birthday is null and @max_birthday is null)
    birthday > @min_birthday


else if (@min_birthday is null and not @max_birthday is null)
    birthday < @max_birthday

else if (not @min_birthday is null and not @max_birthday is null)
    birthday between (@min_birthday and @max_birthday)

有没有办法在一条 sql 语句中完成上述操作?

【问题讨论】:

    标签: sql-server


    【解决方案1】:
    where   active = 1 
            and birthday between isnull(@min_birthday, birthday) 
                                 and isnull(@max_birthday, birthday)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-07
      • 2013-03-02
      • 1970-01-01
      • 2014-06-15
      • 1970-01-01
      相关资源
      最近更新 更多