如何在SQL Server中获取上个月的第一天和最后一天(带时间戳)

时间:2012-07-31 15:37:13

标签: sql-server datetime

我找不到上个月的第一天和最后一天带有时间戳的解决方案。希望这有助于其他人。如果已经有解决这个问题的方法,我道歉。

这是解决方案。

 SELECT DATEADD(month, DATEDIFF(month, -1, getdate()) - 2, 0) as FirtDayPreviousMonthWithTimeStamp,
    DATEADD(ss, -1, DATEADD(month, DATEDIFF(month, 0, getdate()), 0)) as LastDayPreviousMonthWithTimeStamp

如果currentdate = '2012-7-31'

,将返回以下内容

结果:2012-06-01 00:00:00.000 2012-06-30 23:59:59.000

如果currentdate = '2012-1-1'

,将返回以下内容

结果:2011-12-01 00:00:00.000 2011-12-31 23:59:59.000

13 个答案:

答案 0 :(得分:188)

select DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-1, 0) --First day of previous month
select DATEADD(MONTH, DATEDIFF(MONTH, -1, GETDATE())-1, -1) --Last Day of previous month

答案 1 :(得分:100)

本周的第一天。

select CONVERT(varchar,dateadd(week,datediff(week,0,getdate()),0),106)

本周的最后一天。

select CONVERT(varchar,dateadd(week,datediff(week,0,getdate()),6),106)

上周的第一天。

select CONVERT(varchar,DATEADD(week,datediff(week,7,getdate()),0),106)

上周的最后一天。

select CONVERT(varchar,dateadd(week,datediff(week,7,getdate()),6),106)

下周的第一天。

select CONVERT(varchar,dateadd(week,datediff(week,0,getdate()),7),106)

下周的最后一天。

select CONVERT(varchar,dateadd(week,datediff(week,0,getdate()),13),106)

本月第一天。

select CONVERT(varchar,dateadd(d,-(day(getdate()-1)),getdate()),106)

本月最后一天。

select CONVERT(varchar,dateadd(d,-(day(dateadd(m,1,getdate()))),dateadd(m,1,getdate())),106)

在此示例作品中,仅日期为31.而剩余日期不是。

上个月的第一天。

select CONVERT(varchar,dateadd(d,-(day(dateadd(m,-1,getdate()-2))),dateadd(m,-1,getdate()-1)),106)

上个月的最后一天。

select CONVERT(varchar,dateadd(d,-(day(getdate())),getdate()),106)

下个月的第一天。

select CONVERT(varchar,dateadd(d,-(day(dateadd(m,1,getdate()-1))),dateadd(m,1,getdate())),106)

下个月的最后一天。

select CONVERT(varchar,dateadd(d,-(day(dateadd(m,2,getdate()))),DATEADD(m,2,getdate())),106)

本年度的第一天。

select CONVERT(varchar,dateadd(year,datediff(year,0,getdate()),0),106)

当年的最后一天。

select CONVERT(varchar,dateadd(ms,-2,dateadd(year,0,dateadd(year,datediff(year,0,getdate())+1,0))),106)

去年的第一天。

select CONVERT(varchar,dateadd(year,datediff(year,0,getdate())-1,0),106)

去年的最后一天。

select CONVERT(varchar,dateadd(ms,-2,dateadd(year,0,dateadd(year,datediff(year,0,getdate()),0))),106)

明年的第一天。

select CONVERT(varchar,dateadd(YEAR,DATEDIFF(year,0,getdate())+1,0),106)

明年的最后一天。

select CONVERT(varchar,dateadd(ms,-2,dateadd(year,0,dateadd(year,datediff(year,0,getdate())+2,0))),106)

答案 2 :(得分:14)

SELECT CONVERT(DATE,DATEADD(MM, DATEDIFF(MM, 0, GETDATE())-1, 0)) AS FirstDayOfPrevMonth
SELECT CONVERT(DATE,DATEADD(MS, -3, DATEADD(MM, DATEDIFF(MM, 0, GETDATE()) , 0))) AS LastDayOfPrevMonth

