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)
 date

Author  Topic 

gyan.scs
Starting Member

1 Post

Posted - 2010-01-31 : 06:22:58
i want to find data between two date

Kristen
Test

22859 Posts

Posted - 2010-01-31 : 07:09:01
[code]
SELECT Col1, Col2, ...
FROM MyTable
WHERE MyDateColumn >= '20091231' -- 31-Dec-2009
AND MyDateColumn < '20100201' -- 01-Feb-2010
[/code]
Dates between 31-Dec-2009 and 31-Jan-2010 (inclusive) will be included.

Note that this will NOT include any MyDateColumn for 01-Feb-2010, but it WILL include MyDateColumn for 31-Jan-2010 23:59:59

This coding style will make use of an index MyDateColumn where possible.

If your Start and End dates are in @Variables you can "round" them to remove any time part, and to set the End Date to +1 day as follows:
[code]
DECLARE @StartDt datetime,
@EndDt datetime

SELECT @StartDt = '20091231', -- 31-Dec-2009 - First day to include
@EndDt = '20100131' -- 31-Jan-2010 - Last day to include

SELECT Col1, Col2, ...
FROM MyTable
WHERE MyDateColumn >= DATEADD(Day, DATEDIFF(Day, 0, @StartDt), 0)
AND MyDateColumn < DATEADD(Day, DATEDIFF(Day, 0, @EndDt), 1)
[/code]
Go to Top of Page
   

- Advertisement -