Does MS SQL Server's "between" include the range boundaries? - sql

For instance can
SELECT foo
FROM bar
WHERE foo BETWEEN 5 AND 10
select 5 and 10 or they are excluded from the range?

The BETWEEN operator is inclusive.
From Books Online:
BETWEEN returns TRUE if the value of
test_expression is greater than or
equal to the value of begin_expression
and less than or equal to the value of
end_expression.
DateTime Caveat
NB: With DateTimes you have to be careful; if only a date is given the value is taken as of midnight on that day; to avoid missing times within your end date, or repeating the capture of the following day's data at midnight in multiple ranges, your end date should be 3 milliseconds before midnight on of day following your to date. 3 milliseconds because any less than this and the value will be rounded up to midnight the next day.
e.g. to get all values within June 2016 you'd need to run:
where myDateTime between '20160601' and DATEADD(millisecond, -3, '20160701')
i.e.
where myDateTime between '20160601 00:00:00.000' and '20160630 23:59:59.997'
datetime2 and datetimeoffset
Subtracting 3 ms from a date will leave you vulnerable to missing rows from the 3 ms window. The correct solution is also the simplest one:
where myDateTime >= '20160601' AND myDateTime < '20160701'

Yes, but be careful when using between for dates.
BETWEEN '20090101' AND '20090131'
is really interpreted as 12am, or
BETWEEN '20090101 00:00:00' AND '20090131 00:00:00'
so will miss anything that occurred during the day of Jan 31st. In this case, you will have to use:
myDate >= '20090101 00:00:00' AND myDate < '20090201 00:00:00' --CORRECT!
or
BETWEEN '20090101 00:00:00' AND '20090131 23:59:59' --WRONG! (see update!)
UPDATE: It is entirely possible to have records created within that last second of the day, with a datetime as late as 20090101 23:59:59.997!!
For this reason, the BETWEEN (firstday) AND (lastday 23:59:59) approach is not recommended.
Use the myDate >= (firstday) AND myDate < (Lastday+1) approach instead.
Good article on this issue here.

Real world example from SQL Server 2008.
Source data:
ID Start
1 2010-04-30 00:00:01.000
2 2010-04-02 00:00:00.000
3 2010-05-01 00:00:00.000
4 2010-07-31 00:00:00.000
Query:
SELECT
*
FROM
tbl
WHERE
Start BETWEEN '2010-04-01 00:00:00' AND '2010-05-01 00:00:00'
Results:
ID Start
1 2010-04-30 00:00:01.000
2 2010-04-02 00:00:00.000

if you hit this, and don't really want to try and handle adding a day in code, then let the DB do it..
myDate >= '20090101 00:00:00' AND myDate < DATEADD(day,1,'20090101 00:00:00')
If you do include the time portion: make sure it references midnight. Otherwise you can simply omit the time:
myDate >= '20090101' AND myDate < DATEADD(day,1,'20090101')
and not worry about it.

BETWEEN (Transact-SQL)
Specifies a(n) (inclusive) range to test.
test_expression [ NOT ] BETWEEN begin_expression AND end_expression
Arguments
test_expression
Is the expression to test for in the range defined by begin_expression
and end_expression. test_expression
must be the same data type as both
begin_expression and end_expression.
NOT
Specifies that the result of the predicate be negated.
begin_expression
Is any valid expression. begin_expression must be the same data
type as both test_expression and
end_expression.
end_expression
Is any valid expression. end_expression must be the same data
type as both test_expression and
begin_expression.
AND
Acts as a placeholder that indicates test_expression should be
within the range indicated by
begin_expression and end_expression.
Remarks
To specify an exclusive range, use the
greater than (>) and less than
operators (<). If any input to the
BETWEEN or NOT BETWEEN predicate is
NULL, the result is UNKNOWN.
Result Value
BETWEEN returns TRUE if the value of
test_expression is greater than or
equal to the value of begin_expression
and less than or equal to the value of
end_expression.
NOT BETWEEN returns TRUE if the value
of test_expression is less than the
value of begin_expression or greater
than the value of end_expression.

If the column data type is datetime then you can do this following to eliminate time from datetime and compare between date range only.
where cast(getdate() as date) between cast(loginTime as date) and cast(logoutTime as date)

It does includes boundaries.
declare #startDate date = cast('15-NOV-2016' as date)
declare #endDate date = cast('30-NOV-2016' as date)
create table #test (c1 date)
insert into #test values(cast('15-NOV-2016' as date))
insert into #test values(cast('20-NOV-2016' as date))
insert into #test values(cast('30-NOV-2016' as date))
select * from #test where c1 between #startDate and #endDate
drop table #test
RESULT c1
2016-11-15
2016-11-20
2016-11-30
declare #r1 int = 10
declare #r2 int = 15
create table #test1 (c1 int)
insert into #test1 values(10)
insert into #test1 values(15)
insert into #test1 values(11)
select * from #test1 where c1 between #r1 and #r2
drop table #test1
RESULT c1
10
11
15

