I need to know how to do this:
WHERE (if #date is not null) date = #Date
It's only one condition to do if #date is not null else there is no condition to do. I read something like this
WHERE date = IIF (#Date IS NOT NULL, #date , )
But it doesn't work for this case.
Thanks!
WHERE (date = #Date OR #Date IS NULL)
This would check if your column is equal to the parameter; if the parameter is null, then date column wouldnt need to match with the parameter value
Use CASE Statement in WHERE Clause instead of IFF
SELECT * FROM Your_table
WHERE Date = CASE WHEN #date IS NULL THEN Date ELSE #Date END
WHERE Date=CASE WHEN #date IS NULL THEN Date ELSE #Date END
Related
I have some data stored in SQL Server which contains dates (date datatype). I am currently using a BETWEEN clause to filter my records in dates range, something like this ...
SELECT
*
FROM
Report
WHERE
ReportDate BETWEEN '2016-08-01' AND '2017-08-01'
Is it possible to use BETWEEN and LIKE clause at the same time or something close to that so that whenever a user doesn't specify any dates he/she would be able to pull all the reports? So far the query below
SELECT
*
FROM
Report
WHERE
ReportDate BETWEEN '' AND ''
doesn't show any records at all. Is there a way of doing this ..?
Use NULL with a parameter... if no value is given for #startDate and #endDate then the default will be NULL for these parameters, and the second WHERE condition would be met, returning all records.
create proc myProc(#startDate datetime = null, #endDate datetime = null)
as
SELECT * FROM Report
WHERE
(ReportDate BETWEEN #startDate AND #endDate)
or
(#startDate is null and #endDate is null)
Also, if your field is a DATETIME then this blog by Aaron is well worth your read.
Also, this method means the user has to enter both or neither of the parameters. If that's not what you'd want just let us know.
I think the correct logic would be:
SELECT r.*
FROM Report r
WHERE (ReportDate >= #startDate OR #startDate IS NULL) AND
(ReportDate <= #endDate OR #endDate IS NULL);
This works when only one of the values is NULL.
Note:
I would go with Aaron Bertrand's advice and really use:
SELECT r.*
FROM Report r
WHERE (ReportDate >= #startDate OR #startDate IS NULL) AND
(ReportDate < DATEADD(day, 1, #endDate) OR #endDate IS NULL);
I'm trying to figure out a query that if start date and end date are NULL, then search between the current date and the last 30 days. If the start date and end date are NOT NULL, then search between the start date and end date. I am having a hard time figuring out the query structure. This is for SQL Server 2016. So far I have:
CREATE PROCEDURE dbo.getInboundTrackingGraphs
#chartType nvarchar(50) = null,
#geoID int = null,
#startDate date = null,
#endDate date = null,
#currentDate date,
#previous30Days date
AS
BEGIN
SET NOCOUNT ON;
SET #currentDate = GETDATE()
SET #previous30Days = DATEADD(DAY, -30, GETDATE())
SELECT *
FROM InboundTicket
WHERE DateOpened BETWEEN
CASE WHEN #startDate IS NULL AND #endDate IS NULL THEN #currentDate AND #previous30Days
ELSE #startDate AND #endDate
END
END
Error message:
Incorrect syntax near the keyword 'AND'.
The use of COALESCE can make this very simple.
SELECT *
FROM InboundTicket
WHERE DateOpened >= COALESCE(#startDate, #previous30Days)
AND DateOpened <= COALESCE(#endDate, #currentDate)
Noel
First, you don't need the CASE. Simple boolean logic will do:
WHERE (#startDate IS NULL AND #endDate IS NULL AND
DateOpened >= #previous30Days AND DateOpened <= #currentDate
) OR
(DateOpened >= #startDate AND DateOpened <= #endDate)
Second, don't use BETWEEN. You are already confused by it. In your case the confusion is putting the more recent date first. So, keep your life simple and use explicit comparisons.
Note that this version will return nothing if only one of the dates is NULL. That is the logic in your question. I suspect it is not the best approach.
And, the final condition doesn't need to test for NULL, because the comparisons will never return true if any of the operands are NULL.
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
how can i use greater than equal to and less than equal to instead of using between in SQL
I'm checking for date variable in sql
i tried like this.
Date between coalesce(#fromDate,Date) and coalesce(#toDate,Date)
but if user does not enter any of date (fromDate or toDate)
so that I need to convert above condition in greater than equal to and less than equal to
please help in syntax in sql.
thanks
IF #fromDate IS NULL
BEGIN
SET #fromDate = '1900-01-01';
END;
IF #toDate IS NULL
BEGIN
SET #toDate = '2099-01-01';
END;
SELECT things
FROM table
WHERE date_field BETWEEN #toDate AND #fromDate;
This code will essentially give you an arbitrarily large range to check which should give you reasonable performance and return all results (assuming that's what you want when neither value is supplied).
This code can be shortened to:
SELECT things
FROM table
WHERE date_field BETWEEN Coalesce(#toDate, '1900-01-01') AND Coalesce(#fromDate, '2099-01-01');
But I kept the verbose version to illustrate.
Try this
SELECT Date FROM TableName WHERE Date > #fromDate AND Date < #toDate
SELECT *
FROM dbo.Account AS a
WHERE 1 = 1
AND (
#toDate IS NULL
OR a.CreateDate <= #toDate
)
AND (
#fromDate IS NULL
OR a.CreateDate >= #fromDate
)
Please note the 1 = 1 is only there to make the conditions clear and is by no means needed.
This should be what the where clause would look like.
Where
Date >= #fromDate
And
Date <= #toDate
Good day, I receive 2 nullable date parameters in my stored procedure. #StartDate and #EndDate. I need to execute my procedure as normal if these two parameters are NULL, else the #StartDate param needs to be >= my StartDateTime value AND #EndDate param needs to be <= my EndDateTime value.
Below is a snippet of what I am trying to accomplish but are not sure of the syntax.
FROM DI_Intervention_Schedule S
WHERE
(
#ID IS NULL
OR S.[ID] = #ID
)
AND (
CASE #StartDate WHEN IS NOT NULL THEN
#StartDate >= S.[StartDateTime] AND #EndDate <= S.[EndDateTime]
END
)
Any help please?
Try below code :
FROM DI_Intervention_Schedule S
WHERE
(S.[ID] = #ID OR #ID IS NULL)
AND (( S.[StartDateTime] >= #StartDate AND S.[EndDateTime] <= #EndDate) OR #StartDate IS NULL )