conditional 'and' in where clause depending existing value? - sql

Can anybody knows if I could use a "conditional 'and' in where clause" depending of an existing value??, I have a query like this:
#declare idProduct int = 1,
#declare idProducType int = null
select
tbl.idProduct, tbl.idProductType, tbl.ProductName, tbl.ProductPrice
from MyTable tbl
where tbl.idProduct = 1
<IF idProductType is not null>
and tbl.idProductType = SOMEVALUE
<ELSE>
do nothing
<ENDIF>
I require get data from this query always instead of getting null values when condition is false. how I can do like this in SQL Server 2008 ???

I think this is what you want:
select
tbl.idProduct, tbl.idProductType, tbl.ProductName, tbl.ProductPrice
from MyTable tbl
where tbl.idProduct = 1 and (tbl.idProductType = #idProducType or #idProducType is null)

This should work for you:
#declare idProduct int = 1,
#declare idProducType int = null
SELECT
tbl.idProduct, tbl.idProductType, tbl.ProductName, tbl.ProductPrice
FROM MyTable tbl
WHERE tbl.idProduct = 1
AND (CASE WHEN idProductType is not null THEN tbl.idProductType = SOMEVALUE
END)

....
where tbl.idProduct = 1
and (CASE WHEN idProductType is not null and tbl.idProductType = SOMEVALUE Then <something> END) = <something>
/

Related

Inserting value based on any condition in SQL

declare #varID as INT = 2
insert into table1
(
ID
,varName
)
select
ID
,**#####**
from table2
I want value of varName based on #varID. How can I do that?
What should be in place of #####?
Statement that replicate IF #varname = 2 then 'P' else 'I'.
You are very close. Just mention the variable in the SELECT clause. Like this.
declare #varID as INT = 2
insert into table1 (ID ,varName)
select ID,
CASE WHEN #varID = 2 THEN 'P' ELSE 'I' END
from table2
Maybe a CASE WHEN THEN ELSE END statement like below
declare #varID as INT = 2
insert into table1
(
ID
,varName
)
select
ID
,CASE WHEN #varID=2 THEN 'P' ELSE 'I' END
from table2

Range of values as ELSE clause in a CASE statement

I have a stored proc which is supposed to filter results based on an integer which can be NULL and I need to be able to choose results that either match the integer or retrieve all results but I can't figure out the syntax for the "All" results part. Query is basically this:
Select * from dbo.TABLE
WHERE [IDField] =
CASE
WHEN ISNULL(#ID, 0) > 0 THEN #ID
ELSE ?????
END
I want to use an IN range to include all the values because setting it to 0 or Null will return no results, but this doesn't work:
Select * from dbo.TABLE
WHERE [IDField] =
CASE
WHEN ISNULL(#ID, 0) > 0 THEN #ID
ELSE 1 OR 2 OR 3
END
or this:
Select * from dbo.TABLE
WHERE [IDField] IN (
CASE
WHEN ISNULL(#mID, 0) > 0 THEN #mID
ELSE 1,2,3
END
)
Phrase this without the case:
SELECT *
FROM dbo.TABLE
WHERE #id is null OR IDField = #id;
Then you can expand this to:
SELECT *
FROM dbo.TABLE
WHERE (COALESCE(#ID, 0) > 0 AND IDField = #id) OR
IDField IN (1, 2, 3)

IF ELSE in where block of sql query

I have the below stored procedure
CREATE PROCEDURE sp1
(
#param1 bit,
#parma2 bit
)
AS
BEGIN
SELECT * from Table1 where
if #param1 = 1 then column1 is null
else if #parma2 = 1 then column1 is not null
END
Obviously the If else part in the where clause is not correct . Could anyone suggest how to tackle it ?
Something like this:
SELECT * from Table1 where
case
when #param1 = 1 then [column 1] is null
when #param2 = 1 then [column 1] is not null
end
But you don't need two parameters for this procedure, only one should suffice.
Try the following:
SELECT
Case WHEN #param1 = 1 THEN Null
Case WHEN #param2 = 1 THEN column1 Else '' End as column1 ,
* FROM Table1
The above statement will set column1 to null if param1 = 1, it will get the value of column1 if param2 = 1 otherwise it will set it to empty string.
Seems to me like the simplest option would be to use AND and OR:
SELECT *
FROM Table1
WHERE (#param1 = 1 AND column1 is null)
OR (#parma2 = 1 AND column1 is not null)
Use this:
CASE WHEN condition THEN value ELSE value END

How to set bool value in SQL

I have a column which is bool. How can I set true, false values for that? Here is my query :
Update [mydb].[dbo].[myTable]
SET isTrue =
(
CASE WHEN Name = 'Jason' THEN 1
END
)
I don't know what to write after THEN keyword. Should I write 1 or true or 1 AS BIT or something else?
Sql server does not expose a boolean data type which can be used in queries.
Instead, it has a bit data type where the possible values are 0 or 1.
So to answer your question, you should use 1 to indicate a true value, 0 to indicate a false value, or null to indicate an unknown value.
Update [mydb].[dbo].[myTable]
SET isTrue =
CASE WHEN Name = 'Jason' THEN
1
ELSE
0
END
The query you added will work fine, but it will you have to take care of "FALSE" part as well otherwise it will try to enter NULL in your column.
Do you have any default value constrain on isTrue column?
Update [mydb].[dbo].[myTable]
SET isTrue =
(
CASE WHEN Name = 'Jason' THEN 1 ELSE 0
END
)
You need case statement with when and else if not any condition satisfied
Update [mydb].[dbo].[myTable]
SET isTrue = ( CASE WHEN Name = 'Jason'
THEN 1 else 0
END)
Use IIF function of sql server
DECLARE #a int = 45, #b int = 40;
DECLARE #a int = 45, #b int = 40;
SELECT IIF ( #a > #b, 'TRUE', 'FALSE' ) AS Result;
Result
--------
TRUE
(1 row(s) affected)
For Your Problem
Update [mydb].[dbo].[myTable]
SET isTrue = ( Name = 'Jason', 'TRUE', 'FALSE' )

Sql Server Where Case Then Is Null Else Is Not Null

I have a procedure which receive a bit variable called #FL_FINALIZADA.
If it is null or false I want to restrict my select to show only the rows that contain null DT_FINALIZACAO values. Otherwise I want to show the rows containing not null DT_FINALIZACAO values.
Something like this:
SELECT
*
FROM
MyTable
WHERE
...
AND
(
OPE.DT_FINALIZACAO = (
CASE
WHEN (#FL_FINALIZADA <> 1)
THEN NULL
END
) OR
OPE.DT_FINALIZACAO IS NOT NULL
)
In this case I receive the message:
None of the result expressions in a
CASE specification can be NULL.
How can I achieve this?
Thanks in advance.
SELECT
*
FROM
MyTable
WHERE
(ISNULL(#FL_FINALIZADA, 0) = 0
AND
OPE.DT_FINALIZACAO IS NULL
)
OR
(#FL_FINALIZADA = 1
AND
OPE.DT_FINALIZACAO IS NOT NULL
)
Change the AND to be:
AND (((#FL_FINALIZADA <> 1) AND (OPE.DT_FINALIZACAO IS NULL)) OR ( (#FL_FINALIZADA = 1) AND (OPE.DT_FINALIZACAO IS NOT NULL)))
If the bit flag is 1 then DT_FINALIZACAO can't be null.
IF #FL_FINALIZADA IS NULL
SET #FL_FINALIZADA = 0
SELECT * FROM NewsletterSubscribers
WHERE
(#FL_FINALIZADA = 0 AND OPE.DT_FINALIZACAO IS NULL)
OR
(#FL_FINALIZADA = 1 AND OPE.DT_FINALIZACAO IS NOT NULL)
My detailed SQL is a little rusty, but have you tried using 0 insted of NULL? I would expect 0 to evaluate the same as NULL in that select