2011-02-18 77 views

回答

3

你很近。假設date列是datetime數據類型:

SELECT * 
    FROM dbo.events 
    WHERE DATEPART(year, date) BETWEEN $year-1 AND $year 
1

SQL Server具有YEAR function,這甚至短於DATEPART寫:

select * from dbo.events where year(date) between $year-1 and $year 
0

試試這個:

declare @year int 
set @year=2010 

select * from FROM dbo.events 
WHERE cast(YEAR([date]) as int) <= @year 
and cast(YEAR([date]) as int) >= @year-1 
1

你試圖選擇只有一年?

這個工作對我來說:

$year = 2011; 
select * from table where year(date_field) = $year