SQL SERVER 时间显示

--当前时间为2020年5月12日
Select convert(varchar(100),getdate(),0);       --月 日  4位年 时:分AM/PM           (如:05 12 2020 10:50AM)
Select convert(varchar(100),getdate(),1);       --月/日/2位年                      (如:05/12/20)
Select convert(varchar(100),getdate(),2);       --2位年.月.日                       (如:20.05.12)
Select convert(varchar(100),getdate(),3);       --日/月/2位年                      (如:12/05/20)
Select convert(varchar(100),getdate(),4);       --日.月.2位年                      (如:12.05.20)
Select convert(varchar(100),getdate(),5);       --日-月-2位年                      (如:12-05-20)
Select convert(varchar(100),getdate(),6);       --月 日 2位年                      (如:12 05 20)
Select convert(varchar(100),getdate(),7);       --月 日,2位年                      (如:05 12, 20)
Select convert(varchar(100),getdate(),8);       --时:分:秒                         (如:11:20:06)
Select convert(varchar(100),getdate(),9);       --月 日4位年 时:分:秒:毫秒AM/PM    (如:05 12 2020 11:23:36:763AM)
Select convert(varchar(100),getdate(),10);      --月-日-2位年                         (如:05-12-20)             
Select convert(varchar(100),getdate(),11);      --2位年/月/日                      (如:20/05/12)
Select convert(varchar(100),getdate(),12);      --2位年月日                        (如:200512)
Select convert(varchar(100),getdate(),13);      --日月4位年 时:分:秒:毫秒         (如:12 05 2020 11:38:32:920)
Select convert(varchar(100),getdate(),14);      --时:分:秒:毫秒                    (如:11:39:51:107)
Select convert(varchar(100),getdate(),20);      -- 4位年-月-日 时:分:秒            (如:2020-05-12 11:39:51)
Select convert(varchar(100),getdate(),21);      -- 4位年-月-日 时:分:秒:毫秒       (如:2020-05-12 11:39:51.107)
Select convert(varchar(100),getdate(),22);      --月/日/2位年 时:分:秒AM/PM        (如:05/12/20 11:39:51 AM)
Select convert(varchar(100),getdate(),23);      --4位年-月-日                      (如:2020-05-12)
Select convert(varchar(100),getdate(),24);      --时:分:秒                         (如:11:46:00)
Select convert(varchar(100),getdate(),25);      --4位年-月-日 时:分:秒.毫秒        (如:2020-05-12 11:48:24.870)
Select convert(varchar(100),getdate(),100);     --月 日 4位年 时:分AM/PM           (如:05 12 2020 11:48AM)
Select convert(varchar(100),getdate(),101);     --月/日/4位年                      (如:05/12/2020)
Select convert(varchar(100),getdate(),102);     --4位年.月.日                      (如:2020.05.12)
Select convert(varchar(100),getdate(),103);     --月/日/4位年                      (如:12/05/2020)
Select convert(varchar(100),getdate(),104);     --日.月.4位年                      (如:12.05.2020)
Select convert(varchar(100),getdate(),105);     --日-月-4位年                      (如:12-05-2020)
Select convert(varchar(100),getdate(),106);     --日 月 4位年                      (如:12 05 2020)
Select convert(varchar(100),getdate(),107);     --月 日,4为年                      (如:05 12, 2020)
Select convert(varchar(100),getdate(),108);     --时;分;秒                         (如:11:55:34)
Select convert(varchar(100),getdate(),109);     --月 日 4位年 时;分;秒;毫秒AM/PM   (如:05 12 2020 11:55:34:033AM)
Select convert(varchar(100),getdate(),110);     --月-日-4位年                      (如:05-12-2020)
Select convert(varchar(100),getdate(),111);     --4位年/月/日                      (如:2020/05/12)
Select convert(varchar(100),getdate(),112);     --4位年月日                        (如:20200512)
Select convert(varchar(100),getdate(),113);     --日 月 4位年 时:分:秒:毫秒        (如:12 05 2020 11:59:10:587)
Select convert(varchar(100),getdate(),114);     --时:分:秒:毫秒                    (如:11:59:10:587)
Select convert(varchar(100),getdate(),120);     --4位年-月-日 时;分:秒             (如:2020-05-12 11:59:10)
Select convert(varchar(100),getdate(),121);     --4位年-月-日 时:分:秒:毫秒(如:2020-05-12 11:59:10.587)
Select convert(varchar(100),getdate(),126);     --4位年-月-日T时:分:秒:毫秒(如:2020-05-12T11:59:10.587)
    DECLARE @b_yy DATETIME
          , @b_mm DATETIME
          , @e_mm DATETIME
          , @mm   VARCHAR(6)
          , @yy   VARCHAR(4);
    DECLARE @h_mm DATETIME    --环比月月初
          , @h_yy DATETIME    --环比月年初
          , @hm   VARCHAR(6); --环比月
    DECLARE @b_ly  DATETIME
          , @b_lm  DATETIME
          , @b_llm DATETIME
          , @e_lm  DATETIME
          , @lm    VARCHAR(6)
          , @ly    VARCHAR(4);
    --赋值
    SET @b_yy = DATEADD(yy, DATEDIFF(yy, 0, GETDATE() - 1), 0); --年初第一天  
    SET @yy = CONVERT(CHAR(4), @b_yy, 112); --今年年份  
    SET @b_mm = DATEADD(mm, DATEDIFF(mm, 0, GETDATE() - 1), 0); --本月第一天  
    SET @e_mm = DATEADD(dd, DATEDIFF(dd, 0, GETDATE()), 0); --今天  
    SET @mm = CONVERT(CHAR(6), @b_mm, 112); --本月月份  

    SET @h_mm = DATEADD(mm, -1, @b_mm); --环比月初第一天
    SET @h_yy = DATEADD(yy, DATEDIFF(yy, 0, @h_mm), 0); --环比月年初第一天
    SET @hm = CONVERT(CHAR(6), @h_mm, 112); --环比月

    SET @b_ly = DATEADD(yy, -1, @b_yy); --同比年初第一天  
    SET @b_lm = DATEADD(yy, -1, @b_mm); --同比月第一天  
    SET @b_llm = DATEADD(mm, 1, DATEADD(yy, -1, @b_mm)); --同比月的下个月的第一天
    SET @e_lm = DATEADD(yy, -1, @e_mm); --同比月今天  
    SET @ly = CONVERT(CHAR(4), @b_ly, 112); --同比年  
    SET @lm = CONVERT(CHAR(6), @b_lm, 112); --同比月 

posted on 2020-05-12 14:18  好玩的MATLAB  阅读(1)  评论(0编辑  收藏  举报  来源

导航