I've always used this:
WHERE myDate BETWEEN startDate AND (endDate+1)

Related

SQL Server: Inconsistencies in date

create table #temp
(
A date
)
insert into #temp(A)
values(GETDATE())
insert into #temp(A)
values(GETDATE()-1)
Now when I query the table as
select A from #temp where A>=GETDATE() and A<=GETDATE()
I get no records
But GETDATE() record value should satisfy my where condition, shouldn't it at least pass me one record?
Please guide me if I am missing some point here.
You need to do conversion, so it seems :
where a >= convert(date, dateadd(day, -1, getdate())) and
a <= convert(date, getdate());
Your where clause comparing date as :
where a >= '2020-04-21 16:01:27.277' and a <= '2020-04-21 16:01:27.277'
So, you need to convert date because getdate()will also return time portions.
Since your where clause looks for single day so you can do :
where a = convert(date, getdate())
Yogesh is right.
GETDATE() gives the present DATEIME value. When you insert it into a DATE column, SQL Server coerces -- silently typecasts -- the DATETIME to a DATE before inserting it.
But when you use it in a WHERE clause, SQL Server coerces the DATE from your column into a DATETIME value by turning 2020-04-20 into 2020-04-20 00:00:00. That can't be the same as GETDATE() except during the first few milliseconds of each day. (Meaning you or your test krewe are extremely unlikely to catch it equal.)

SQL Server : today is not equal to today

