Null and not null values in the same parameter - sql

I have this:
Decalre #param int
select * from table where param = #param
sometimes people would like to select records where param is null.
so, something like this:
if #param is null
begin
select * from table where param is null
end
else select * from table where param =#param
Is it possible to incorporate this in a single query?

Should be able to cover it off in a where clause:
select * from table where (param =#param) or (param is null and #param is null)

Would use the coalesce to replace the null value, will act better on performance than the or i think :
SELECT *
FROM table
WHERE Coalesce(#param, 1) = Coalesce(param, 1)

select
*
from table
where (#param is null AND param is null) OR (#param is not null AND param = #param)

Related

Conditional filtering using CONTAINS in sql

I have a parameter called #Param that can be null or not null.
1- I want to filter the table if #Param is not null.
2- I want to use CONTAINS
Something like:
select * from MyTable
where case when #Param is not null then contains(MyColumn, #Param)
else null.
How can I do this?
Something like this probably:
select * from myTable
where #param is null or contains(MyColumn, #Param)
You can do so using AND condition like
select * from MyTable
where (#Param is not null AND contains(MyColumn, #Param))
or #Param is null;

How to use not equal and is null together in where clause in sql server?

http://www.sqlfiddle.com/#!6/be964/5
I expect two result.
If table contains the product key such as 'x', it need to get them.
Otherwise; if table doesn't contain the product key such as 't', it need to get records are null.
I have tried a query like below;
declare #param nvarchar(30);
set #param = 'x' -- or 't'
SELECT * FROM [Test]
WHERE
([ProductKey] = #param and [ProductKey] is not null)
or
([ProductKey] <>#param and [ProductKey] is null)
But It doesn't work.
the where clause in your select works by-line. you only want the productkey is null part when there are no matching product keys if i am not mistaken. i suggest you use the exists function. so when the productkey exists in your table, it uses that or when it does not it uses null:
declare #param varchar;
set #param = 'x' -- or 't'
SELECT * FROM [Test]
WHERE ([ProductKey] = #param)
or ([ProductKey] is null
and not exists (select * from [Test] where [ProductKey] = #param))

Display all records of a table unless I pass a key

Is there an other way to get all rows of a table using a stored procedure unless I pass the procedure a key?
If so, how can I code that in SQL Server?
try this
CREATE PROCEDURE GetData(#key int = null)
BEGIN
SELECT * FROM Table WHERE (#Key Is NULL or id = #Key)
END
You could also use the COALESCE operator:
CREATE PROCEDURE dbo.uspMySprocName(#Key INT = NULL)
AS
BEGIN
SELECT * FROM MyTable WHERE ID = COALESCE(#Key, ID);
END
you can use a if condition and have 2 queries
**syntax:**
if (#key is null ) then
begin
select * from table1 ;
end
else
being
select * from tabel1 where field1 = #key ;
end
endif
The following link should help
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=146620

sql is there a command to check if a row is zero?

I need to check if a row is zero. For nulls I usually use isnull() but my rows are never null. If there is such a command, I would use it like this:
select * from maintable where column1 = iszero(#param,column1)
Assuming your looking for something to work in the same way as IsNull() you could define your own function
create function IsZero(#value int, #default int)
returns int
as
begin
if (#value <> 0)
return #value
return #default
end
Example of usage
select
Returns5 = dbo.Iszero(5, 10),
Returns10 = dbo.Iszero(0, 10)
or in your case
select * from maintable where column1 = IsZero(#param,column1)
SELECT *
FROM maintable
WHERE column1 = CASE #param
WHEN 0 THEN column1
ELSE #param
END
This will give you the record in the maintable with where column1 equals the #param variable. If the #param variable is 0 then it will return all records in the maintable.
Try this:
select *
from maintable
where column1 = #param or #param = 0

Syntax for SQL Server when either of parameter is passed

If one of the paramater is passed / or both passed how to match that to single column in WHERE clause.
If both are passed should get result set just with the first one else with second one.
Ex:
--either will be passed
declare #processStepID int = null
declare #PROCESS_StepID int = null
if(#processStepID is null)
select * from CTL.CTRL.CTRL_PROCESS_STEP_ACTIVITIES
where PROCESS_STEP_ID = #PROCESS_StepID
else
select * from CTL.CTRL.CTRL_PROCESS_STEP_ACTIVITIES
where PROCESS_STEP_ID = #processStepID
How can this be written in where clause not using if statement.
thanks
WHERE PROCESS_STEP_ID = coalesce( #processStepID, #PROCESS_StepID )
Use the ISNULL function
SELECT *
FROM CTL.CTRL.CTRL_PROCESS_STEP_ACTIVITIES
WHERE PROCESS_STEP_ID = ISNULL( #processStepID, #PROCESS_StepID )
select * from CTL.CTRL.CTRL_PROCESS_STEP_ACTIVITIES
where (PROCESS_STEP_ID = #PROCESS_StepID) OR (PROCESS_STEP_ID = #processStepID)
Seems far too obvious. What am I missing?
select *
from CTL.CTRL.CTRL_PROCESS_STEP_ACTIVITIES
where PROCESS_STEP_ID = ISNULL(#PROCESS_StepID,#processStepID)
This should do what you are looking for