Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jingyang-li/f3f806b2e429c9604e75d4722ce6990b to your computer and use it in GitHub Desktop.
Save jingyang-li/f3f806b2e429c9604e75d4722ce6990b to your computer and use it in GitHub Desktop.
create table #temp (ID varchar(50)
,DateTime_ dateTime)
insert into #temp (ID,DateTime_) values ('1001','2022-01-01 06:01:24.000')
,('1002','2022-01-01 06:07:24.000')
,('1003','2022-01-01 06:15:24.000')
, ('1004','2022-01-01 06:20:24.000')
,('1005','2022-01-01 06:39:24.000')
, ('1006','2022-01-01 06:46:24.000')
, ('1007','2022-01-01 16:59:24.000')
select ID
,datetimefromparts (Year(DateTime_),Month(DateTime_),day(DateTime_)
,datepart(hour,DATEADD(hour, DATEDIFF(hour, 0,DateTime_)+(Case when floor(datepart(minute,DateTime_) /15.) =3 then 1 else 0 end), 0) )
,Case when floor(datepart(minute,DateTime_) /15.) in(1,2) then 30 else 0 end
,0
,0
) dtRounded30
from #temp
DROP Table #TEMP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment