SQL query not returning expected date time range
up vote
0
down vote
favorite
I am working on a query that I hope to be able to use to query against a database for a specific range of time on a specific date. If I query for a full day of data I get the correct data returned. One row per hour of data available (0 - 23).
WHERE Documents.CreationTime BETWEEN '2014-10-01 00:00:00.000' AND '2014-10-01 23:59:59.999'
If I attempt to query for a portion of the day, the results are unusual.
WHERE Documents.CreationTime BETWEEN '2014-10-01 00:00:00.000' AND '2014-10-01 06:00:00.000'
Part day query returns: (Note the hours jump from 0 to 19)
Hours Faxes Good Page Count
0 3 4
19 15 58
20 4 9
21 8 42
22 2 4
23 4 12
Here is my reduced query I created to try and resolve the issue.
SELECT DATEPART(hour, DATEADD(HH, - DATEDIFF(Hour, GETDATE(), GETUTCDATE()), Documents.CreationTime)) AS Hours
,COUNT(*) AS Faxes
,SUM(goodpagecount) AS [Good Page Count]
FROM Documents
JOIN Users
ON Documents.OwnerID = Users.handle
JOIN Groups
ON Users.GroupID = Groups.handle
JOIN History
ON History.OWNER = Documents.handle
JOIN HistoryTRX
ON History.handle = HistoryTRX.handle
WHERE Documents.CreationTime BETWEEN '2014-10-01 00:00:00.000'
AND '2014-10-01 06:00:00.000'
GROUP BY DATEPART(hour, DATEADD(HH, - DATEDIFF(Hour, GETDATE(), GETUTCDATE()), Documents.CreationTime))
ORDER BY DATEPART(hour, DATEADD(HH, - DATEDIFF(Hour, GETDATE(), GETUTCDATE()), Documents.CreationTime))
Any suggestions as to what I am missing or improvements?
EDIT- More details
The "Documents.CreationTime" is in UTC. I am looking to have the "Hours" column correspond to local time. In this case UTC -5 as of this entry.
sql sql-server
|
show 1 more comment
up vote
0
down vote
favorite
I am working on a query that I hope to be able to use to query against a database for a specific range of time on a specific date. If I query for a full day of data I get the correct data returned. One row per hour of data available (0 - 23).
WHERE Documents.CreationTime BETWEEN '2014-10-01 00:00:00.000' AND '2014-10-01 23:59:59.999'
If I attempt to query for a portion of the day, the results are unusual.
WHERE Documents.CreationTime BETWEEN '2014-10-01 00:00:00.000' AND '2014-10-01 06:00:00.000'
Part day query returns: (Note the hours jump from 0 to 19)
Hours Faxes Good Page Count
0 3 4
19 15 58
20 4 9
21 8 42
22 2 4
23 4 12
Here is my reduced query I created to try and resolve the issue.
SELECT DATEPART(hour, DATEADD(HH, - DATEDIFF(Hour, GETDATE(), GETUTCDATE()), Documents.CreationTime)) AS Hours
,COUNT(*) AS Faxes
,SUM(goodpagecount) AS [Good Page Count]
FROM Documents
JOIN Users
ON Documents.OwnerID = Users.handle
JOIN Groups
ON Users.GroupID = Groups.handle
JOIN History
ON History.OWNER = Documents.handle
JOIN HistoryTRX
ON History.handle = HistoryTRX.handle
WHERE Documents.CreationTime BETWEEN '2014-10-01 00:00:00.000'
AND '2014-10-01 06:00:00.000'
GROUP BY DATEPART(hour, DATEADD(HH, - DATEDIFF(Hour, GETDATE(), GETUTCDATE()), Documents.CreationTime))
ORDER BY DATEPART(hour, DATEADD(HH, - DATEDIFF(Hour, GETDATE(), GETUTCDATE()), Documents.CreationTime))
Any suggestions as to what I am missing or improvements?
EDIT- More details
The "Documents.CreationTime" is in UTC. I am looking to have the "Hours" column correspond to local time. In this case UTC -5 as of this entry.
sql sql-server
4
What's the result ofDATEDIFF(Hour,GETDATE(),GETUTCDATE())
for you?. Also, don't useBETWEEN
in this case for filtering aDATETIME
, instead ofBETWEEN '2014-10-01 00:00:00.000' AND '2014-10-01 23:59:59.999'
usecol >= '20141001' AND col < '20141002'
– Lamak
Oct 13 '14 at 16:18
To be honest, I'm not sure what your question is. What is the issue and what improvements do you want?
– Andriy M
Oct 13 '14 at 16:51
1
@AndriyM The problem is that the filter suggests times from0
till5
, but the results are times from19
till0
. This is because of the "correction" that's being made on theSELECT
(-DATEDIFF(Hour,GETDATE(),GETUTCDATE())
). That correction is substracting 5 hours to the results, hence giving different results than expected
– Lamak
Oct 13 '14 at 16:58
@gritts All right, some more details are needed. IsDocuments.CreationTime
UTC or local time? When you specify the boundaries, are they supposed to be UTC or local time? My guess is you would like the input and the output to be consistent (both to be either UTC or local time) but I'm not sure, and I'm also not sure if that is the actual problem you are trying to solve with this question.
– Andriy M
Oct 13 '14 at 17:20
@AndriyM, the Documents.CreationTime is recorded in UTC. I am attempting to output in local time to save confusion in the final report to be created.
– gritts
Oct 13 '14 at 20:29
|
show 1 more comment
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am working on a query that I hope to be able to use to query against a database for a specific range of time on a specific date. If I query for a full day of data I get the correct data returned. One row per hour of data available (0 - 23).
WHERE Documents.CreationTime BETWEEN '2014-10-01 00:00:00.000' AND '2014-10-01 23:59:59.999'
If I attempt to query for a portion of the day, the results are unusual.
WHERE Documents.CreationTime BETWEEN '2014-10-01 00:00:00.000' AND '2014-10-01 06:00:00.000'
Part day query returns: (Note the hours jump from 0 to 19)
Hours Faxes Good Page Count
0 3 4
19 15 58
20 4 9
21 8 42
22 2 4
23 4 12
Here is my reduced query I created to try and resolve the issue.
SELECT DATEPART(hour, DATEADD(HH, - DATEDIFF(Hour, GETDATE(), GETUTCDATE()), Documents.CreationTime)) AS Hours
,COUNT(*) AS Faxes
,SUM(goodpagecount) AS [Good Page Count]
FROM Documents
JOIN Users
ON Documents.OwnerID = Users.handle
JOIN Groups
ON Users.GroupID = Groups.handle
JOIN History
ON History.OWNER = Documents.handle
JOIN HistoryTRX
ON History.handle = HistoryTRX.handle
WHERE Documents.CreationTime BETWEEN '2014-10-01 00:00:00.000'
AND '2014-10-01 06:00:00.000'
GROUP BY DATEPART(hour, DATEADD(HH, - DATEDIFF(Hour, GETDATE(), GETUTCDATE()), Documents.CreationTime))
ORDER BY DATEPART(hour, DATEADD(HH, - DATEDIFF(Hour, GETDATE(), GETUTCDATE()), Documents.CreationTime))
Any suggestions as to what I am missing or improvements?
EDIT- More details
The "Documents.CreationTime" is in UTC. I am looking to have the "Hours" column correspond to local time. In this case UTC -5 as of this entry.
sql sql-server
I am working on a query that I hope to be able to use to query against a database for a specific range of time on a specific date. If I query for a full day of data I get the correct data returned. One row per hour of data available (0 - 23).
WHERE Documents.CreationTime BETWEEN '2014-10-01 00:00:00.000' AND '2014-10-01 23:59:59.999'
If I attempt to query for a portion of the day, the results are unusual.
WHERE Documents.CreationTime BETWEEN '2014-10-01 00:00:00.000' AND '2014-10-01 06:00:00.000'
Part day query returns: (Note the hours jump from 0 to 19)
Hours Faxes Good Page Count
0 3 4
19 15 58
20 4 9
21 8 42
22 2 4
23 4 12
Here is my reduced query I created to try and resolve the issue.
SELECT DATEPART(hour, DATEADD(HH, - DATEDIFF(Hour, GETDATE(), GETUTCDATE()), Documents.CreationTime)) AS Hours
,COUNT(*) AS Faxes
,SUM(goodpagecount) AS [Good Page Count]
FROM Documents
JOIN Users
ON Documents.OwnerID = Users.handle
JOIN Groups
ON Users.GroupID = Groups.handle
JOIN History
ON History.OWNER = Documents.handle
JOIN HistoryTRX
ON History.handle = HistoryTRX.handle
WHERE Documents.CreationTime BETWEEN '2014-10-01 00:00:00.000'
AND '2014-10-01 06:00:00.000'
GROUP BY DATEPART(hour, DATEADD(HH, - DATEDIFF(Hour, GETDATE(), GETUTCDATE()), Documents.CreationTime))
ORDER BY DATEPART(hour, DATEADD(HH, - DATEDIFF(Hour, GETDATE(), GETUTCDATE()), Documents.CreationTime))
Any suggestions as to what I am missing or improvements?
EDIT- More details
The "Documents.CreationTime" is in UTC. I am looking to have the "Hours" column correspond to local time. In this case UTC -5 as of this entry.
sql sql-server
sql sql-server
edited Oct 13 '14 at 18:47
asked Oct 13 '14 at 16:15
gritts
651211
651211
4
What's the result ofDATEDIFF(Hour,GETDATE(),GETUTCDATE())
for you?. Also, don't useBETWEEN
in this case for filtering aDATETIME
, instead ofBETWEEN '2014-10-01 00:00:00.000' AND '2014-10-01 23:59:59.999'
usecol >= '20141001' AND col < '20141002'
– Lamak
Oct 13 '14 at 16:18
To be honest, I'm not sure what your question is. What is the issue and what improvements do you want?
– Andriy M
Oct 13 '14 at 16:51
1
@AndriyM The problem is that the filter suggests times from0
till5
, but the results are times from19
till0
. This is because of the "correction" that's being made on theSELECT
(-DATEDIFF(Hour,GETDATE(),GETUTCDATE())
). That correction is substracting 5 hours to the results, hence giving different results than expected
– Lamak
Oct 13 '14 at 16:58
@gritts All right, some more details are needed. IsDocuments.CreationTime
UTC or local time? When you specify the boundaries, are they supposed to be UTC or local time? My guess is you would like the input and the output to be consistent (both to be either UTC or local time) but I'm not sure, and I'm also not sure if that is the actual problem you are trying to solve with this question.
– Andriy M
Oct 13 '14 at 17:20
@AndriyM, the Documents.CreationTime is recorded in UTC. I am attempting to output in local time to save confusion in the final report to be created.
– gritts
Oct 13 '14 at 20:29
|
show 1 more comment
4
What's the result ofDATEDIFF(Hour,GETDATE(),GETUTCDATE())
for you?. Also, don't useBETWEEN
in this case for filtering aDATETIME
, instead ofBETWEEN '2014-10-01 00:00:00.000' AND '2014-10-01 23:59:59.999'
usecol >= '20141001' AND col < '20141002'
– Lamak
Oct 13 '14 at 16:18
To be honest, I'm not sure what your question is. What is the issue and what improvements do you want?
– Andriy M
Oct 13 '14 at 16:51
1
@AndriyM The problem is that the filter suggests times from0
till5
, but the results are times from19
till0
. This is because of the "correction" that's being made on theSELECT
(-DATEDIFF(Hour,GETDATE(),GETUTCDATE())
). That correction is substracting 5 hours to the results, hence giving different results than expected
– Lamak
Oct 13 '14 at 16:58
@gritts All right, some more details are needed. IsDocuments.CreationTime
UTC or local time? When you specify the boundaries, are they supposed to be UTC or local time? My guess is you would like the input and the output to be consistent (both to be either UTC or local time) but I'm not sure, and I'm also not sure if that is the actual problem you are trying to solve with this question.
– Andriy M
Oct 13 '14 at 17:20
@AndriyM, the Documents.CreationTime is recorded in UTC. I am attempting to output in local time to save confusion in the final report to be created.
– gritts
Oct 13 '14 at 20:29
4
4
What's the result of
DATEDIFF(Hour,GETDATE(),GETUTCDATE())
for you?. Also, don't use BETWEEN
in this case for filtering a DATETIME
, instead of BETWEEN '2014-10-01 00:00:00.000' AND '2014-10-01 23:59:59.999'
use col >= '20141001' AND col < '20141002'
– Lamak
Oct 13 '14 at 16:18
What's the result of
DATEDIFF(Hour,GETDATE(),GETUTCDATE())
for you?. Also, don't use BETWEEN
in this case for filtering a DATETIME
, instead of BETWEEN '2014-10-01 00:00:00.000' AND '2014-10-01 23:59:59.999'
use col >= '20141001' AND col < '20141002'
– Lamak
Oct 13 '14 at 16:18
To be honest, I'm not sure what your question is. What is the issue and what improvements do you want?
– Andriy M
Oct 13 '14 at 16:51
To be honest, I'm not sure what your question is. What is the issue and what improvements do you want?
– Andriy M
Oct 13 '14 at 16:51
1
1
@AndriyM The problem is that the filter suggests times from
0
till 5
, but the results are times from 19
till 0
. This is because of the "correction" that's being made on the SELECT
(-DATEDIFF(Hour,GETDATE(),GETUTCDATE())
). That correction is substracting 5 hours to the results, hence giving different results than expected– Lamak
Oct 13 '14 at 16:58
@AndriyM The problem is that the filter suggests times from
0
till 5
, but the results are times from 19
till 0
. This is because of the "correction" that's being made on the SELECT
(-DATEDIFF(Hour,GETDATE(),GETUTCDATE())
). That correction is substracting 5 hours to the results, hence giving different results than expected– Lamak
Oct 13 '14 at 16:58
@gritts All right, some more details are needed. Is
Documents.CreationTime
UTC or local time? When you specify the boundaries, are they supposed to be UTC or local time? My guess is you would like the input and the output to be consistent (both to be either UTC or local time) but I'm not sure, and I'm also not sure if that is the actual problem you are trying to solve with this question.– Andriy M
Oct 13 '14 at 17:20
@gritts All right, some more details are needed. Is
Documents.CreationTime
UTC or local time? When you specify the boundaries, are they supposed to be UTC or local time? My guess is you would like the input and the output to be consistent (both to be either UTC or local time) but I'm not sure, and I'm also not sure if that is the actual problem you are trying to solve with this question.– Andriy M
Oct 13 '14 at 17:20
@AndriyM, the Documents.CreationTime is recorded in UTC. I am attempting to output in local time to save confusion in the final report to be created.
– gritts
Oct 13 '14 at 20:29
@AndriyM, the Documents.CreationTime is recorded in UTC. I am attempting to output in local time to save confusion in the final report to be created.
– gritts
Oct 13 '14 at 20:29
|
show 1 more comment
2 Answers
2
active
oldest
votes
up vote
1
down vote
How about using the DATEADD function in your where clause:
WHERE Documents.CreationTime >= '20141001' AND Documents.CreationTime <= DATEADD(HOUR,6,'20141001')
Interesting Blog on the comment made by Lamak written by Aaron Bertrand :
- What do BETWEEN and the devil have in common?
I adjusted what you suggested to read "WHERE Documents.CreationTime >= '2014-10-01 00:00:00.000' and Documents.CreationTime <= DATEADD(HH,6,'2014-10-01 00:00:00.000')" but the results were the same.
– gritts
Oct 13 '14 at 16:29
@gritts Well, let us help you, can you answer what's the result that you get when you doDATEDIFF(Hour,GETDATE(),GETUTCDATE())
?
– Lamak
Oct 13 '14 at 16:31
Sure, did not mean to sound gruff... Using "SELECT DATEDIFF(Hour,GETDATE(),GETUTCDATE())" I get the result of "5"
– gritts
Oct 13 '14 at 16:36
@gritts Well, then, that explain your results, doesn't it?. So, you have results from the hours0
till5
(this because of your filter). Then, you are substracting 5 hours to those results (this from yourDATEADD()
. So, now your results are for hours since19
from the day before till0
– Lamak
Oct 13 '14 at 16:39
1
@gritts If you want your results to be consistent, then you should apply that same correction to your filter (though if the results are actually what you are looking for, I'm not sure)
– Lamak
Oct 13 '14 at 16:51
|
show 1 more comment
up vote
0
down vote
accepted
Based on suggestions provided in response to my question, I came up with the following new query:
SELECT DATEPART(hour, DATEADD(HH,-DATEDIFF(Hour,GETDATE(),GETUTCDATE()),Documents.CreationTime)) AS Hours ,COUNT(*) AS Faxes,SUM(goodpagecount) AS [Good Page Count]
FROM Documents
JOIN Users ON Documents.OwnerID=Users.handle
JOIN Groups ON Users.GroupID=Groups.handle
JOIN History ON History.Owner=Documents.handle
JOIN HistoryTRX ON History.handle=HistoryTRX.handle
WHERE DATEADD(HH,-DATEDIFF(Hour,GETDATE(),GETUTCDATE()),Documents.CreationTime) >= '2014-10-01 00:00:00.000' and DATEADD(HH,-DATEDIFF(Hour,GETDATE(),GETUTCDATE()),Documents.CreationTime) <= '2014-10-03 08:00:00.000'
GROUP BY DATEPART(hour, DATEADD(HH,-DATEDIFF(Hour,GETDATE(),GETUTCDATE()),Documents.CreationTime))
ORDER BY DATEPART(hour, DATEADD(HH,-DATEDIFF(Hour,GETDATE(),GETUTCDATE()),Documents.CreationTime))
My changes are to the "WHERE" statement by adding my UTC compensation. The "WHERE" now matches the "SELECT".
Before:
WHERE Documents.CreationTime >= '2014-10-01 00:00:00.000' and Documents.CreationTime <= '2014-10-03 08:00:00.000'
After:
WHERE DATEADD(HH,-DATEDIFF(Hour,GETDATE(),GETUTCDATE()),Documents.CreationTime) >= '2014-10-01 00:00:00.000' and DATEADD(HH,-DATEDIFF(Hour,GETDATE(),GETUTCDATE()),Documents.CreationTime) <= '2014-10-03 08:00:00.000'
Also removed the BETWEEN keyword as it may not be as precise for results as I would like.
The results now look like:
Hours Faxes Good Page Count
0 3 4
1 5 9
3 9 50
4 8 16
5 14 40
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
How about using the DATEADD function in your where clause:
WHERE Documents.CreationTime >= '20141001' AND Documents.CreationTime <= DATEADD(HOUR,6,'20141001')
Interesting Blog on the comment made by Lamak written by Aaron Bertrand :
- What do BETWEEN and the devil have in common?
I adjusted what you suggested to read "WHERE Documents.CreationTime >= '2014-10-01 00:00:00.000' and Documents.CreationTime <= DATEADD(HH,6,'2014-10-01 00:00:00.000')" but the results were the same.
– gritts
Oct 13 '14 at 16:29
@gritts Well, let us help you, can you answer what's the result that you get when you doDATEDIFF(Hour,GETDATE(),GETUTCDATE())
?
– Lamak
Oct 13 '14 at 16:31
Sure, did not mean to sound gruff... Using "SELECT DATEDIFF(Hour,GETDATE(),GETUTCDATE())" I get the result of "5"
– gritts
Oct 13 '14 at 16:36
@gritts Well, then, that explain your results, doesn't it?. So, you have results from the hours0
till5
(this because of your filter). Then, you are substracting 5 hours to those results (this from yourDATEADD()
. So, now your results are for hours since19
from the day before till0
– Lamak
Oct 13 '14 at 16:39
1
@gritts If you want your results to be consistent, then you should apply that same correction to your filter (though if the results are actually what you are looking for, I'm not sure)
– Lamak
Oct 13 '14 at 16:51
|
show 1 more comment
up vote
1
down vote
How about using the DATEADD function in your where clause:
WHERE Documents.CreationTime >= '20141001' AND Documents.CreationTime <= DATEADD(HOUR,6,'20141001')
Interesting Blog on the comment made by Lamak written by Aaron Bertrand :
- What do BETWEEN and the devil have in common?
I adjusted what you suggested to read "WHERE Documents.CreationTime >= '2014-10-01 00:00:00.000' and Documents.CreationTime <= DATEADD(HH,6,'2014-10-01 00:00:00.000')" but the results were the same.
– gritts
Oct 13 '14 at 16:29
@gritts Well, let us help you, can you answer what's the result that you get when you doDATEDIFF(Hour,GETDATE(),GETUTCDATE())
?
– Lamak
Oct 13 '14 at 16:31
Sure, did not mean to sound gruff... Using "SELECT DATEDIFF(Hour,GETDATE(),GETUTCDATE())" I get the result of "5"
– gritts
Oct 13 '14 at 16:36
@gritts Well, then, that explain your results, doesn't it?. So, you have results from the hours0
till5
(this because of your filter). Then, you are substracting 5 hours to those results (this from yourDATEADD()
. So, now your results are for hours since19
from the day before till0
– Lamak
Oct 13 '14 at 16:39
1
@gritts If you want your results to be consistent, then you should apply that same correction to your filter (though if the results are actually what you are looking for, I'm not sure)
– Lamak
Oct 13 '14 at 16:51
|
show 1 more comment
up vote
1
down vote
up vote
1
down vote
How about using the DATEADD function in your where clause:
WHERE Documents.CreationTime >= '20141001' AND Documents.CreationTime <= DATEADD(HOUR,6,'20141001')
Interesting Blog on the comment made by Lamak written by Aaron Bertrand :
- What do BETWEEN and the devil have in common?
How about using the DATEADD function in your where clause:
WHERE Documents.CreationTime >= '20141001' AND Documents.CreationTime <= DATEADD(HOUR,6,'20141001')
Interesting Blog on the comment made by Lamak written by Aaron Bertrand :
- What do BETWEEN and the devil have in common?
edited Nov 9 at 22:30
Aaron Bertrand
206k27360401
206k27360401
answered Oct 13 '14 at 16:25
Mattgb
31328
31328
I adjusted what you suggested to read "WHERE Documents.CreationTime >= '2014-10-01 00:00:00.000' and Documents.CreationTime <= DATEADD(HH,6,'2014-10-01 00:00:00.000')" but the results were the same.
– gritts
Oct 13 '14 at 16:29
@gritts Well, let us help you, can you answer what's the result that you get when you doDATEDIFF(Hour,GETDATE(),GETUTCDATE())
?
– Lamak
Oct 13 '14 at 16:31
Sure, did not mean to sound gruff... Using "SELECT DATEDIFF(Hour,GETDATE(),GETUTCDATE())" I get the result of "5"
– gritts
Oct 13 '14 at 16:36
@gritts Well, then, that explain your results, doesn't it?. So, you have results from the hours0
till5
(this because of your filter). Then, you are substracting 5 hours to those results (this from yourDATEADD()
. So, now your results are for hours since19
from the day before till0
– Lamak
Oct 13 '14 at 16:39
1
@gritts If you want your results to be consistent, then you should apply that same correction to your filter (though if the results are actually what you are looking for, I'm not sure)
– Lamak
Oct 13 '14 at 16:51
|
show 1 more comment
I adjusted what you suggested to read "WHERE Documents.CreationTime >= '2014-10-01 00:00:00.000' and Documents.CreationTime <= DATEADD(HH,6,'2014-10-01 00:00:00.000')" but the results were the same.
– gritts
Oct 13 '14 at 16:29
@gritts Well, let us help you, can you answer what's the result that you get when you doDATEDIFF(Hour,GETDATE(),GETUTCDATE())
?
– Lamak
Oct 13 '14 at 16:31
Sure, did not mean to sound gruff... Using "SELECT DATEDIFF(Hour,GETDATE(),GETUTCDATE())" I get the result of "5"
– gritts
Oct 13 '14 at 16:36
@gritts Well, then, that explain your results, doesn't it?. So, you have results from the hours0
till5
(this because of your filter). Then, you are substracting 5 hours to those results (this from yourDATEADD()
. So, now your results are for hours since19
from the day before till0
– Lamak
Oct 13 '14 at 16:39
1
@gritts If you want your results to be consistent, then you should apply that same correction to your filter (though if the results are actually what you are looking for, I'm not sure)
– Lamak
Oct 13 '14 at 16:51
I adjusted what you suggested to read "WHERE Documents.CreationTime >= '2014-10-01 00:00:00.000' and Documents.CreationTime <= DATEADD(HH,6,'2014-10-01 00:00:00.000')" but the results were the same.
– gritts
Oct 13 '14 at 16:29
I adjusted what you suggested to read "WHERE Documents.CreationTime >= '2014-10-01 00:00:00.000' and Documents.CreationTime <= DATEADD(HH,6,'2014-10-01 00:00:00.000')" but the results were the same.
– gritts
Oct 13 '14 at 16:29
@gritts Well, let us help you, can you answer what's the result that you get when you do
DATEDIFF(Hour,GETDATE(),GETUTCDATE())
?– Lamak
Oct 13 '14 at 16:31
@gritts Well, let us help you, can you answer what's the result that you get when you do
DATEDIFF(Hour,GETDATE(),GETUTCDATE())
?– Lamak
Oct 13 '14 at 16:31
Sure, did not mean to sound gruff... Using "SELECT DATEDIFF(Hour,GETDATE(),GETUTCDATE())" I get the result of "5"
– gritts
Oct 13 '14 at 16:36
Sure, did not mean to sound gruff... Using "SELECT DATEDIFF(Hour,GETDATE(),GETUTCDATE())" I get the result of "5"
– gritts
Oct 13 '14 at 16:36
@gritts Well, then, that explain your results, doesn't it?. So, you have results from the hours
0
till 5
(this because of your filter). Then, you are substracting 5 hours to those results (this from your DATEADD()
. So, now your results are for hours since 19
from the day before till 0
– Lamak
Oct 13 '14 at 16:39
@gritts Well, then, that explain your results, doesn't it?. So, you have results from the hours
0
till 5
(this because of your filter). Then, you are substracting 5 hours to those results (this from your DATEADD()
. So, now your results are for hours since 19
from the day before till 0
– Lamak
Oct 13 '14 at 16:39
1
1
@gritts If you want your results to be consistent, then you should apply that same correction to your filter (though if the results are actually what you are looking for, I'm not sure)
– Lamak
Oct 13 '14 at 16:51
@gritts If you want your results to be consistent, then you should apply that same correction to your filter (though if the results are actually what you are looking for, I'm not sure)
– Lamak
Oct 13 '14 at 16:51
|
show 1 more comment
up vote
0
down vote
accepted
Based on suggestions provided in response to my question, I came up with the following new query:
SELECT DATEPART(hour, DATEADD(HH,-DATEDIFF(Hour,GETDATE(),GETUTCDATE()),Documents.CreationTime)) AS Hours ,COUNT(*) AS Faxes,SUM(goodpagecount) AS [Good Page Count]
FROM Documents
JOIN Users ON Documents.OwnerID=Users.handle
JOIN Groups ON Users.GroupID=Groups.handle
JOIN History ON History.Owner=Documents.handle
JOIN HistoryTRX ON History.handle=HistoryTRX.handle
WHERE DATEADD(HH,-DATEDIFF(Hour,GETDATE(),GETUTCDATE()),Documents.CreationTime) >= '2014-10-01 00:00:00.000' and DATEADD(HH,-DATEDIFF(Hour,GETDATE(),GETUTCDATE()),Documents.CreationTime) <= '2014-10-03 08:00:00.000'
GROUP BY DATEPART(hour, DATEADD(HH,-DATEDIFF(Hour,GETDATE(),GETUTCDATE()),Documents.CreationTime))
ORDER BY DATEPART(hour, DATEADD(HH,-DATEDIFF(Hour,GETDATE(),GETUTCDATE()),Documents.CreationTime))
My changes are to the "WHERE" statement by adding my UTC compensation. The "WHERE" now matches the "SELECT".
Before:
WHERE Documents.CreationTime >= '2014-10-01 00:00:00.000' and Documents.CreationTime <= '2014-10-03 08:00:00.000'
After:
WHERE DATEADD(HH,-DATEDIFF(Hour,GETDATE(),GETUTCDATE()),Documents.CreationTime) >= '2014-10-01 00:00:00.000' and DATEADD(HH,-DATEDIFF(Hour,GETDATE(),GETUTCDATE()),Documents.CreationTime) <= '2014-10-03 08:00:00.000'
Also removed the BETWEEN keyword as it may not be as precise for results as I would like.
The results now look like:
Hours Faxes Good Page Count
0 3 4
1 5 9
3 9 50
4 8 16
5 14 40
add a comment |
up vote
0
down vote
accepted
Based on suggestions provided in response to my question, I came up with the following new query:
SELECT DATEPART(hour, DATEADD(HH,-DATEDIFF(Hour,GETDATE(),GETUTCDATE()),Documents.CreationTime)) AS Hours ,COUNT(*) AS Faxes,SUM(goodpagecount) AS [Good Page Count]
FROM Documents
JOIN Users ON Documents.OwnerID=Users.handle
JOIN Groups ON Users.GroupID=Groups.handle
JOIN History ON History.Owner=Documents.handle
JOIN HistoryTRX ON History.handle=HistoryTRX.handle
WHERE DATEADD(HH,-DATEDIFF(Hour,GETDATE(),GETUTCDATE()),Documents.CreationTime) >= '2014-10-01 00:00:00.000' and DATEADD(HH,-DATEDIFF(Hour,GETDATE(),GETUTCDATE()),Documents.CreationTime) <= '2014-10-03 08:00:00.000'
GROUP BY DATEPART(hour, DATEADD(HH,-DATEDIFF(Hour,GETDATE(),GETUTCDATE()),Documents.CreationTime))
ORDER BY DATEPART(hour, DATEADD(HH,-DATEDIFF(Hour,GETDATE(),GETUTCDATE()),Documents.CreationTime))
My changes are to the "WHERE" statement by adding my UTC compensation. The "WHERE" now matches the "SELECT".
Before:
WHERE Documents.CreationTime >= '2014-10-01 00:00:00.000' and Documents.CreationTime <= '2014-10-03 08:00:00.000'
After:
WHERE DATEADD(HH,-DATEDIFF(Hour,GETDATE(),GETUTCDATE()),Documents.CreationTime) >= '2014-10-01 00:00:00.000' and DATEADD(HH,-DATEDIFF(Hour,GETDATE(),GETUTCDATE()),Documents.CreationTime) <= '2014-10-03 08:00:00.000'
Also removed the BETWEEN keyword as it may not be as precise for results as I would like.
The results now look like:
Hours Faxes Good Page Count
0 3 4
1 5 9
3 9 50
4 8 16
5 14 40
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Based on suggestions provided in response to my question, I came up with the following new query:
SELECT DATEPART(hour, DATEADD(HH,-DATEDIFF(Hour,GETDATE(),GETUTCDATE()),Documents.CreationTime)) AS Hours ,COUNT(*) AS Faxes,SUM(goodpagecount) AS [Good Page Count]
FROM Documents
JOIN Users ON Documents.OwnerID=Users.handle
JOIN Groups ON Users.GroupID=Groups.handle
JOIN History ON History.Owner=Documents.handle
JOIN HistoryTRX ON History.handle=HistoryTRX.handle
WHERE DATEADD(HH,-DATEDIFF(Hour,GETDATE(),GETUTCDATE()),Documents.CreationTime) >= '2014-10-01 00:00:00.000' and DATEADD(HH,-DATEDIFF(Hour,GETDATE(),GETUTCDATE()),Documents.CreationTime) <= '2014-10-03 08:00:00.000'
GROUP BY DATEPART(hour, DATEADD(HH,-DATEDIFF(Hour,GETDATE(),GETUTCDATE()),Documents.CreationTime))
ORDER BY DATEPART(hour, DATEADD(HH,-DATEDIFF(Hour,GETDATE(),GETUTCDATE()),Documents.CreationTime))
My changes are to the "WHERE" statement by adding my UTC compensation. The "WHERE" now matches the "SELECT".
Before:
WHERE Documents.CreationTime >= '2014-10-01 00:00:00.000' and Documents.CreationTime <= '2014-10-03 08:00:00.000'
After:
WHERE DATEADD(HH,-DATEDIFF(Hour,GETDATE(),GETUTCDATE()),Documents.CreationTime) >= '2014-10-01 00:00:00.000' and DATEADD(HH,-DATEDIFF(Hour,GETDATE(),GETUTCDATE()),Documents.CreationTime) <= '2014-10-03 08:00:00.000'
Also removed the BETWEEN keyword as it may not be as precise for results as I would like.
The results now look like:
Hours Faxes Good Page Count
0 3 4
1 5 9
3 9 50
4 8 16
5 14 40
Based on suggestions provided in response to my question, I came up with the following new query:
SELECT DATEPART(hour, DATEADD(HH,-DATEDIFF(Hour,GETDATE(),GETUTCDATE()),Documents.CreationTime)) AS Hours ,COUNT(*) AS Faxes,SUM(goodpagecount) AS [Good Page Count]
FROM Documents
JOIN Users ON Documents.OwnerID=Users.handle
JOIN Groups ON Users.GroupID=Groups.handle
JOIN History ON History.Owner=Documents.handle
JOIN HistoryTRX ON History.handle=HistoryTRX.handle
WHERE DATEADD(HH,-DATEDIFF(Hour,GETDATE(),GETUTCDATE()),Documents.CreationTime) >= '2014-10-01 00:00:00.000' and DATEADD(HH,-DATEDIFF(Hour,GETDATE(),GETUTCDATE()),Documents.CreationTime) <= '2014-10-03 08:00:00.000'
GROUP BY DATEPART(hour, DATEADD(HH,-DATEDIFF(Hour,GETDATE(),GETUTCDATE()),Documents.CreationTime))
ORDER BY DATEPART(hour, DATEADD(HH,-DATEDIFF(Hour,GETDATE(),GETUTCDATE()),Documents.CreationTime))
My changes are to the "WHERE" statement by adding my UTC compensation. The "WHERE" now matches the "SELECT".
Before:
WHERE Documents.CreationTime >= '2014-10-01 00:00:00.000' and Documents.CreationTime <= '2014-10-03 08:00:00.000'
After:
WHERE DATEADD(HH,-DATEDIFF(Hour,GETDATE(),GETUTCDATE()),Documents.CreationTime) >= '2014-10-01 00:00:00.000' and DATEADD(HH,-DATEDIFF(Hour,GETDATE(),GETUTCDATE()),Documents.CreationTime) <= '2014-10-03 08:00:00.000'
Also removed the BETWEEN keyword as it may not be as precise for results as I would like.
The results now look like:
Hours Faxes Good Page Count
0 3 4
1 5 9
3 9 50
4 8 16
5 14 40
answered Oct 13 '14 at 21:29
gritts
651211
651211
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f26344434%2fsql-query-not-returning-expected-date-time-range%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
4
What's the result of
DATEDIFF(Hour,GETDATE(),GETUTCDATE())
for you?. Also, don't useBETWEEN
in this case for filtering aDATETIME
, instead ofBETWEEN '2014-10-01 00:00:00.000' AND '2014-10-01 23:59:59.999'
usecol >= '20141001' AND col < '20141002'
– Lamak
Oct 13 '14 at 16:18
To be honest, I'm not sure what your question is. What is the issue and what improvements do you want?
– Andriy M
Oct 13 '14 at 16:51
1
@AndriyM The problem is that the filter suggests times from
0
till5
, but the results are times from19
till0
. This is because of the "correction" that's being made on theSELECT
(-DATEDIFF(Hour,GETDATE(),GETUTCDATE())
). That correction is substracting 5 hours to the results, hence giving different results than expected– Lamak
Oct 13 '14 at 16:58
@gritts All right, some more details are needed. Is
Documents.CreationTime
UTC or local time? When you specify the boundaries, are they supposed to be UTC or local time? My guess is you would like the input and the output to be consistent (both to be either UTC or local time) but I'm not sure, and I'm also not sure if that is the actual problem you are trying to solve with this question.– Andriy M
Oct 13 '14 at 17:20
@AndriyM, the Documents.CreationTime is recorded in UTC. I am attempting to output in local time to save confusion in the final report to be created.
– gritts
Oct 13 '14 at 20:29