Member 13863605 Ответов: 1

Позвольте пользователю выбрать, сколько дней данных он хочет получить


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.

Что я уже пробовал:

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()))

1 Ответов

Рейтинг:
9

C Pottinger

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

это позволит удалить часть часов из даты, с которой вы сравниваете, так что ваше сравнение станет where employeedate >= startofday

Иначе говоря:
DATEADD(D, 0, DATEDIFF(D, 0, @sometimestamp))
выбирает начало дня для @sometimestamp