SELECT * FROM TABLE
WHERE YEAR(MDTFlgtStart)=YEAR(GETDATE()) AND MONTH(MDTFlgtStart)=MONTH(GETDATE())
以上代码将当前年份和月份与列年份和月份进行比较。 但我们是否有机会给年= 2012月= 3 或年= 2011月= 5
答案 0 :(得分:1)
您可以声明变量:
DECLARE @YEAR AS INT
DECLARE @MONTH AS INT
SET @YEAR = 2012
SET @MONTH = 3
SELECT *
FROM TABLE
WHERE YEAR(MDTFlgtStart)=@YEAR AND MONTH(MDTFlgtStart)=@MONTH
您可以将上述内容包含在可重用性的程序中....
答案 1 :(得分:1)
您可以使用这些值的参数。作为奖励,avoiding functions against the column will help assist a seek if an index exists on the column(当然SELECT *
means it will likely end up as a full scan anyway, or a range scan and a bunch of lookups)...
-- these would be input parameters for your stored procedure
DECLARE @y INT = 2011, @m INT = 5;
-- now have a date variable:
DECLARE @dt DATE = DATEADD(MONTH, @m-1, DATEADD(YEAR, @y-1900, 0));
SELECT ... FROM dbo.tablename
WHERE MDTFlgtStart >= @dt
AND MDTFlgtStart < DATEADD(MONTH, 1, @dt);
此外,您应该停止邀请任何将这些列命名为午餐的人,因为我必须假设他们不是很好。