SQL Server : today is not equal to today - sql

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.

Related

Why is '=' operator not working on comparing Datetime?

I am trying to fetch records only of the current day. I have used the = operator to compare the date but it is not working, if I subtract 1 from the current date and then use >= operator then it works.
I am putting both queries here.
This code works:
1 = (
CASE WHEN ISNULL(DO.CREATEDON, '') >= GETDATE()-1
THEN 1
END
This code doesn't work:
1 = (
CASE WHEN ISNULL(DO.CREATEDON, '') = GETDATE()
THEN 1
END
Why is the latter code not working?
GETDATE() returns a datetime, so (for me), right now, using GETDATE() returns something like 2021-01-19T09:43:27.123. It's therefore very unlikely the value in your column CREATEDON is going to be the same exact time that GETDATE() returns, accurate to the nearest 1/300 of a second.
If your column CREATEDON is a date and time value, use inclusive date ranges:
WHERE DO.CREATEDON >= CONVERT(date,GETDATE())
AND DO.CREATEDON < CONVERT(date,DATEADD(DAY, 1, GETDATE())
If CREATEDON is a date, then just CONVERT the value of GETDATE() to a date:
WHERE DO.CREATEDON = CONVERT(date,GETDATE())
Note that there's no need for the ISNULL, as is the value of NULL it is by definition not the value of GETDATE(). Also, converting'' to a date and time value is a little odd; but it would convert to 1900-01-01, which again, is not today. Adding an ISNULL on DO.CREATEDON in the WHERE will only harm the performance; don't do it.

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 statement between date

This is driving me crazy and not sure what I'm missing here..
so here is my data column looks like:
StartDateTime:
---------------
2012-01-17 11:13:46.530
2012-01-17 11:17:22.530
2012-02-17 11:31:22.223
here is my query trying to get:
select * from tablName
where convert(varchar(10), startDateTime, 101) between '2012-01-17' and '2012-01-17'
based on the above I should be getting TWO rows? but it does not, it return zero rows. what will be the correct way of doing?
PS:
I've looked at the MSDN site too:
Your query would only match dates that are between 2012-01-17 00:00:00 and 2012-01-17 00:00:00. So, the only matches would be when the date is exactly 2012-01-17 00:00:00.
Instead, I would do this:
declare #dateInput as DateTime
set #dateInput = '2012-01-17'
select *
from tablName
where startDateTime >= #dateInput
and startDateTime < dateadd(day, 1, #dateInput)
Note: SQL Server 2008+ has a new data type Date with no time component that can make these types of queries more readable.
There is now more information so I'll add a more appropriate answer.
The requirements are now a stored procedure passed a Date type parameter, not DateTime, and the desire is to return rows from a table based on criterion against a DateTime field named StartDateTime...
create procedure dbo.spGetEntriesForOneDay
#DesiredDate DateTime
as
SET NOCOUNT ON;
SET #DesiredDate = DATEADD(day, DATEDIFF(day, 0, #DesiredDate), 0)
SELECT Field1, Field2 -- see note 1
FROM dbo.TableName
WHERE StartDateTime >= #DesiredDate -- see note 2
AND StartDateTime < DATEADD(day, 1, #DesiredDate) -- see note 3
NOTE 1: Don't use * in production code, especially in a stored procedure. Besides being wasteful by returning columns you probably don't need and precluding the optimization of a covering index on a subset of the columns required you would need to recompile this stored procedure whenever the underlying table is altered in order to avoid unpredictable results.
NOTE 2: Avoid wrapping fields in functions. A field not wrapped in a function can potentially be matched by the optimizer to an index while a field wrapped in a function never will.
NOTE 3: #Martin Smith and #RedFilter are correct in that .997 precision assumes DateTime datatype forever; this approach is more future proof because is makes no assumptions of data type precision.
You're using a datetime field (I'm guessing).
Don't forget the time:
select * from tablName
where startDateTime between '2012-01-17' and '2012-01-17 23:59:59.997'
You can use the DateDiff function in the where clause. It would look like this:
select col1, col2 from tablName where DateDiff(day, startDateTime, #DesiredDate) = 0

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

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)

To get date from datetime in sql

I have datecreated field in a table. It contains value as "2009-12-30 11:47:20:297"
I have a query like this:
select *
from table
where DateCreated = getdate()
Although one row exists with today's date, I am not getting that row while executing above query. Can anybody help?
The reason why your query doesn't return the row you expect, is because GETDATE() returns the date and time portion at the moment the query was executed. The value in your DateCreated column will not match the time portion, so no rows are returned.
There are various ways to construct a query so that it evaluates the date based on only the date component. Here's one example:
WHERE YEAR(datecreated) = YEAR(GETDATE())
AND MONTH(datecreated) = MONTH(GETDATE())
AND DAY(datecreated) = DAY(GETDATE())
The unfortunate reality is that any query using a function on the column means that if an index exists on the column, it can't be used.
You can use something like this with Sql Server
CREATE FUNCTION [dbo].[udf_DateOnly](#DateTime DATETIME)
RETURNS DATETIME
AS
BEGIN
RETURN DATEADD(dd,0, DATEDIFF(dd,0,#DateTime))
END
This line
DATEADD(dd,0, DATEDIFF(dd,0,#DateTime))
will strip out the Date portion.
The datetime field includes both the date and the time, accurate to the millisecond. Your query will only work if it is the exact millisecond stored in the database.
To check if it is today, but ignore the time of day, you can check for a range like this:
select * from table where
DateCreated >= '2009-12-30' and
DateCreated < '2009-12-31'
You can use that in conjunction with a function that converts the current date, as astander or Khilon has posted. Here is a full example using astander's answer. Also, as Craig Young points out, this will work with indexes.
select * from table where
DateCreated >= DATEDIFF(dd,0,GETDATE()) and
DateCreated < DATEDIFF(dd,0,GETDATE())
The simplest solution might be :
SELECT CAST(GETDATE() as DATE)
You can convert datetime to a string with only the date by using
CONVERT(varchar(8), GETDATE(), 112)
If needed, you can then change it back to datetime and as a result you'll get a datetime with the hours, minutes, seconds and milliseconds set to zero.