答案 3 :(得分:10)

<强>解决方案

  

您请求的日期格式称为 ODBC 格式(代码 120 )。

要实际计算您请求的值,请在SQL中包含以下内容。

  

复制,粘贴......

DECLARE
    @FirstDayOfLastMonth DATETIME = CONVERT(DATE, DATEADD(d, -( DAY(DATEADD(m, -1, GETDATE() - 2)) ), DATEADD(m, -1, GETDATE() - 1)))
    , @LastDayOfLastMonth DATETIME = CONVERT(DATE, DATEADD(d, -( DAY(GETDATE()) ), GETDATE()))
  

...并在您的代码中使用:

     
      
  • @FirstDayOfLastMonth
  •   
  • @LastDayOfLastMonth
  •   

请注意,它必须比引用参数的任何语句更早粘贴,但从那时起,您可以在代码中引用 @FirstDayOfLastMonth @LastDayOfLastMonth

示例

让我们看一些代码:

DECLARE
    @FirstDayOfLastMonth DATETIME = CONVERT(DATE, DATEADD(d, -( DAY(DATEADD(m, -1, GETDATE() - 2)) ), DATEADD(m, -1, GETDATE() - 1)))
    , @LastDayOfLastMonth DATETIME = CONVERT(DATE, DATEADD(d, -( DAY(GETDATE()) ), GETDATE()))

SELECT
    'First day of last month' AS Title, CONVERT(VARCHAR, @FirstDayOfLastMonth , 120) AS [ODBC]
UNION 

SELECT 
    'Last day of last month' AS Title, CONVERT(VARCHAR, @LastDayOfLastMonth , 120) AS [ODBC]

运行以上代码以生成以下输出:

Figure a

注意: 请记住,今天的日期是 2016年9月12日

  

更多(为了完整性)

常用日期参数

你还想要更多吗?

要设置更全面的便捷日期相关参数,请在SQL中包含以下内容:

DECLARE
    @FirstDayOfCurrentWeek DATETIME = CONVERT(DATE, DATEADD(WEEK, DATEDIFF(WEEK, 0, GETDATE()), 0))
    , @LastDayOfCurrentWeek DATETIME = CONVERT(DATE, DATEADD(WEEK, DATEDIFF(WEEK, 0, GETDATE()), 6))
    , @FirstDayOfLastWeek DATETIME = CONVERT(DATE, DATEADD(WEEK, DATEDIFF(WEEK, 7, GETDATE()), 0))
    , @LastDayOfLastWeek DATETIME = CONVERT(DATE, DATEADD(WEEK, DATEDIFF(WEEK, 7, GETDATE()), 6))
    , @FirstDayOfNextWeek DATETIME = CONVERT(DATE, DATEADD(WEEK, DATEDIFF(WEEK, 0, GETDATE()), 7))
    , @LastDayOfNextWeek DATETIME = CONVERT(DATE, DATEADD(WEEK, DATEDIFF(WEEK, 0, GETDATE()), 13))
    , @FirstDayOfCurrentMonth DATETIME = CONVERT(DATE, DATEADD(d, -( DAY(GETDATE() - 1) ), GETDATE()))
    , @LastDayOfCurrentMonth DATETIME = CONVERT(DATE, DATEADD(d, -( DAY(DATEADD(m, 1, GETDATE())) ), DATEADD(m, 1, GETDATE())))
    , @FirstDayOfLastMonth DATETIME = CONVERT(DATE, DATEADD(d, -( DAY(DATEADD(m, -1, GETDATE() - 2)) ), DATEADD(m, -1, GETDATE() - 1)))
    , @LastDayOfLastMonth DATETIME = CONVERT(DATE, DATEADD(d, -( DAY(GETDATE()) ), GETDATE()))
    , @FirstDayOfNextMonth DATETIME = CONVERT(DATE, DATEADD(d, -( DAY(DATEADD(m, 1, GETDATE() - 1)) ), DATEADD(m, 1, GETDATE())))
    , @LastDayOfNextMonth DATETIME = CONVERT(DATE, DATEADD(d, -( DAY(DATEADD(m, 2, GETDATE())) ), DATEADD(m, 2, GETDATE())))
    , @FirstDayOfCurrentYear DATETIME = CONVERT(DATE, DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()), 0))
    , @LastDayOfCurrentYear DATETIME = CONVERT(DATE, DATEADD(ms, -2, DATEADD(YEAR, 0, DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()) + 1, 0))))
    , @FirstDayOfLastYear DATETIME = CONVERT(DATE, DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()) - 1, 0))
    , @LastDayOfLastYear DATETIME = CONVERT(DATE, DATEADD(ms, -2, DATEADD(YEAR, 0, DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()), 0))))
    , @FirstDayOfNextYear DATETIME = CONVERT(DATE, DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()) + 1, 0))
    , @LastDayOfNextYear DATETIME = CONVERT(DATE, DATEADD(ms, -2, DATEADD(YEAR, 0, DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()) + 2, 0))))

