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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Where Time

Author  Topic 

mapidea
Posting Yak Master

124 Posts

Posted - 2009-02-03 : 04:04:55
I want to get all the orders placed in month of december between 12 Noon and 5 pm.

I got the december part. How can we get the time section.

select * from
orders
where
(orderdate between '2008/12/12' and '2008/12/31')
and "What will be syntax to get time?"

Thanks

elancaster
A very urgent SQL Yakette

1208 Posts

Posted - 2009-02-03 : 04:07:24
look up the DATEPART function in books online

Em
Go to Top of Page

Nageswar9
Aged Yak Warrior

600 Posts

Posted - 2009-02-03 : 04:14:59
select * from
orders
where
orderdate between convert(varchar(32),datefield,100) and
convert(varchar(32),datefield,100)
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-02-03 : 04:16:46
try like this using datepart as elancaster said
select * from
orders
where
(orderdate between '2008/12/12' and '2008/12/31')
and datepart(h,orderdate) > 12 and datepart(hh,orderdate)<17
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-02-03 : 09:21:21
quote:
Originally posted by Nageswar9

select * from
orders
where
orderdate between convert(varchar(32),datefield,100) and
convert(varchar(32),datefield,100)



this might not work as expected as between will consider dates as strings rather than date values themselves
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-02-03 : 09:22:24
quote:
Originally posted by bklr

try like this using datepart as elancaster said
select * from
orders
where
(orderdate between '2008/12/12' and '2008/12/31')
and datepart(h,orderdate) > 12 and datepart(hh,orderdate)<17



this wont return any records between the dates which has a time part outside this range (12-17)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-02-03 : 09:23:37
[code]select * from
orders
where orderdate >= '2008/12/12 12:00'
and orderdate <='2008/12/31 17:00'
[/code]
Go to Top of Page

mapidea
Posting Yak Master

124 Posts

Posted - 2009-02-03 : 09:23:57
visakh16 the query you gave will return all the order between that date.

I am looking for orders in the particular time slot on all those days. Given by bklr.

Thanks
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-02-03 : 09:40:12
quote:
Originally posted by mapidea

visakh16 the query you gave will return all the order between that date.

I am looking for orders in the particular time slot on all those days. Given by bklr.

Thanks


Oh ok...i thought you're looking for time range.
In that case the previous suggestion should work fine.

Go to Top of Page

mapidea
Posting Yak Master

124 Posts

Posted - 2009-02-03 : 09:47:01
Yes. Thanks
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-02-03 : 23:15:43
hi visakh, for only timepart given query will works fine i have checked the query by sample data
Go to Top of Page
   

- Advertisement -