日期的最后一次出现 [英] last occurence of date
问题描述
我需要找到日期的最后一次出现(在我的情况下是5月1日的静态日期)
I need to find the last occurence of a date (in my case a static date 1st of may)
我做到了这一点,但我知道可以用更聪明的方式完成
I made this which works but i know this can be done in a much smarter way
declare @lastmay date
set @lastmay = DATEADD(YY,YEAR(GETDATE())-2000,'20000501')
IF @lastmay <= GETDATE()
BEGIN
SET @lastmay = DATEADD(YY,-1,@lastmay)
END
推荐答案
在SQL中使用日期时,对数据库中的Dates实用程序表进行比较是很有帮助的.
When you are working with dates in SQL it can be a real help to have a Dates utility table on your database that you can then compare against.
本文对此进行了很好的讨论: http://www.techrepublic.com/blog/datacenter/simplify-sql-server-2005-queries-with-a-dates-table/326
This article discusses it well: http://www.techrepublic.com/blog/datacenter/simplify-sql-server-2005-queries-with-a-dates-table/326
如果您实现了查询变得非常简单,例如(使用文章中的列)
If you implemented that your queries could become quite simple such as (using columns from article)
Declare @lastmay date
Select @lastmay = DateFull
from DateLookup
where MonthNumber = 5
and MonthDay = 1
and Datefull < getdate()
这篇关于日期的最后一次出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!