SQL Filter Out Specific Month, Day, and Time for Any Year

百般思念 提交于 2019-12-07 03:19:26

May be this:

WHERE MONTH([Posting Date]) <> 12 OR
      DAY([Posting Date]) <> 31 OR
      CAST([Posting Date] AS TIME) <> CAST('23:59:59.000' AS TIME)

Even more short answer:

WHERE YEAR([Posting Date]) <> YEAR(DATEADD(ss, 1, [Posting Date]))

Another thing which you can also do is something like this, which is a slight variation from what you have tried.

WHERE 1 = CASE 
        WHEN (
                DATEPART(MONTH, [POSTING DATE]) = 12
                AND DATEPART(DAY, [POSTING DATE]) = 31
                AND DATEPART(HOUR, [POSTING DATE]) = 23
                AND DATEPART(MINUTE, [POSTING DATE]) = 59
                AND DATEPART(SECOND, [POSTING DATE]) = 59
                )
            THEN 0
        ELSE 1
        END

In this scenario I just use a CASE to identify those rows which have the date the last second of the year and the evaluation of 1/0 does the rest and filters out the unwanted records.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!