最好先包含它,最好是在过程或SQL查询的顶部。

声明后,可以在代码中的任何位置引用参数,这些参数可以根据需要多次引用。

示例

让我们看一些代码:

DECLARE
    @FirstDayOfCurrentWeek DATETIME = CONVERT(DATE, DATEADD(WEEK, DATEDIFF(WEEK, 0, GETDATE()), 0))
    , @LastDayOfCurrentWeek DATETIME = CONVERT(DATE, DATEADD(WEEK, DATEDIFF(WEEK, 0, GETDATE()), 6))
    , @FirstDayOfLastWeek DATETIME = CONVERT(DATE, DATEADD(WEEK, DATEDIFF(WEEK, 7, GETDATE()), 0))
    , @LastDayOfLastWeek DATETIME = CONVERT(DATE, DATEADD(WEEK, DATEDIFF(WEEK, 7, GETDATE()), 6))
    , @FirstDayOfNextWeek DATETIME = CONVERT(DATE, DATEADD(WEEK, DATEDIFF(WEEK, 0, GETDATE()), 7))
    , @LastDayOfNextWeek DATETIME = CONVERT(DATE, DATEADD(WEEK, DATEDIFF(WEEK, 0, GETDATE()), 13))
    , @FirstDayOfCurrentMonth DATETIME = CONVERT(DATE, DATEADD(d, -( DAY(GETDATE() - 1) ), GETDATE()))
    , @LastDayOfCurrentMonth DATETIME = CONVERT(DATE, DATEADD(d, -( DAY(DATEADD(m, 1, GETDATE())) ), DATEADD(m, 1, GETDATE())))
    , @FirstDayOfLastMonth DATETIME = CONVERT(DATE, DATEADD(d, -( DAY(DATEADD(m, -1, GETDATE() - 2)) ), DATEADD(m, -1, GETDATE() - 1)))
    , @LastDayOfLastMonth DATETIME = CONVERT(DATE, DATEADD(d, -( DAY(GETDATE()) ), GETDATE()))
    , @FirstDayOfNextMonth DATETIME = CONVERT(DATE, DATEADD(d, -( DAY(DATEADD(m, 1, GETDATE() - 1)) ), DATEADD(m, 1, GETDATE())))
    , @LastDayOfNextMonth DATETIME = CONVERT(DATE, DATEADD(d, -( DAY(DATEADD(m, 2, GETDATE())) ), DATEADD(m, 2, GETDATE())))
    , @FirstDayOfCurrentYear DATETIME = CONVERT(DATE, DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()), 0))
    , @LastDayOfCurrentYear DATETIME = CONVERT(DATE, DATEADD(ms, -2, DATEADD(YEAR, 0, DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()) + 1, 0))))
    , @FirstDayOfLastYear DATETIME = CONVERT(DATE, DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()) - 1, 0))
    , @LastDayOfLastYear DATETIME = CONVERT(DATE, DATEADD(ms, -2, DATEADD(YEAR, 0, DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()), 0))))
    , @FirstDayOfNextYear DATETIME = CONVERT(DATE, DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()) + 1, 0))
    , @LastDayOfNextYear DATETIME = CONVERT(DATE, DATEADD(ms, -2, DATEADD(YEAR, 0, DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()) + 2, 0))))

