GETDATE last month

后端 未结 8 1711
日久生厌
日久生厌 2020-12-06 19:18

I am trying to list last a website\'s statistics. I listed Last 30 days with;

CONVERT(VARCHAR(10), S.DATEENTERED, 101) 
  BETWEEN 
    CONVERT(VARCHAR(10),          


        
8条回答
  •  温柔的废话
    2020-12-06 19:55

    The following will find you the start of the last month:

    -- Start of last month 
    SELECT CAST('01 '+ RIGHT(CONVERT(CHAR(11),DATEADD(MONTH,-1,GETDATE()),113),8) AS datetime)
    

    You would then find the start of this month, using the following, minus one.

    -- Start of the month 
    SELECT CAST('01 '+ RIGHT(CONVERT(CHAR(11),GETDATE(),113),8) AS datetime) 
    

    When I have to work with dates in SQL Server I often reference Robyn Page's SQL Server DATE/TIME Workbench. The workbench (tutorial) is well laid out and contains just about everything I have ever needed when working with dates on SQL Server.

提交回复
热议问题