sql server 2008 r2 - sql : which dates contain time between 12:00 and 22:00 -


i have mytable column rectime datetime

rectime ----------------------- 2013-05-22 15:32:37.530 2013-05-22 22:11:16.103 2013-05-22 16:24:06.883 2013-05-22 16:38:30.717 2013-05-22 23:54:41.777 2013-05-23 22:01:00.000 2013-05-23 09:59:59.997 

i need sql statement tell me dates contain time between 12:00 , 22:00

expected result :-

rectime                 | foo ------------------------|----- 2013-05-22 15:32:37.530 |   1 2013-05-22 22:11:16.103 |   0 2013-05-22 16:24:06.883 |   1 2013-05-22 16:38:30.717 |   1 2013-05-22 23:54:41.777 |   0 2013-05-23 22:01:00.000 |   0 2013-05-23 09:59:59.997 |   0 

for use following :-

select      [rectime]     , case when [rectime] >= convert(datetime, convert(varchar(4), datepart(year, [rectime])) + '-' + convert(varchar(2), datepart(month, [rectime])) + '-' + convert(varchar(2), datepart(day, [rectime])) + ' 12:00')             , [rectime] <= convert(datetime, convert(varchar(4), datepart(year, [rectime])) + '-' + convert(varchar(2), datepart(month, [rectime])) + '-' + convert(varchar(2), datepart(day, [rectime])) + ' 22:00')         1         else 0     end   dbo.mytable 

and know not best solution / performance.

help appreciated

one more approach

select rectime,         case when cast(rectime time)                   between '12:00:00' , '22:00:00'              1 else 0 end foo   table1 

output:

 |                    rectime | foo | |----------------------------|-----| | may, 22 2013 15:32:37+0000 |   1 | | may, 22 2013 22:11:16+0000 |   0 | | may, 22 2013 16:24:06+0000 |   1 | | may, 22 2013 16:38:30+0000 |   1 | | may, 22 2013 23:54:41+0000 |   0 | | may, 23 2013 22:01:00+0000 |   0 | | may, 23 2013 09:59:59+0000 |   0 | 

here sqlfiddle demo


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -