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
 get hhmmss by date

Author  Topic 

peace
Constraint Violating Yak Guru

420 Posts

Posted - 2014-01-02 : 03:42:39
how can i get the hhmmss between 9am to 11am for every date in my datetime column.

i tried:

select datetime from tableA where datetime between '2014-01-03 09:00:00' and '2014-01-31 11:00:00'

but it returns all

datetime
2014-01-31 05:30:00
2014-01-06 09:30:00
2014-01-06 10:30:00
.
.
.
.

Ifor
Aged Yak Warrior

700 Posts

Posted - 2014-01-02 : 08:42:42
[code]
SELECT YourDate
FROM YourTable
WHERE DATEADD(d, -DATEDIFF(d, 0, YourDate), YourDate) BETWEEN '19000101 09:00:00' AND '19000101 11:00:00'
[/code]
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2014-01-02 : 12:49:51
or simply this


WHERE DATEPART(hh,Date) BETWEEN 9 AND 11


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2014-01-06 : 02:03:53
More querying on datetime column http://beyondrelational.com/modules/2/blogs/70/posts/10899/understanding-datetime-column-part-iii.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -