• 【SQL】How to get First and Last day of a month – TSQL


    – First Day Previous/Current/Next Months
    SELECT      DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) - 1, 0),
                'First Day of Previous Month'
    UNION ALL
    SELECT      DATEADD(DAY, -(DAY(DATEADD(MONTH, 1, GETDATE())) - 1),
                DATEADD(MONTH, -1, GETDATE())),
                'First Day of Previous Month (2)'
    UNION ALL
    SELECT      DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0),
                'First Day of Current Month'
    UNION ALL
    SELECT      DATEADD(DAY, -(DAY(GETDATE()) - 1), GETDATE()),
                'First Day of Current Month (2)'
    UNION ALL
    SELECT      DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) + 1, 0),
                'First Day of Next Month'
    UNION ALL
    SELECT      DATEADD(DAY, -(DAY(DATEADD(MONTH, 1, GETDATE())) - 1),
                DATEADD(MONTH, 1, GETDATE())),
                'First Day of Next Month (2)'

    Result Set:

                           

    ———————– ——————————-

    2011-04-01 00:00:00.000 First Day of Previous Month

    2011-04-01 15:47:36.660 First Day of Previous Month (2)

    2011-05-01 00:00:00.000 First Day of Current Month

    2011-05-01 15:47:36.660 First Day of Current Month (2)

    2011-06-01 00:00:00.000 First Day of Next Month

    2011-06-01 15:47:36.660 First Day of Next Month (2)

    (6 row(s) affected)

    The above queries can be generalized as below:

    DECLARE @DURATION INT = 2
    SELECT      DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) + @DURATION, 0)
                AS '+2 Months'
     
    SET @DURATION = -2
    SELECT      DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) + @DURATION, 0)
                AS '-2 Months'

    Result Set:

    +2 Months

    ———————–

    2011-07-01 00:00:00.000

    (1 row(s) affected)

    -2 Months

    ———————–

    2011-03-01 00:00:00.000

    (1 row(s) affected)

    And, to get last day of a month use:

    – Last Day Previous/Current/Next Months
    SELECT      DATEADD(DAY, -(DAY(GETDATE())), GETDATE()),
                'Last Day of Previous Month'
    UNION ALL
    SELECT      DATEADD(MILLISECOND, -3,
                DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)),
                'Last Day of Previous Month (2)'
    UNION ALL
    SELECT      DATEADD(DAY, -(DAY(DATEADD(MONTH, 1, GETDATE()))),
                DATEADD(MONTH, 1, GETDATE())),
                'Last Day of Current Month'
    UNION ALL
    SELECT      DATEADD(MILLISECOND, -3,
                DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) + 1, 0)),
                'Last Day of Current Month (2)'
    UNION ALL
    SELECT      DATEADD(DAY, -(DAY(DATEADD(MONTH,0,GETDATE()))),
                DATEADD(MONTH, 2, GETDATE())),
                'Last Day of Next Month'
    UNION ALL
    SELECT      DATEADD(SECOND, -1,
                DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) + 2, 0)),
                'Last Day of Next Month (2)'

    Result Set:

                           

    ———————– ——————————

    2011-04-30 15:54:35.523 Last Day of Previous Month

    2011-04-30 23:59:59.997 Last Day of Previous Month (2)

    2011-05-31 15:54:35.523 Last Day of Current Month

    2011-05-31 23:59:59.997 Last Day of Current Month (2)

    2011-06-30 15:54:35.523 Last Day of Next Month

    2011-06-30 23:59:59.000 Last Day of Next Month (2)

    (6 row(s) affected)

    The above queries can be generalized as below:

    DECLARE @DURATION INT = 2
    SELECT   DATEADD(MILLISECOND, -3,
             DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) + @DURATION + 1, 0))
             AS '+2 Months'
     
    SET @DURATION = -2
    SELECT   DATEADD(MILLISECOND, -3,
             DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) + @DURATION + 1, 0))
             AS '-2 Months'

    Result Set:

    +2 Months

    ———————–

    2011-07-31 23:59:59.997

    (1 row(s) affected)

    -2 Months

    ———————–

    2011-03-31 23:59:59.997

    (1 row(s) affected)

    Hope This Helps!

  • 相关阅读:
    类成员函数的重载、覆盖和隐藏区别 (C++)(转)
    man时括号里的数字是啥意思
    Redis事务
    功能接口
    持久化方式
    宿主
    路由
    静态文件
    Log4Net 配置
    Redis命令与配置
  • 原文地址:https://www.cnblogs.com/taoqianbao/p/2835386.html
Copyright © 2020-2023  润新知