Conditional WHERE clause in SQL Server
Try this
SELECT
DateAppr,
TimeAppr,
TAT,
LaserLTR,
Permit,
LtrPrinter,
JobName,
JobNumber,
JobDesc,
ActQty,
(ActQty-LtrPrinted) AS L,
(ActQty-QtyInserted) AS M,
((ActQty-LtrPrinted)-(ActQty-QtyInserted)) AS N
FROM
[test].[dbo].[MM]
WHERE
DateDropped = 0
AND (
(ISNULL(@JobsOnHold, 0) = 1 AND DateAppr >= 0)
OR
(ISNULL(@JobsOnHold, 0) != 1 AND DateAppr != 0)
)
You can read more about conditional WHERE here.
How do I create a conditional WHERE clause?
Could you just do the following?
SELECT
*
FROM
Table
WHERE
(@booleanResult = 1
AND Column1 = 'value1')
OR
(@booleanResult = 0
AND Column1 = 'value1'
AND Column2 = 'value2')
SQL Server conditional where clause based on declared variable
You are looking for this
DECLARE @ITEST INT = 1
SELECT NAME, LNAME, CADDRESS
FROM JEEVEN
WHERE (@ITEST = 1 AND EVEN_KEY > 5 AND EVEN_KEY < 10)
OR (@ITEST = 2 AND EVEN_KEY > 20 AND EVEN_KEY < 30)
Conditional Where clause in SQL query if one or both date range parameters are null
Don't cast your column, it'll be bad for performance. I also assume that your parameters are date
data types, but that your column is actually a date and time (such as a datetime2
), due to your casting. You can then just use a single ISNULL
on the parameters, and then an OR
for when they are both NULL
:
WHERE (CreateDate >= @StartDate AND CreateDate < DATEADD(DAY,1,ISNULL(@EndDate,@StartDate))
OR (@StartDate IS NULL AND @EndDate IS NULL)
You'll probably want to put an OPTION (RECOMPILE)
in there too, as the plan for when they both have NULL
could be very different to when they don't.
SQL - Conditional WHERE clause
Couple those with some OR statements:
SELECT *
FROM
Customer c
WHERE
c.StateID=@StateID
AND ( c.CountyID=@CountyID OR @CountyID IS NULL )
AND ( c.CityID=@CityID OR @CityID IS NULL )
For each of those parameters, if it is null, then the check is basically ignored.
Conditional Where Statement
Something like this:
WHERE (cte1.counte > 1 and cte1.built is not null or cte1.counte <= 1) and
(cte2.countd > 1 and cte2.demo is not null or cte2.countd <= 1)
Conditional WHERE statement SQL
If I understand you question correctly I believe you want
SELECT * FROM
`...`
WHERE product = 'core news' OR (product <> 'core news' AND type <> 'video')
ORDER BY product, metric, type
how to add conditional where clause in sql
This can be achieved with conditional clauses.
$users = DB::table('users')
->where('aid', 2)
->when($cid, function ($query) {
$query->where('cid', 1);
})
->get();
Related Topics
Temporary Table Record Limit in SQL Server
How to Select Column Which Field Name Contains a Dot
Openrowset and Opendataset Without Sysadmin Rights
Getting Extra Rows - After Joing the 3 Tables Using Left Join
Count Distinct Over Partition by SQL
Ora-12728: Invalid Range in Regular Expression
Getting Unavailable Dates for Renting a Product That Has Stocks
Stored Procedure Failing on a Specific User
Insert Blank Row Between Groups of Rows and Sorted by Id in SQL
How to Extend the Query to Add 0 in the Cell When No Activity Is Performed
How to Pass Column Name as Parameter in Select Statement SQL Server
Sql: Difference Between Two Dates
Using Openxml in SQL Server 2008 Stored Proc - Insert Order Differs from Xml Document
How to Specify SQL Sort Order in SQL Query
Rename a Column in All the Tables - SQL
Update Statement in Oracle Using SQL or Pl/SQL to Update First Duplicate Row Only
How to Prevent Deletion of the First Row in Table (Postgresql)