Maybe I'm making an obvious mistake but can anyone explain what's going on here? I was running a query where the table's field is datetime and the query I was running was something like
SELECT *
FROM Table
WHERE DateTimeColumn <= '20170714'
and I noticed the output excluded the records where DateTimeColumn is '20170714' they finished at '20170713'
Below I was expecting all 3 IIF to fall into true.
DECLARE #d1 DATE = '20170714'
SELECT IIF(GETDATE() <= #d1, 'GETDATE() Less than or equal to #d1', 'GETDATE() **NOT** Less than or equal to #d1')
DECLARE #d2 DATE = '20170714 11:59:59'
SELECT IIF(GETDATE() <= #d2, 'GETDATE() Less than or equal to #d2', 'GETDATE() **NOT** Less than or equal to #d2')
DECLARE #tomorrow DATE = '20170715'
SELECT IIF(GETDATE() <= #tomorrow, 'GETDATE() Less than or equal to #tomorrow', 'GETDATE() **NOT** Less than or equal to #tomorrow')
Just use less than 2017-07-15 (tomorrow)
SELECT *
FROM Table
WHERE DateTimeColumn < '20170715'
If wanting to use getdate, try this:
SELECT *
FROM Table
WHERE DateTimeColumn < dateadd(day,1,cast(getdate() as date))
Use sargable predicates. DO NOT convert your data to suit a filtering predicate, this affects index access and/or requires unnecessary calculations. Here is a former answer on the similar question.
Also note that 23:59:59 is NOT the end of a day, it is one full second short of a full day: datetime is accurate to approx 3 milliseconds and datetime2 is even more sensitive.
Can you just change the query like this :
SELECT *
FROM Table
WHERE CONVERT(date, DateTimeColumn) <= '20170714'
It would return all the records less that 14 and record with date 14.

Explicit conversion from data type date to bigint is not allowed

This used to work with a column type of DATEIME but now it won't with DATE.
CONVERT(BIGINT,ev.StartDate) * -1
Is there anyway to get a BIGINT value from a DATE column?
You can cast the startdate as datetime for conversion.
CONVERT(BIGINT,CAST(ev.StartDate as DATETIME)) * -1
Yet another option. This will even flip the sign for you
Example
Declare #YourTable table (StartDate date)
Insert Into #YourTable values ('2017-05-30')
Select DateDiff(DAY,StartDate,-1)
From #YourTable
Returns
-42884
First, dates in SQL Server are counted by days from the year 1900. A big int starts to be useful at about 2.1 billion. That corresponds to a year in the range of 5.8 million. Do you really have dates that large?
Of course, casting to an int is not permitted. You can cast datetime values . . . but are there other ways?
One simple way is:
select 1 + datediff(day, 0, datecol)
The "+ 1" is needed so the value matches the actual conversion. (You can use "-1" instead of "0" instead.)
Or, perhaps you want Unix time in seconds or milliseconds. For that:
select datediff_big(ms, '1970-01-01', datecol)
You might require to convert to varchar and then bigint
select Convert(bigint,convert(varchar(10),ev.StartDate,112))*(-1)

Comparing a Date column with a datetime value

I have found the below query in one our stored procedures
SELECT *FROM TABLE1
WHERE (CONVERT(DATE,DateTo) BETWEEN #wkstdate AND #wkenddate))
Since Usage of Functions in where clause may hinder the performance I have changed it as below,
SELECT *FROM TABLE1
WHERE DateTo BETWEEN #wkstdate AND #wkenddate
The result is same after changing the codes. But i am not sure whether both will give same result in all the time. Any Scenarios where the above codes bring different results?
(P.S: #wkstdate and #wkenddate are DATE values & DateTo is a
DATETIME value)
Appreciate Your Suggestions
This will not yield the same result.
Let's say your DateTo, which is a DATETIME value, has a time component:
'2015-09-21 01:00:00'
Your #wkenddate is '2015-09-21'. The WHERE DateTo BETWEEN #wkstdate AND #wkenddate will not retrieve the above row since '2015-09-21 01:00:00' > #wkenddate.
For more example:
CREATE TABLE tbl(DateTo DATETIME)
INSERT INTO tbl
SELECT CAST('2015-09-21 00:00:00.000' AS DATETIME) UNION ALL
SELECT CAST('2015-09-21 16:10:49.047' AS DATETIME) UNION ALL
SELECT CAST('2015-09-22 16:10:49.047' AS DATETIME) UNION ALL
SELECT CAST('2015-09-20 16:10:49.047' AS DATETIME)
DECLARE #wkstdate DATE = '20150921',
#wkenddate DATE = '20150921'
SELECT *
FROM tbl
WHERE DateTo BETWEEN #wkstdate AND #wkenddate
SELECT * FROM tbl
WHERE (CONVERT(DATE,DateTo) BETWEEN #wkstdate AND #wkenddate)
DROP TABLE tbl
Now, using function in WHERE clause does make your query un-SARGable but there are exceptions. One of them is CASTing to DATE.
Another alternative if you do not want to CAST to DATE is to not use the BETWEEN operator. Instead use >= and <:
WHERE
DateTo >= #wkstdate
AND DateTo < DATEADD(DAY, 1, #wkenddate)
The BETWEEN operator will not cope properly with Times on your date data. So if you have two dates 1/1/2000 and 2/1/2000, and then ask for BETWEEN to work on a datetime like 2/1/2000 14:00, then this datetime does NOT fall between them. Stripping the Time portion off the datetime is advisable, using your CONVERT function as in your example is probably the best way. There are other ways to strip off the Time portion, but CONVERT is probably the most efficient. (My example using dd/mm/yyyy format)
What is the least efficient thing I noticed about your stored procedure is the use of SELECT * FROM. Try to use explicit field selections - to minimize the load on SQL if you want more efficient Stored procedures.
Yes both will give different results.
Lets say wkStDate = 9/1/2015 and wkEndDta = 9/30/2015
DateTo = 9/1/2015 18:00 HRS (will be included in both Cases)
DateTo = 9/21/2105 18:00 HRS (will be included in both cases)
DateTo = 9/30/2105 18:00 HRS (will be included in original query but
excluded in second query without the convert)
In other words anyvalue where date part is same as end date and has time greater than midnight will be excluded from your query without the convert function i.e. WkEndDate + 00:00:01 To WkEndDate + 23:59:59 will be excluded. All other dates will show the same result.
If your DateTo is date time and will never has a time other than midnight then both the queries will give same result.

SQL Server: how to avoid between operator with datetime column?

I have a datetime column and I would like to select rows based on this columns. My query is
SELECT *
FROM dbo.mytable
WHERE daycol BETWEEN '20110404' AND '20110406';
This will also brings me a row
2011-04-06 00:00:00.000
Which is not correct. How to avoid this? Should between operator be avoided with datetime columns?
You may use this if you don't want the row of 2011-04-06 00:00:00.000 date
SELECT *
FROM dbo.mytable
WHERE daycol >= '20110404' AND daycol < '20110406';
If you write out this line:
WHERE daycol BETWEEN '20110404' AND '20110406';
You get:
WHERE '2011-04-04 00:00:00.000' <= daycol
and daycol <= '2011-04-06 00:00:00.000'
Between is inclusive. If you'd like to exclude the boundaries, change <= to < in the expanded version.
Please note this is the expected behavior of BETWEEN
(See: http://msdn.microsoft.com/en-us/library/ms187922.aspx)
From the article:
BETWEEN returns TRUE if the value of test_expression is greater than or equal to the value of begin_expression and less than or equal to the value of end_expression.
You can modify your end_expression to get different results than you are receiving.
Or use >=, < operators. But be careful if the column you are comparing against is DATETIME T-SQL will convert '20110406' to '2011-04-06 00:00:00.000' so take this into consideration when determining which operators to use.