Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sql query for nullable number greater than parameter

Tags:

sql

sql-server

Simple table:

create table Items
(
  Price money null
)

Now I need to create a stored procedure that accepts one paramter of type bit @ItemsWithPriceTenDollarsOrMore which:

  1. returns all items if parameter is null
  2. returns all items with Price >=10 if parameter = 1
  3. returns all items with Price < 10 if parameter = 0

I have difficulty expressing this filter in a single where statement (not using dynamic sql or conditional logic).

like image 547
Valentin V Avatar asked Apr 28 '26 02:04

Valentin V


1 Answers

Try this one:

SELECT * FROM Items
WHERE (@ItemsWithPriceTenDollarsOrMore = 1 AND Price >=10)
OR (@ItemsWithPriceTenDollarsOrMore = 0 AND Price <10)
OR (@ItemsWithPriceTenDollarsOrMore IS NULL)
like image 92
Pavel Morshenyuk Avatar answered Apr 30 '26 16:04

Pavel Morshenyuk