Get current year in TSQL

前端 未结 2 1258
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-12 07:08

I have data I am trying to pull for a report and I am working on a Year to Date report. My columns in the table are formatted as datetime. I am trying to run a select statement

相关标签:
2条回答
  • 2021-02-12 07:54

    Yes, it's remarkably easy in fact:

    WHERE YEAR(A.[reimbursementDate]) = YEAR(GETDATE())
    
    0 讨论(0)
  • 2021-02-12 08:10

    This should be a better way if performance is an issue (although it isn't as readable)

    where A.[reimbursementDate] between  
         DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()), 0) and
         DATEADD(MILLISECOND, -3, DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()) + 1, 0))
    

    Those funky DATEADD statements return the first and last days of the current year (through December 31 23:59:59.997). Since reimbursementDate isn't contained in a function, the query will be able to take advantage of any applicable indexes.

    0 讨论(0)
提交回复
热议问题