【问题标题】:last occurence of date最后出现的日期
【发布时间】:2011-08-31 10:18:39
【问题描述】:

我需要找到最后一次出现的日期(在我的例子中是 5 月 1 日的静态日期)

我做了这个工作,但我知道这可以用更聪明的方式完成

declare @lastmay date
set @lastmay = DATEADD(YY,YEAR(GETDATE())-2000,'20000501')
IF @lastmay <= GETDATE()
BEGIN
    SET @lastmay = DATEADD(YY,-1,@lastmay)
END

【问题讨论】:

    标签: sql-server tsql date dateadd


    【解决方案1】:

    当您在 SQL 中处理日期时,在您的数据库中拥有一个 Dates 实用程序表会很有帮助,然后您可以进行比较。

    本文讨论得很好:http://www.techrepublic.com/blog/datacenter/simplify-sql-server-2005-queries-with-a-dates-table/326

    如果您实现了查询可能会变得非常简单,例如(使用文章中的列)

    Declare @lastmay date
    
    Select @lastmay = DateFull 
    from DateLookup
        where MonthNumber = 5
        and MonthDay = 1
        and Datefull < getdate()
    

    【讨论】:

      猜你喜欢
      • 2018-01-03
      • 1970-01-01
      • 2016-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多