SELECT  
    'a) FirstDayOfCurrentWeek.' AS [Title] ,
    @FirstDayOfCurrentWeek AS [DATE (Server default)] ,
    CONVERT(VARCHAR, @FirstDayOfCurrentWeek, 127) AS [ISO8601] ,
    CONVERT(VARCHAR, @FirstDayOfCurrentWeek, 103) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [British&French] ,
    CONVERT(VARCHAR, @FirstDayOfCurrentWeek, 104) + ' ' + CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [German] ,
    CONVERT(VARCHAR, @FirstDayOfCurrentWeek, 105) + ' ' + CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [Italian] ,
    CONVERT(VARCHAR, @FirstDayOfCurrentWeek, 111) + ' ' + CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [Japan] ,
    CONVERT(VARCHAR, @FirstDayOfCurrentWeek, 100) AS [U.S.] ,
    CONVERT(VARCHAR, @FirstDayOfCurrentWeek, 120) AS [ODBC]
UNION
SELECT  
    'b) LastDayOfCurrentWeek.' AS [Title] ,
    @LastDayOfCurrentWeek AS [DATE (Server default)] ,
    CONVERT(VARCHAR, @LastDayOfCurrentWeek, 127) AS [ISO8601] ,
    CONVERT(VARCHAR, @LastDayOfCurrentWeek, 103) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [British&French] ,
    CONVERT(VARCHAR, @LastDayOfCurrentWeek, 104) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [German] ,
    CONVERT(VARCHAR, @LastDayOfCurrentWeek, 105) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [Italian] ,
    CONVERT(VARCHAR, @LastDayOfCurrentWeek, 111) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [Japan] ,
    CONVERT(VARCHAR, @LastDayOfCurrentWeek, 100) AS [U.S.] ,
    CONVERT(VARCHAR, @LastDayOfCurrentWeek, 120) AS [ODBC]
UNION
SELECT  
    'c) FirstDayOfLastWeek.' AS [Title] ,
    @FirstDayOfLastWeek AS [DATE (Server default)] ,
    CONVERT(VARCHAR, @FirstDayOfLastWeek, 127) AS [ISO8601] ,
    CONVERT(VARCHAR, @FirstDayOfLastWeek, 103) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [British&French] ,
    CONVERT(VARCHAR, @FirstDayOfLastWeek, 104) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [German] ,
    CONVERT(VARCHAR, @FirstDayOfLastWeek, 105) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [Italian] ,
    CONVERT(VARCHAR, @FirstDayOfLastWeek, 111) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [Japan] ,
    CONVERT(VARCHAR, @FirstDayOfLastWeek, 100) AS [U.S.] ,
    CONVERT(VARCHAR, @FirstDayOfLastWeek, 120) AS [ODBC]
UNION
SELECT  
    'd) LastDayOfLastWeek.' AS [Title] ,
    @LastDayOfLastWeek AS [DATE (Server default)] ,
    CONVERT(VARCHAR, @LastDayOfLastWeek, 127) AS [ISO8601] ,
    CONVERT(VARCHAR, @LastDayOfLastWeek, 103) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [British&French] ,
    CONVERT(VARCHAR, @LastDayOfLastWeek, 104) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [German] ,
    CONVERT(VARCHAR, @LastDayOfLastWeek, 105) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [Italian] ,
    CONVERT(VARCHAR, @LastDayOfLastWeek, 111) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [Japan] ,
    CONVERT(VARCHAR, @LastDayOfLastWeek, 100) AS [U.S.] ,
    CONVERT(VARCHAR, @LastDayOfLastWeek, 120) AS [ODBC]
