2

I have a scenario where I need to select a list of employees from a table where both a min birthdate and a max birthdate could be passed. So what I need is something like this : -

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)

Is there anyway to acomplish the above in a single sql statement?

Matt
  • 215
  • 1
  • 11

1 Answers1

1
where   active = 1 
        and birthday between isnull(@min_birthday, birthday) 
                             and isnull(@max_birthday, birthday)
Andomar
  • 232,371
  • 49
  • 380
  • 404