check date between two dates or NULL in sql - mysql

I want to check date in between two dates or can pass as a NULL
using stored proc
code
declare #fromDate date = null
declare #toDate date = null
select * from Mytable
where date betweeen #fromDate and #toDate OR NULL (how to check for both parameters)
I have other 2 more parameters so irrespective with date result should be displayed.
if #todate and #fromDate is NULL
please help.

Try with coalesce function as below
declare #fromDate date = null
declare #toDate date = null
select * from Mytable
where date between coalesce(#fromDate,date) and coalesce(#toDate,date)

Pretty much convert your English to SQL:
where (date is null or date betweeen #fromDate and #toDate)

When searching for a better solution than "between" with "coalesce" i found this thread:
firebird SQL
param is never NULL
createdatum is never NULL
offdatum can be NULL - for obvious reasons ;)
Initial solution:
where
X = :X
AND
cast(:QryDateTime as DATE) between
createdatum and coalesce(offdatum, cast(:QryDateTime as DATE))
problem there was a little tricky: when using "now" as value for QryDateTime there was (very rarely) no result found - even if there should exist one
Problem was that now is evaluated every time separately and obviously not in the left-to-right order.
I finally changed it to:
where
X = :X
AND
iif(offdatum is null, 1, iif(offdatum > cast(:QryDateTime as DATE), 1, 0)) = 1
AND
cast(:QryDateTime as DATE) > createdatum

Related

How to use BETWEEN for datetime stored procedure

I am creating a stored procedure for searching. DateOrdered is column name in table of datetime type.
The problem is that I want to perform search on this column. The user can search on start date and end date. Also user can send null for any parameter, like start date or end date.
When the user will not send the start date or end date, I shall search on another option. My problem is that how can I handle this below is the query I tried, but without success
SELECT
#C_Order_ID = C_Order_ID
FROM
C_Order COrder
WHERE
(#AD_Org_ID IS NULL OR
COrder.AD_Org_ID IN (SELECT ID FROM fnSplitter(#AD_Org_ID)))
AND (#AD_Client_ID IS NULL OR
#AD_Client_ID IN (SELECT ID FROM fnSplitter(#AD_Client_ID)))
AND (#IsActive IS NULL OR COrder.IsActive = #IsActive)
AND (#startDate IS NULL OR
COrder.DateOrdered = #startDate BETWEEN #EndDate IS NULL
OR COrder.DateOrdered = #EndDate)
Thanks for your reply .
You may try like this:
COrder.DateOrdered BETWEEN #startDate AND #EndDate
So your query would be like
SELECT #C_Order_ID= C_Order_ID FROM C_Order COrder
WHERE
(#AD_Org_ID IS NULL OR COrder.AD_Org_ID IN (Select ID From fnSplitter(#AD_Org_ID)))
AND (#AD_Client_ID IS NULL OR #AD_Client_ID IN (Select ID From fnSplitter(#AD_Client_ID)))
AND (#IsActive IS NULL OR COrder.IsActive = #IsActive)
AND (#startDate IS NULL OR COrder.DateOrdered BETWEEN isnull(#startDate,'') AND isnull(#EndDate,''))
or better
SELECT #C_Order_ID= C_Order_ID FROM C_Order COrder
WHERE
(#AD_Org_ID IS NULL OR COrder.AD_Org_ID IN (Select ID From fnSplitter(#AD_Org_ID)))
AND (#AD_Client_ID IS NULL OR #AD_Client_ID IN (Select ID From fnSplitter(#AD_Client_ID)))
AND (#IsActive IS NULL OR COrder.IsActive = #IsActive)
AND (CAST(#startDate AS DATE) IS NULL OR (CAST(#startDate AS DATE) IS NULL OR CAST(COrder.DateOrdered AS DATE) >=CAST(#startDate AS DATE))
AND (CAST(#endDate AS DATE) IS NULL OR CAST(COrder.DateOrdered AS DATE) <=CAST(#endDate AS DATE))
SELECT #C_Order_ID= C_Order_ID FROM C_Order COrder
WHERE
(#AD_Org_ID IS NULL OR COrder.AD_Org_ID IN (Select ID From fnSplitter(#AD_Org_ID)))
AND (#AD_Client_ID IS NULL OR #AD_Client_ID IN (Select ID From fnSplitter(#AD_Client_ID)))
AND (#IsActive IS NULL OR COrder.IsActive = #IsActive)
AND (#startDate IS NULL OR COrder.DateOrdered BETWEEN #startDate AND #EndDate
OR COrder.DateOrdered = #EndDate)
Aaron Bertrand has written a good article about using BETWEEN with date ranges - What do BETWEEN and the devil have in common?, this is worth a read. Also it looks like you are passing a comma separated list in a string, then splitting it with a function fnsplitter, you may want to consider using table-valued paramters
However, to actually answer your question you can use the above idea of not using between and change your query to:
AND (#startDate IS NULL OR COrder.DateOrdered >= #startDate)
AND (#EndDate IS NULL OR COrder.DateOrdered <= #EndDate)
So you have 4 permutations.
Both #StartDate and #EndDate are null - returns all dates
#StartDate is not null and #EndDate is null - returns all dates after #StartDate
#StartDate is null and #EndDate is not null - returns all dates before #EndDate
Both #StartDate and #EndDate are not null - returns all dates between #StartDate and #EndDate
Based on your comments on another answer, it sounds like you may be passing a date as the #EndDate parameter, e.g. 2014-08-28, but still want to include all records on that date, even if they have a time associated, e.g. 2014-08-28 12:00, this is exactly why BETWEEN is not used for date ranges, instead you need to use the less than operator and add 1 day to your End date:
AND (#startDate IS NULL OR COrder.DateOrdered >= #startDate)
AND (#EndDate IS NULL OR COrder.DateOrdered < DATEADD(DAY, 1, #EndDate))

Simple T-SQL function to convert date to displayable format

I have a strange bug that I cannot resolve (with a one line function).
This code works:
DECLARE #TestDate datetime = '2013-05-01 23:15:11'
select IsNull(convert(varchar(max), #TestDate, 120), 'null') as 'test1'
Displays: 2013-05-01 23:15:11
CREATE FUNCTION [dbo].[DateOrNullToChar] (#InputDate date)
RETURNS VARCHAR(40)
BEGIN
return ISNULL(convert(varchar(40),#InputDate, 120),'null');
END
select dbo.DateOrNullToChar('2013-05-01 23:15:11') as 'result'
Returns: 2013-05-01 (no time)
I have also tried varchar(max).
The purpose of the function is for something like this:
Set #ErrorMessage = ' #ArrivalDate=' + dbo.DateOrNullToChar(#ArrivalDate) +
' #DepartureDate=' + dbo.DateOrNullToChar(#DepartureDate);
If any one value is null, the whole value becomes null. So I want to see the string 'null' when a date has a null value.
#InputDate should be datetime or datetime2 if you want time to be shown
The clues are in the code...
#TestDate datetime
#InputDate date
You need to make the parameter type to be datetime instead of date:
CREATE FUNCTION [dbo].[DateOrNullToChar] (#InputDate datetime)
It's silently converting the string to your date parameter type and thus dropping the time portion.

How to give date range in a month & Year format in where clause sql server

I am trying to give date range in where clause like below.. however its not giving correct o/p
declare #StartDate DATETIME, #EndDate DATETIME
set #StartDate='9/01/2011'
set #EndDate='1/30/2012'
Select * from mytable Where MONTH(WT.ToDate) >= MONTH(#StartDate) AND MONTH(WT.ToDate) <=MONTH(#Enddate)
AND YEAR(WT.ToDate)>= YEAR(#StartDate) AND YEAR(WT.ToDate) <=YEAR(#Enddate)
Please help
What is WT.ToDate, What WT is referring to?
it should be.
declare #StartDate DATETIME, #EndDate DATETIME
set #StartDate='9/01/2011'
set #EndDate='1/30/2012'
Select * from mytable as WT Where MONTH(WT.ToDate) >= MONTH(#StartDate) AND MONTH(WT.ToDate) <=MONTH(#Enddate)
AND YEAR(WT.ToDate)>= YEAR(#StartDate) AND YEAR(WT.ToDate) <=YEAR(#Enddate)
Firstly, note I've changed the format you've specified the dates in to avoid any risk of misinterpretation (now yyyyMMdd).
Secondly, try a clause like this:
declare #StartDate DATETIME, #EndDate DATETIME
set #StartDate='20110901'
set #EndDate='20120130'
Select *
from mytable
Where WT.ToDate >= #StartDate AND WT.ToDate < #EndDate
Note, this will not return rows for 30 Jan 2012, so just tweak your #EndDate as appropriate

T-SQL IF Block Not Executing Though Conditions Are Met

I've got the following SQL statement that I am working on. I've trimmed it down to the parts necessary to illustrate the problem.
DECLARE #mwareId as int = 9647,
#startDate as datetime = '2011-07-20',
#endDate as datetime = '2011-07-20'
IF OBJECT_ID('tempdb..#tmpInvoiceList', 'U') IS NOT NULL DROP TABLE #tmpInvoiceList
-- Get base invoice list for customer
SELECT invoiceId
,invoiceNumber
,customerId
,customerName
,customerCode
,createDate
,lastModifiedDate
,invoiceDate
,totalInvoiceAmount
,statusId
,isPaid
INTO #tmpInvoiceList
FROM Invoice.Invoice
-- Apply date range if applicable
IF ( #startDate != NULL AND #endDate != NULL )
BEGIN
DELETE FROM #tmpInvoiceList
WHERE invoiceDate NOT BETWEEN #startDate AND #endDate
END
SELECT * FROM #tmpInvoiceList
I have the #startDate and #endDate variables set to date values. The problem is that the if block that applies the date range to the temp table is not executing, though neither of the two variables are null, and I can't find a reason for this.
As far as I'm aware, TSQL doesn't support the != operator for null checks. Try this:
IF ( #startDate IS NOT NULL AND #endDate IS NOT NULL )
IS [NOT] NULL is the right way to compare value with NULL. All of (NULL != NULL) , (1!= NULL), (NULL =NULL) evaluate as NULL(false)
You have to use IS NOT NULL instead of != NULL
IF ( #startDate IS NOT NULL AND #endDate IS NOT NULL )
...

Optional parameters in SQL Server stored procedure

I'm writing some stored procedures in SQL Server 2008. Is the concept of optional input parameters possible here?
I suppose I could always pass in NULL for parameters I don't want to use, check the value in the stored procedure, and then take things from there, but I was interested if the concept is available here.
You can declare it like this:
CREATE PROCEDURE MyProcName
#Parameter1 INT = 1,
#Parameter2 VARCHAR (100) = 'StringValue',
#Parameter3 VARCHAR (100) = NULL
AS
/* Check for the NULL / default value (indicating nothing was passed) */
if (#Parameter3 IS NULL)
BEGIN
/* Whatever code you desire for a missing parameter */
INSERT INTO ........
END
/* And use it in the query as so */
SELECT *
FROM Table
WHERE Column = #Parameter
Yes, it is. Declare the parameter as so:
#Sort varchar(50) = NULL
Now you don't even have to pass the parameter in. It will default to NULL (or whatever you choose to default to).
In SQL Server 2014 and above at least, you can set a default, and it will take that and not error when you do not pass that parameter.
Partial example: the third parameter is added as optional. Execution (exec) of the actual procedure with only the first two parameters worked fine.
exec getlist 47,1,0
create procedure getlist
#convId int,
#SortOrder int,
#contestantsOnly bit = 0
as
The default mentioned in previous answers only works for simple cases. In more complicated cases, I use an IF clause near the beginning of the stored procedure to provide a value, if the parameter is NULL or empty and calculations are required.
I often use optional parameters in the WHERE clause, and discovered that SQL does not short circuit logic, so use a CASE statement to make sure not to try to evaluate NULL or empty dates or unique identifiers, like so:
CREATE Procedure ActivityReport
(
#FromDate varchar(50) = NULL,
#ToDate varchar(50) = NULL
)
AS
SET ARITHABORT ON
IF #ToDate IS NULL OR #ToDate = '' BEGIN
SET #ToDate = CONVERT(varchar, GETDATE(), 101)
END
SELECT ActivityDate, Details
FROM Activity
WHERE
1 = CASE
WHEN #FromDate IS NULL THEN 1
WHEN #FromDate = '' THEN 1
WHEN ActivityDate >= #FromDate AND ActivityDate < DATEADD(DD,1,#ToDate) THEN 1
ELSE 0
END