UNION
SELECT  
    'e) FirstDayOfNextWeek.' AS [Title] ,
    @FirstDayOfNextWeek AS [DATE (Server default)] ,
    CONVERT(VARCHAR, @FirstDayOfNextWeek, 127) AS [ISO8601] ,
    CONVERT(VARCHAR, @FirstDayOfNextWeek, 103) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [British&French] ,
    CONVERT(VARCHAR, @FirstDayOfNextWeek, 104) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [German] ,
    CONVERT(VARCHAR, @FirstDayOfNextWeek, 105) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [Italian] ,
    CONVERT(VARCHAR, @FirstDayOfNextWeek, 111) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [Japan] ,
    CONVERT(VARCHAR, @FirstDayOfNextWeek, 100) AS [U.S.] ,
    CONVERT(VARCHAR, @FirstDayOfNextWeek, 120) AS [ODBC]
UNION
SELECT  
    'f) LastDayOfNextWeek.' AS [Title] ,
    @LastDayOfNextWeek AS [DATE (Server default)] ,
    CONVERT(VARCHAR, @LastDayOfNextWeek, 127) AS [ISO8601] ,
    CONVERT(VARCHAR, @LastDayOfNextWeek, 103) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [British&French] ,
    CONVERT(VARCHAR, @LastDayOfNextWeek, 104) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [German] ,
    CONVERT(VARCHAR, @LastDayOfNextWeek, 105) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [Italian] ,
    CONVERT(VARCHAR, @LastDayOfNextWeek, 111) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [Japan] ,
    CONVERT(VARCHAR, @LastDayOfNextWeek, 100) AS [U.S.] ,
    CONVERT(VARCHAR, @LastDayOfNextWeek, 120) AS [ODBC]
UNION
SELECT  
    'g) FirstDayOfCurrentMonth.' AS [Title] ,
    @FirstDayOfCurrentMonth AS [DATE (Server default)] ,
    CONVERT(VARCHAR, @FirstDayOfCurrentMonth, 127) AS [ISO8601] ,
    CONVERT(VARCHAR, @FirstDayOfCurrentMonth, 103) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [British&French] ,
    CONVERT(VARCHAR, @FirstDayOfCurrentMonth, 104) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [German] ,
    CONVERT(VARCHAR, @FirstDayOfCurrentMonth, 105) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [Italian] ,
    CONVERT(VARCHAR, @FirstDayOfCurrentMonth, 111) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [Japan] ,
    CONVERT(VARCHAR, @FirstDayOfCurrentMonth, 100) AS [U.S.] ,
    CONVERT(VARCHAR, @FirstDayOfCurrentMonth, 120) AS [ODBC]
UNION
SELECT  
    'h) LastDayOfCurrentMonth.' AS [Title] ,
    @LastDayOfCurrentMonth AS [DATE (Server default)] ,
    CONVERT(VARCHAR, @LastDayOfCurrentMonth, 127) AS [ISO8601] ,
    CONVERT(VARCHAR, @LastDayOfCurrentMonth, 103) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [British&French] ,
    CONVERT(VARCHAR, @LastDayOfCurrentMonth, 104) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [German] ,
    CONVERT(VARCHAR, @LastDayOfCurrentMonth, 105) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [Italian] ,
    CONVERT(VARCHAR, @LastDayOfCurrentMonth, 111) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [Japan] ,
    CONVERT(VARCHAR, @LastDayOfCurrentMonth, 100) AS [U.S.] ,
    CONVERT(VARCHAR, @LastDayOfCurrentMonth, 120) AS [ODBC]
UNION
SELECT  
    'i) FirstDayOfLastMonth.' AS [Title] ,
    @FirstDayOfLastMonth AS [DATE (Server default)] ,
    CONVERT(VARCHAR, @FirstDayOfLastMonth, 127) AS [ISO8601] ,
    CONVERT(VARCHAR, @FirstDayOfLastMonth, 103) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [British&French] ,
    CONVERT(VARCHAR, @FirstDayOfLastMonth, 104) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [German] ,
    CONVERT(VARCHAR, @FirstDayOfLastMonth, 105) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [Italian] ,
    CONVERT(VARCHAR, @FirstDayOfLastMonth, 111) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [Japan] ,
    CONVERT(VARCHAR, @FirstDayOfLastMonth, 100) AS [U.S.] ,
    CONVERT(VARCHAR, @FirstDayOfLastMonth, 120) AS [ODBC]
