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
Related
I faced a problem with a SQL query. I have a table with 10 fields.
I need to create a query, which gets date by field ProductionYear(int) between 2 variables #startDate(int) and #endDate(int). Both of these variables are unnecessary. And I need to build a SQL query with following conditions:
If(#endDate = 0)
Select Id from MyTable where ProductionYear > #startDate
else
Select Id from MyTable where ProductionYear BETWEEN #startDate and #endDate.
How can I build a query with those conditions?
You can incorporate this into a single query:
Select Id
from MyTable
where ProductionYear >= #startDate and
(ProductionYear <= #endDate or #endDate = 0);
Your two queries are inconsistent on whether #startDate is included. BETWEEN includes the comparison values, but > does not.
If you want #startDate to also be optional:
Select Id
from MyTable
where (ProductionYear >= #startDate or #startDate = 0) and
(ProductionYear <= #endDate or #endDate = 0);
Some additional comments. Calling a "year" a "date" is confusing. Your parameters should probably be called #startYear and #endYear.
These queries are going to result in full table scans of your table. This is probably not a big deal, because the granularity is by year. If the granularity were more refined, you might want to use an index. In that case, perhaps the best approach is dynamic SQL.
you can try by using case when
Select Id from MyTable
where ProductionYear BETWEEN (case when #startDate>#endDate then #endDate
else #startDate end) and
(case when #startDate>#endDate then #startDate else #endDate end)
#dateFrom and #dateTo are the variables coming from outside users. They may can put a date in, or choose not to have any range and want to see all data,
SELECT *
FROM tbl
WHERE --
I want to implement following conditions
If #dateFrom and #dateTO both variable have values, then select data in range
If #dateFrom has value and #dateTo don't, then select data range starting from #dateFrom with no ending range.
If #dateTo has value and #dateFrom don't, then select data range ending at #dateTo with no starting range.
If non of the variable exist, return all the data.
You can try OR with AND and your logic.
SELECT * FROM tbl
WHERE
(#dateFrom IS NULL OR DateFrom > #dateFrom)
AND
(#dateTo IS NULL OR DateTo < #dateTo)
You can use ISNULL for this purpose
SELECT * FROM tbl
WHERE ISNULL(DateFrom,'')=COALESCE(#dateFrom,DateFrom,'')
AND ISNULL(DateTo,'')=COALESCE(#dateTo,DateTo,'')
You can try BETWEEN Operator with ISNULL
SELECT * FROM Table1
WHERE ISNULL(OrderDate,'') BETWEEN COALESCE(#DateFrom,OrderDate,'') AND COALESCE(#DateTo,OrderDate,'')
Edited: Zohar's approach for NULL value in Table's Column.
Now it's working perfect!
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
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)
)
)
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