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
 DateTime Range

Author  Topic 

akien_ghie09
Starting Member

22 Posts

Posted - 2011-12-20 : 21:51:22
Can somebody tell me how I can make a date and time range in my code?

For example I want a result for the date of Sept.15 to Oct.30.

Thanks.



AkiEn09

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-12-20 : 22:21:26
[code]
WHERE datecol >= '2011-09-15'
and datecol < '2011-10-31'
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

akien_ghie09
Starting Member

22 Posts

Posted - 2011-12-20 : 22:38:56
I don't have specific date. It can change depending on what my boss like.

Can I use BETWEEN?

For Example

WHERE OrdersHistory.OrderDate BETWEEN '2011-10-01' AND '2011-10-30'

Is that right?

Thanks!



AkiEn09
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-12-20 : 22:45:11
yes. you can use BETWEEN.

Does the OrderDate contain the time ? If it does, you will need to take care of that


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

akien_ghie09
Starting Member

22 Posts

Posted - 2011-12-20 : 23:01:18
Yes, it does contain.

What do you mean I have to take care this?

In what way?

AkiEn09
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-12-20 : 23:21:07
quote:
WHERE OrdersHistory.OrderDate BETWEEN '2011-10-01' AND '2011-10-30'

this is actually equates to

WHERE OrdersHistory.OrderDate >= '2011-10-01'
AND OrdersHistory.OrderDate <= '2011-10-30'


If OrderDate contains time, like '2011-10-30 12:34:56' it will excluded as '2011-10-30' implies '2011-10-30 00:00:00'

To include such records, we checks for LESS THAN the next day '2011-10-31' which is as shown in my first post.

WHERE OrdersHistory.OrderDate >= '2011-10-01'
AND OrdersHistory.OrderDate < '2011-10-31'



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

akien_ghie09
Starting Member

22 Posts

Posted - 2011-12-21 : 00:32:50
Okay. Thank you for the help!


AkiEn09
Go to Top of Page
   

- Advertisement -