我不熟悉SQL Server语法,但我想它会是这样的:
$year = 2010;
$query = mssql_query("SELECT * FROM dbo.events
WHERE date BETWEEN $year AND $year-1");
在这种情况下,我只是不确定如何格式化$ year。
答案 0 :(得分:3)
你很亲密。假设date
列是datetime数据类型:
SELECT *
FROM dbo.events
WHERE DATEPART(year, date) BETWEEN $year-1 AND $year
答案 1 :(得分:1)
SQL Server有YEAR function,写入比DATEPART更短:
select * from dbo.events where year(date) between $year-1 and $year
答案 2 :(得分:1)
这对我有用:
$year = 2011;
select * from table where year(date_field) = $year
答案 3 :(得分: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