Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to display current days data only. Currently my data is in UTC time and the way I wrote my query looks like it's not show my current days data. Instead it's showing me data which include yesterday's data too. I have declared @Days where the user will set the number of days they want the data from. Like if today is the the 10th and is currently 5pm, I want the data from midnight till 5pm only for the 10th. But when I set @Days = -1, it's actually going back and showing me data from 5pm on the 9th to 5pm of the 10th. But I want if the user selects @Days = -7, it'll show data for the last SEVEN DAYS ONLY(i.e. from 4th midnight to 5pm of the 10th) not last 7 * 24 = 168 hours from now.

What I have tried:

declare @Days int

set @Days = -1

select      

dateadd(hour,datepart(hour,Timestamp),cast(CAST((Timestamp) as date) as datetime)) as [Time]      

from  [Employee]

where dateadd(hour,datepart(hour,Timestamp),cast(CAST((Timestamp) as date) as datetime)) >= DATEADD(day, @Days , GETDATE()))
Posted
Updated 25-Jul-18 10:52am
v3

1 solution

where dateadd(hour,datepart(hour,Timestamp),cast(CAST((Timestamp) as date) as datetime)) >= DATEADD(D, 0, DATEDIFF(D, 0, DATEADD(day, @Days + 1, GETDATE()))))

this will remove the hours portion from the date you are comparing against, so that your comparision becomes where employeedate >= startofday

In other words:
DATEADD(D, 0, DATEDIFF(D, 0, @sometimestamp))
selects the start of day for @sometimestamp
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900