如果那么运算符在sql语句的where子句中

时间:2018-03-24 20:19:00

标签: sql-server

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

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语句中完成上述内容?

1 个答案:

答案 0 :(得分:1)

where   active = 1 
        and birthday between isnull(@min_birthday, birthday) 
                             and isnull(@max_birthday, birthday)