UNION
SELECT  
    'j) LastDayOfLastMonth.' AS [Title] ,
    @LastDayOfLastMonth AS [DATE (Server default)] ,
    CONVERT(VARCHAR, @LastDayOfLastMonth, 127) AS [ISO8601] ,
    CONVERT(VARCHAR, @LastDayOfLastMonth, 103) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [British&French] ,
    CONVERT(VARCHAR, @LastDayOfLastMonth, 104) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [German] ,
    CONVERT(VARCHAR, @LastDayOfLastMonth, 105) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [Italian] ,
    CONVERT(VARCHAR, @LastDayOfLastMonth, 111) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [Japan] ,
    CONVERT(VARCHAR, @LastDayOfLastMonth, 100) AS [U.S.] ,
    CONVERT(VARCHAR, @LastDayOfLastMonth, 120) AS [ODBC]
UNION
SELECT  
    'k) FirstDayOfNextMonth.' AS [Title] ,
    @FirstDayOfNextMonth AS [DATE (Server default)] ,
    CONVERT(VARCHAR, @FirstDayOfNextMonth, 127) AS [ISO8601] ,
    CONVERT(VARCHAR, @FirstDayOfNextMonth, 103) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [British&French] ,
    CONVERT(VARCHAR, @FirstDayOfNextMonth, 104) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [German] ,
    CONVERT(VARCHAR, @FirstDayOfNextMonth, 105) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [Italian] ,
    CONVERT(VARCHAR, @FirstDayOfNextMonth, 111) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [Japan] ,
    CONVERT(VARCHAR, @FirstDayOfNextMonth, 100) AS [U.S.] ,
    CONVERT(VARCHAR, @FirstDayOfNextMonth, 120) AS [ODBC]
UNION
SELECT  
    'l) LastDayOfNextMonth.' AS [Title] ,
    @LastDayOfNextMonth AS [DATE (Server default)] ,
    CONVERT(VARCHAR, @LastDayOfNextMonth, 127) AS [ISO8601] ,
    CONVERT(VARCHAR, @LastDayOfNextMonth, 103) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [British&French] ,
    CONVERT(VARCHAR, @LastDayOfNextMonth, 104) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [German] ,
    CONVERT(VARCHAR, @LastDayOfNextMonth, 105) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [Italian] ,
    CONVERT(VARCHAR, @LastDayOfNextMonth, 111) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [Japan] ,
    CONVERT(VARCHAR, @LastDayOfNextMonth, 100) AS [U.S.] ,
    CONVERT(VARCHAR, @LastDayOfNextMonth, 120) AS [ODBC]
UNION
SELECT  
    'm) FirstDayOfCurrentYear.' AS [Title] ,
    @FirstDayOfCurrentYear AS [DATE (Server default)] ,
    CONVERT(VARCHAR, @FirstDayOfCurrentYear, 127) AS [ISO8601] ,
    CONVERT(VARCHAR, @FirstDayOfCurrentYear, 103) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [British&French] ,
    CONVERT(VARCHAR, @FirstDayOfCurrentYear, 104) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [German] ,
    CONVERT(VARCHAR, @FirstDayOfCurrentYear, 105) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [Italian] ,
    CONVERT(VARCHAR, @FirstDayOfCurrentYear, 111) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [Japan] ,
    CONVERT(VARCHAR, @FirstDayOfCurrentYear, 100) AS [U.S.] ,
    CONVERT(VARCHAR, @FirstDayOfCurrentYear, 120) AS [ODBC]
