My previous question was this Case Statement With Between Clause In Sql Server
I got a solution and it works. But the problem is when I add another column to the WHERE clause, it won't filter with this new column:
SELECT
tbl1.Column1, tbl1.Column2
FROM
table1 tbl1
WHERE
EmployeeId = CASE WHEN #employeeId = '' THEN tbl1.EmployeeId
ELSE #employeeId END
AND (#DateTo = '1900-01-01' AND #DateFrom = '1900-01-01') OR
(NOT (#DateTo = '1900-01-01' AND #DateFrom = '1900-01-01')
AND (CAST(tbl1.txnDate AS DATE) BETWEEN #DateFrom AND #DateTo))
This code is what I am trying to use. But it doesn't work. I tried hard-coding a value for EmployeeId. But it doesn't work
You must Use ( and ) in where clause where you use And and OR together in order to define priority of each condition:
SELECT tbl1.Column1,tbl1.Column2
FROM table1 tbl1
WHERE EmployeeId = CASE WHEN #employeeId = '' THEN tbl1.EmployeeId ELSE #employeeId END
AND (
(#DateTo = '1900-01-01' AND #DateFrom = '1900-01-01')
OR (
NOT (#DateTo = '1900-01-01' AND #DateFrom = '1900-01-01')
AND (CAST(tbl1.txnDate AS DATE) BETWEEN #DateFrom AND #DateTo)
)
)
Related
I have a stored procedure in SQL Server.
The SELECT part for this SP is as follows :
SELECT * FROM demoTable dt
INNER JOIN demoTable2 dt2
WHERE
(#StartDate IS NULL OR dt.StartDate = #StartDate OR (dt.StartDate >= #StartDate and dt.EndDate <= #EndDate))
AND (#EndDate IS NULL OR dt.EndDate = #EndDate OR (dt.StartDate >= #StartDate and dt.EndDate <= #EndDate))
Now I want to modify the above WHERE clause part after the AND condition.
I have added 3 new parameters/variables which are being passed to this SP.
#ID int , #isAdmin int and #Date varchar(50)
Using the values of these variables I'm trying to manipulate the above WHERE clause as follows :
WHERE
#StartDate IS NULL OR dt.StartDate = #StartDate OR (dt.StartDate >= #StartDate and dt.EndDate <= #EndDate))
-- Logic change below
AND(
CASE WHEN (#ID != 100 AND #isAdmin != 1)
THEN (#EndDate IS NULL OR dt.EndDate = #EndDate OR (dt.StartDate >= #StartDate and dt.EndDate <= #EndDate))
ELSE (dt.EndDate>sysdatetime())
END)
But after this modification I'm receiving an error.
Error statement :
Incorrect syntax near the keyword 'IS'.
This error is pointing to the line where I've used IS NULL from the modified logic part.
Basically what I want to do is to apply the original logic for AND i.e.
(#EndDate IS NULL OR dt.EndDate = #EndDate OR (dt.StartDate >= #StartDate and dt.EndDate <= #EndDate))
only when the
(#ID != 100 AND isAdmin != 1)
holds true or else I want to apply the new logic i.e.
(dt.EndDate>sysdatetime()
How can I achieve this?
If you want a special condition for #ID !=100 AND isAdmin !=1
You don't need case/when, you need just a valid boolean expression
change :
AND(
CASE WHEN (#ID != 100 AND isAdmin != 1)
THEN (#EndDate IS NULL OR dt.EndDate = #EndDate OR (dt.StartDate >=
#StartDate and dt.EndDate <= #EndDate))
ELSE (dt.EndDate>sysdatetime())
END
)
to
AND(
((#ID != 100 AND isAdmin != 1) AND (#EndDate IS NULL OR dt.EndDate = #EndDate OR (dt.StartDate >= #StartDate and dt.EndDate <= #EndDate)))
OR
((#ID == 100 OR isAdmin == 1) AND (dt.EndDate>sysdatetime()) )
)
I need to create a SQL statement to check where condition if the column is null, it will do other condition. Below is my example:
SELECT
REPLACE(xxShip.Shipment_Date, '/', '-') AS ShipDate,xxShip.Customer,
xxShip.TravelSheetNo
FROM
(SELECT
RANK() OVER (PARTITION BY TravelSheetNo ORDER BY created_on DESC) AS shipwocode,
ShipDate, ShipTime, Shipment_Date,
Customer, Product_no, TravelSheetNo,
[status], ProcessLotNo
FROM
xxDEShipment) xxShip
WHERE
xxShip.shipwocode = 1
AND xxShip.TravelSheetNo = 'ABC'
--here i need to check the condition----------------------------
AND (CASE
WHEN ISNULL(xxShip.Shipment_Date,'') != '' OR xxShip.Shipment_Date != ''
THEN
xxShip.Shipment_Date IS NULL OR xxShip.Shipment_Date = ''
ELSE
CONVERT(DATETIME, xxShip.Shipment_Date) >= #DATESTART and CONVERT(DATETIME, xxShip.Shipment_Date) <= #DATEEND
END)
Please help. Thank you
This should be something like this:
AND(
(ISNULL(xxShip.Shipment_Date,'') <> '' AND
CONVERT(DATETIME, xxShip.Shipment_Date) >= #DATESTART AND
CONVERT(DATETIME, xxShip.Shipment_Date) <= #DATEEND
)
OR ISNULL(xxShip.Shipment_Date,'') == ''
)
Note that storing dates as strings is a bad practice.
If you need to select dates between #DATESTART and #DATEEND or null or empty date, use below:
ISNULL(CONVERT(DATETIME, NULLIF(xxShip.Shipment_Date, '')), #DATESTART)
BETWEEN #DATESTART AND #DATEEND
select
id,
starttime,
endtime,
name
from
table1
where
id != 0
and starttime >= #startdate and endtime <= #enddate
In the above query, how do I perform if the #startdate and #enddate parameter is null by without using if else?
starttime >= #startdate and endtime <= #enddate this condition should check if #startdate and #enddate is not null. else it won't to check.
Any suggestion for this?
you simply check that the variable is null thus returning true as as appropriate
doing this individually for each parameter
where (startdate >= #startdate or #startdate is null)
and (enddate <= #enddate or #enddate is null)
you could also 'ignore' the criteria if either are null
where ((startdate >= #startdate and enddate <= #enddate)
or #startdate is null
or #enddate is null)
All you need is ISNULL or COALESCE function in the where condition.
select id,
starttime,
endtime,
name
from table1
where
id != 0 and
starttime >= ISNULL(#startdate,starttime) and endtime <= ISNULL(#enddate,endtime)
Add extra condition:
and #startdate is not null and #enddate is not null
try this:
for this
should check if #startdate and #enddate is not null. else it won't to check use first answer
if #startdate is null then it will return true and gives all result
select id,
starttime,
endtime,
name
from table1
where
id != 0 and
(starttime >= #startdate or #startdate is null )
and
(endtime <= #enddate or #enddate is null)
or
select id,
starttime,
endtime,
name
from table1
where
id != 0 and
(starttime >= #startdate and #startdate is NOT null )
and
(endtime <= #enddate and #enddate is NOT null)
Try to use OR to check this:
where
(id != 0)
and
(
(#startdate IS NULL) OR (#enddate IS NULL)
OR
(starttime >= #startdate and endtime <= #enddate)
)
I am used to get parameters to my Stored Procedure with a default value ''. I want to check whether if the Date is selected and , if so I want to Execute My Between Clause. In Normal Scenario I am gonna do like this
SELECT tbl1.Column1,tbl1.Column2 FROM table1 tbl1
WHERE tbl1.Column1 = CASE WHEN #Column1Val = '' THEN tbl1.Column1 ELSE #Column1Val END
But I can't Do this with Between Clause. I can't figure a way to do this other than dynamic query. Is there a way other than Dynamic Query?
This is what I am Trying to do
SELECT tbl1.Column1,tbl1.Column2 FROM table1 tbl1
WHERE tbl1.txnDate = CASE WHEN #DateTo = '1900-01-01' AND #DateFrom = '1900-01-01'
THEN CAST(tbl1.txnDate AS DATE)
ELSE CAST(tbl1.txnDate AS DATE) BETWEEN #DateTo AND #DateFrom
END
Try this:
SELECT tbl1.Column1,tbl1.Column2
FROM table1 tbl1
WHERE
(#DateTo = '1900-01-01' AND #DateFrom = '1900-01-01')
OR
(
NOT (#DateTo = '1900-01-01' AND #DateFrom = '1900-01-01')
AND (CAST(tbl1.txnDate AS DATE) BETWEEN #DateFrom AND #DateTo)
)
From what I understand, '1900-01-01' is your default value for start and end dates, so you only need a filter if the user has selected some non-default values for start and end dates. Please let me know if this is what you need.
Demo
I have table called "detail" where i am storing start date and end date of jobs.I have one more table called "leaves" which is also have leave startdate and leave enddate fields.I need to find the nearest available dates of a user without weekends and leave dates.
DECLARE #PackagerLastAssignedDate DATETIME
SELECT #PackagerLastAssignedDate = MAX(EndDate) FROM detail WHERE userId = 1
SELECT lveStartDate,lveEndDate FROM Leaves WHERE UserId = 1 and lveStartDate > #PackagerLastAssignedDate
Thanks In advance
Berlin.M
Try this one -
DECLARE
#DateFrom DATETIME
, #DateTo DATETIME
SELECT
#DateFrom = '20130101'
, #DateTo = '20130202'
SELECT [Date]
FROM (
SELECT [Date] = DATEADD(DAY, sv.number, t.DateFrom)
FROM (
SELECT
DateFrom = #DateFrom
, diff = DATEDIFF(DAY, #DateFrom, #DateTo)
) t
JOIN [master].dbo.spt_values sv ON sv.number <= diff
WHERE sv.[type] = 'p'
) t2
WHERE DATENAME(WEEKDAY, [Date]) NOT IN ('Saturday', 'Sunday')
AND NOT EXISTS (
SELECT 1
FROM dbo.Leaves l
WHERE l.UserId = 1
AND t2.[Date] BETWEEN l.lveStartDate AND l.lveEndDate
)