得到一年所有天數SQLServer

來源:互聯網
上載者:User

if   exists   (select   *   from   dbo.sysobjects   where   id   =   object_id(N'[dbo].[time_by_day]')   and   OBJECTPROPERTY(id,   N'IsUserTable')   =   1)  
  drop   table   [dbo].[time_by_day]  
  GO  
   
  CREATE   TABLE   [dbo].[time_by_day]   (  
  [time_id]   [int]   IDENTITY   (1,   1)   NOT   NULL   ,  
  [the_date]   [datetime]   NULL   ,  
  [the_day]   [nvarchar]   (15)   COLLATE   Chinese_PRC_CI_AS   NULL   ,  
  [the_month]   [nvarchar]   (15)   COLLATE   Chinese_PRC_CI_AS   NULL   ,  
  [the_year]   [smallint]   NULL   ,  
  [day_of_month]   [smallint]   NULL   ,  
  [week_of_year]   [float]   NULL   ,  
  [month_of_year]   [smallint]   NULL   ,  
  [quarter]   [nvarchar]   (2)   COLLATE   Chinese_PRC_CI_AS   NULL   ,  
  [tenday_of_month]   [nvarchar]   (15)   COLLATE   Chinese_PRC_CI_AS   NULL   ,  
  [half_year]   [nvarchar]   (15)   COLLATE   Chinese_PRC_CI_AS   NULL   ,  
  )   ON   [PRIMARY]  
  GO  
   
   
   
  truncate   table   time_by_day  
  go  
  declare   @DateJ   int  
  declare   @CurDate     datetime  
  declare   @StartDate   datetime  
  declare   @EndDate   datetime  
  set   @StartDate='2005-5-1'   ---------這裡填寫起始時間  
  set   @EndDate='2050-12-31'   ---------這裡填寫結束時間  
  --SELECT   DATEDIFF(day,   @StartDate,   @EndDate)   into   @DateJ  
     
  set @CurDate=   @StartDate    
  while   @CurDate<@EndDate   BEGIN  
  insert   into   time_by_day   (the_date,the_day,the_month,the_year,day_of_month,  
  week_of_year,month_of_year,quarter,tenday_of_month,half_year)  
  values   (@CurDate,  
    case   when   DATEPART(weekday,   @CurDate)=1   then   '星期日'  
  when   DATEPART(weekday,   @CurDate)=2   then     '星期一'  
  when   DATEPART(weekday,   @CurDate)=3   then     '星期二'  
  when   DATEPART(weekday,   @CurDate)=4   then     '星期三'  
  when   DATEPART(weekday,   @CurDate)=5   then     '星期四'  
  when   DATEPART(weekday,   @CurDate)=6   then     '星期五'  
  when   DATEPART(weekday,   @CurDate)=7   then     '星期六'    
  end   ,  
    case   when     DATEPART(Month,   @CurDate)=1   then   '一月'  
  when   DATEPART(Month,   @CurDate)=2   then     '二月'  
  when   DATEPART(Month,   @CurDate)=3   then     '三月'  
  when   DATEPART(Month,   @CurDate)=4   then     '四月'  
  when   DATEPART(Month,   @CurDate)=5   then     '五月'  
  when   DATEPART(Month,   @CurDate)=6   then     '六月'  
  when   DATEPART(Month,   @CurDate)=7   then     '七月'  
  when   DATEPART(Month,   @CurDate)=8   then     '八月'  
  when   DATEPART(Month,   @CurDate)=9   then     '九月'  
  when   DATEPART(Month,   @CurDate)=10   then     '十月'  
  when   DATEPART(Month,   @CurDate)=11   then     '十一月'  
  when   DATEPART(Month,   @CurDate)=12   then     '十二月'  
  end   ,  
  DATENAME(year,   @CurDate)   ,  
  DATENAME(day,   @CurDate)   ,  
  DATENAME(week,   @CurDate),DATENAME(month,   @CurDate)   ,DATENAME(quarter,   @CurDate)  
  ,  
  case   when   DATEPART(day,   @CurDate)<=10   then   '上旬'  
            when   DATEPART(day,   @CurDate)<=20   and   DATEPART(day,   @CurDate)>10   then   '中旬'  
            when   DATEPART(day,   @CurDate)>20       then   '下旬'  
  end,  
  case   when   DATEPART(month,   @CurDate)<=6   then   '上半年'  
            when   DATEPART(month,   @CurDate)>6   then   '下半年'  
    end  
  )  
  --print     @CurDate  
          set @CurDate=DATEADD(day,1,@CurDate)  
  end

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.