UNION
SELECT  
    'n) LastDayOfCurrentYear.' AS [Title] ,
    @LastDayOfCurrentYear AS [DATE (Server default)] ,
    CONVERT(VARCHAR, @LastDayOfCurrentYear, 127) AS [ISO8601] ,
    CONVERT(VARCHAR, @LastDayOfCurrentYear, 103) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [British&French] ,
    CONVERT(VARCHAR, @LastDayOfCurrentYear, 104) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [German] ,
    CONVERT(VARCHAR, @LastDayOfCurrentYear, 105) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [Italian] ,
    CONVERT(VARCHAR, @LastDayOfCurrentYear, 111) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [Japan] ,
    CONVERT(VARCHAR, @LastDayOfCurrentYear, 100) AS [U.S.] ,
    CONVERT(VARCHAR, @LastDayOfCurrentYear, 120) AS [ODBC]
UNION
SELECT  
    'o) FirstDayOfLastYear.' AS [Title] ,
    @FirstDayOfLastYear AS [DATE (Server default)] ,
    CONVERT(VARCHAR, @FirstDayOfLastYear, 127) AS [ISO8601] ,
    CONVERT(VARCHAR, @FirstDayOfLastYear, 103) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [British&French] ,
    CONVERT(VARCHAR, @FirstDayOfLastYear, 104) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [German] ,
    CONVERT(VARCHAR, @FirstDayOfLastYear, 105) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [Italian] ,
    CONVERT(VARCHAR, @FirstDayOfLastYear, 111) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [Japan] ,
    CONVERT(VARCHAR, @FirstDayOfLastYear, 100) AS [U.S.] ,
    CONVERT(VARCHAR, @FirstDayOfLastYear, 120) AS [ODBC]
UNION
SELECT  
    'p) LastDayOfLastYear.' AS [Title] ,
    @LastDayOfLastYear AS [DATE (Server default)] ,
    CONVERT(VARCHAR, @LastDayOfLastYear, 127) AS [ISO8601] ,
    CONVERT(VARCHAR, @LastDayOfLastYear, 103) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [British&French] ,
    CONVERT(VARCHAR, @LastDayOfLastYear, 104) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [German] ,
    CONVERT(VARCHAR, @LastDayOfLastYear, 105) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [Italian] ,
    CONVERT(VARCHAR, @LastDayOfLastYear, 111) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [Japan] ,
    CONVERT(VARCHAR, @LastDayOfLastYear, 100) AS [U.S.] ,
    CONVERT(VARCHAR, @LastDayOfLastYear, 120) AS [ODBC]
UNION
SELECT  
    'q) FirstDayOfNextYear.' AS [Title] ,
    @FirstDayOfNextYear AS [DATE (Server default)] ,
    CONVERT(VARCHAR, @FirstDayOfNextYear, 127) AS [ISO8601] ,
    CONVERT(VARCHAR, @FirstDayOfNextYear, 103) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [British&French] ,
    CONVERT(VARCHAR, @FirstDayOfNextYear, 104) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [German] ,
    CONVERT(VARCHAR, @FirstDayOfNextYear, 105) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [Italian] ,
    CONVERT(VARCHAR, @FirstDayOfNextYear, 111) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [Japan] ,
    CONVERT(VARCHAR, @FirstDayOfNextYear, 100) AS [U.S.] ,
    CONVERT(VARCHAR, @FirstDayOfNextYear, 120) AS [ODBC]
UNION
SELECT  
    'r) LastDayOfNextYear.' AS [Title] ,
    @LastDayOfNextYear AS [DATE (Server default)] ,
    CONVERT(VARCHAR, @LastDayOfNextYear, 127) AS [ISO8601] ,
    CONVERT(VARCHAR, @LastDayOfNextYear, 103) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [British&French] ,
    CONVERT(VARCHAR, @LastDayOfNextYear, 104) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [German] ,
    CONVERT(VARCHAR, @LastDayOfNextYear, 105) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [Italian] ,
    CONVERT(VARCHAR, @LastDayOfNextYear, 111) + ' ' +  CONVERT(CHAR(5), @FirstDayOfCurrentWeek, 108) AS [Japan] ,
    CONVERT(VARCHAR, @LastDayOfNextYear, 100) AS [U.S.] ,
    CONVERT(VARCHAR, @LastDayOfNextYear, 120) AS [ODBC];

