How do you query an int column for any value?

select, sql, sql-server-2008, t-sql

Solution

Sometimes I would query for actual value (like 1, 2...) so I can't not have a condition either.

I take it you want some dynamic behavior on your `WHERE` clause, without having to dynamically build your `WHERE` clause.

With a single parameter, you can use `ISNULL` (or `COALESCE`) like this:

 SELECT * FROM Table WHERE ID = ISNULL(@id, ID)

which allows a `NULL` parameter to match all. Some prefer the longer but more explicit:

 SELECT * FROM Table WHERE (@id IS NULL) OR (ID = @id)

Problem

How can you query a column for any value in that column? (ie. How do I build a dynamic where clause that can either filter the value, or not.) I want to be able to query for either a specific value, or not. For instance, I might want the value to be 1, but I might want it to be any number. Is there a way to use a wild card (like "*"), to match any value, so that it can be dynamically inserted where I want no filter? For instance: ``` select int_col from table where int_col = 1 // Query for a specific value select int_col from table where int_col = * // Query for any value ``` The reason why I do not want to use 2 separate SQL statements is because I am using this as a SQL Data Source, which can only have 1 select statement.

Original source