Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 pull data by certain hour

Author  Topic 

peace
Constraint Violating Yak Guru

420 Posts

Posted - 2014-09-03 : 00:42:42
Can anyone help me on the logic. I would like to pull a data every 4 hours.

select columnA from tableA

the columnA will consist lots of datetime value.

would like to pull 00:00-04:00, 04:01-08:00, and so on..

i tried something like this..

declare @time time
declare @condition time
set @time = CONVERT(VARCHAR(8),dateadd(hour,8,GETDATE()),108)
set @condition = CONVERT(VARCHAR(8),dateadd(hour,8,GETDATE()),108)

select @time, @condition

If @condition < @time
Begin

select columnA from tableA
where CONVERT(VARCHAR(8),columnA ,108)>= '00:00:00'
and CONVERT(VARCHAR(8),columnA ,108)<= '04:00:00'

End

Else

Begin

select columnA from tableA
where CONVERT(VARCHAR(8),columnA ,108)>= '04:01:00'
and CONVERT(VARCHAR(8),columnA ,108)<= '08:00:00'

End

Else
'
'
'
'
   

- Advertisement -