获取去年一年的年月日的SQL

1)获取去年一年的年月日的SQL:
SELECT
    CONVERT (
     VARCHAR (7),
     dateadd(mm ,- 12, getdate()),
     120
    ) sj,YEAR(dateadd(mm ,- 12, getdate())) year,MONTH(dateadd(mm ,- 12, getdate())) month
   UNION ALL
    SELECT
     CONVERT (
      VARCHAR (7),
      dateadd(mm ,- 11, getdate()),
      120
     ) sj,YEAR(dateadd(mm ,- 11, getdate())) year,MONTH(dateadd(mm ,- 11, getdate())) month
    UNION ALL
     SELECT
      CONVERT (
       VARCHAR (7),
       dateadd(mm ,- 10, getdate()),
       120
      ) sj,YEAR(dateadd(mm ,- 10, getdate())) year,MONTH(dateadd(mm ,- 10, getdate())) month
     UNION ALL
      SELECT
       CONVERT (
        VARCHAR (7),
        dateadd(mm ,- 9, getdate()),
        120
       ) sj,YEAR(dateadd(mm ,- 9, getdate())) year,MONTH(dateadd(mm ,- 9, getdate())) month
      UNION ALL
       SELECT
        CONVERT (
         VARCHAR (7),
         dateadd(mm ,- 8, getdate()),
         120
        ) sj,YEAR(dateadd(mm ,- 8, getdate())) year,MONTH(dateadd(mm ,- 8, getdate())) month
       UNION ALL
        SELECT
         CONVERT (
          VARCHAR (7),
          dateadd(mm ,- 7, getdate()),
          120
         ) sj,YEAR(dateadd(mm ,- 7, getdate())) year,MONTH(dateadd(mm ,- 7, getdate())) month
        UNION ALL
         SELECT
          CONVERT (
           VARCHAR (7),
           dateadd(mm ,- 6, getdate()),
           120
          ) sj,YEAR(dateadd(mm ,- 6, getdate())) year,MONTH(dateadd(mm ,- 6, getdate())) month
         UNION ALL
          SELECT
           CONVERT (
            VARCHAR (7),
            dateadd(mm ,- 5, getdate()),
            120
           ) sj,YEAR(dateadd(mm ,- 5, getdate())) year,MONTH(dateadd(mm ,- 5, getdate())) month
          UNION ALL
           SELECT
            CONVERT (
             VARCHAR (7),
             dateadd(mm ,- 4, getdate()),
             120
            ) sj,YEAR(dateadd(mm ,- 4, getdate())) year,MONTH(dateadd(mm ,- 4, getdate())) month
           UNION ALL
            SELECT
             CONVERT (
              VARCHAR (7),
              dateadd(mm ,- 3, getdate()),
              120
             ) sj,YEAR(dateadd(mm ,- 3, getdate())) year,MONTH(dateadd(mm ,- 3, getdate())) month
            UNION ALL
             SELECT
              CONVERT (
               VARCHAR (7),
               dateadd(mm ,- 2, getdate()),
               120
              ) sj,YEAR(dateadd(mm ,- 2, getdate())) year,MONTH(dateadd(mm ,- 2, getdate())) month
             UNION ALL
              SELECT
               CONVERT (
                VARCHAR (7),
                dateadd(mm ,- 1, getdate()),
                120
               ) sj,YEAR(dateadd(mm ,- 1, getdate())) year,MONTH(dateadd(mm ,- 1, getdate())) month

你可能感兴趣的:(sqlserver)