运行以上代码以生成以下输出:

enter image description here

如果你的国家遗失,那是因为我不知道它的代码。如果您能够编辑此答案并为您所在的国家/地区添加新列,那将是最有帮助和最受欢迎的。

提前致谢。

注意: 请记住,今天的日期是 2016年9月12日

<强>参考

如需进一步阅读ISO8601国际日期标准,请点击以下链接:

有关ODBC国际日期标准的进一步阅读,请点击以下链接:

要查看我工作的日期格式列表,请点击以下链接:

有关DATETIME数据类型的进一步阅读,请点击以下链接:

答案 4 :(得分:3)

SELECT DATEADD(m,DATEDIFF(m,0,GETDATE())-1,0) AS PreviousMonthStart

SELECT DATEADD(ms,-2,DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0)) AS PreviousMonthEnd

答案 5 :(得分:1)

从SQL2012开始,引入了一个名为EOMONTH的新功能。使用此功能可以轻松找到该月的第一天和最后一天。

select DATEADD(DD,1,EOMONTH(Getdate(),-1)) firstdayofmonth, EOMONTH(Getdate()) lastdayofmonth

答案 6 :(得分:1)

我还没有看到这个解决方案;这是我更喜欢它的简单可读性:

select dateadd(month,-1,format(getutcdate(),'yyyy-MM-01'))

答案 7 :(得分:1)

要获取上个月的第一个日期:

select DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-1, 0) LastMonthFirstDate                                                                

要获取上个月的最后日期:

select DATEADD(MONTH, DATEDIFF(MONTH, -1, GETDATE())-1, -1) LastMonthEndDate

答案 8 :(得分:0)

我在SSRS报告中使用了以下逻辑。

BUS_DATE = 17-09-2013

X=DATEADD(MONTH,-1,BUS_DATE) = 17-08-2013

Y=DAY(BUS_DATE)=17

first_date = DATEADD(DAY,-Y+1,X)=01-08-2013

last_date  = DATEADD(DAY,-Y,BUS_DATE)=31-08-2013

答案 9 :(得分:0)

取一些基准日期,即某个月的第31天,例如&#39; 20011231&#39 ;.然后使用
以下程序(我在下面给出了3个相同的例子,只有@dt值不同)。

declare @dt datetime;

set @dt = '20140312'

SELECT DATEADD(month, DATEDIFF(month, '20011231', @dt), '20011231');



set @dt = '20140208'

SELECT DATEADD(month, DATEDIFF(month, '20011231', @dt), '20011231');



set @dt = '20140405'

SELECT DATEADD(month, DATEDIFF(month, '20011231', @dt), '20011231');

答案 10 :(得分:0)

这是一种相当直接和动态的方式。对于上个月的最后一天,从今天的日期减去当前数字日。对于上个月的第一天,请使用相同的代码,只需重复从先前结果​​中减去数字日,然后添加1.

declare @PriorEOM as datetime
declare @PriorFOM as datetime

Set @PriorEOM = dateadd(day,-day(getdate()),getdate())

Set @PriorFOM = dateadd(day,-day(@PriorEOM) + 1,@PriorEOM)

Print @PriorEOM
Print @PriorFOM

答案 11 :(得分:0)

这是我知道上个月第一天的最简单方法:

第1步:将数据移至Postgres

第2步:

select date_trunc('month', getdate()) - '1 MONTH'::interval;

答案 12 :(得分:-1)

您可以在SQL Server中获取上个月的第一天和最后一天(带时间戳) 通过执行

--select dateadd(dd,-datepart(dd,getdate())+1,dateadd(mm,-1,getdate())) --first day of previous month 
--select dateadd(dd,-datepart(dd,getdate()),getdate()) -- last day of previous month**