Conditional filtering using CONTAINS in sql - 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;

Related

How to use case statement inside an SQL select Query

I need to get the output of a selected query depending on certain conditions
Means if(id=uid)
then I need the below query
select * from table1 where id=5;
else
I need the below one
select * from table1 where id=10
I know i can use if condition for this. But my query is very long one so when I use if else then it would look like
if(#id=#uid)
begin
select * from table1 where id=5;// query 1
end
else
select * from table1 where id=10;//query 2
but here I need to replace the entire query once again for a single check. I hope I can do something like this:
declare #id int=4;
declare #uid=10;
select * from table1 where
case
when #id=#uid
then
id=5
else
id=10;
end
Updation
I need one more condition too
in this case id=5 and uid=10
then if(id=uid)
then
select * from table1 where id=5
and
if(id!=uid)
then
select * from table1
something like this
You can use the case expression to return the value id should be equal to:
SELECT *
FROM table1
WHERE id = CASE WHEN #id = #uid THEN 5 ELSE 10 END;
EDIT:
The updated requirement in the question is to return all rows when #id != #uid. This can be done by comparing id to id:
SELECT *
FROM table1
WHERE id = CASE WHEN #id = #uid THEN 5 ELSE id END;
Alternatively, with this updated requirement, a simple or expression might be simpler to use:
SELECT *
FROM table1
WHERE #id = #uid OR id = 5;
SELECT * FROM table1
WHERE (id=5 AND #id=#uid) OR (id=10 AND #id<>#uid)
SELECT
*
FROM
table1
WHERE
(
#id = #uid
AND
id =5
)
OR
(
not #id = #uid
AND
id=10
)

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))

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

Null and not null values in the same parameter

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)

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