splitting a datetime column into year, month and week

前端 未结 3 1832
时光说笑
时光说笑 2021-01-12 07:24

I want to split a datetime column so that the year and the month both have their own column in a select statement output. I also want to have a column by week of the year, a

3条回答
  •  天涯浪人
    2021-01-12 08:22

    Try using the DatePart function as shown in the following:

    select
      datepart(year,Mydate), 
      datepart(month,Mydate),
      datepart(week,Mydate)
    From
      MyTable
    

    Note: If you need to calculate the week number by ISO 8601 standards then you'll need to use datepart(iso_week,Mydate)

    You could also look at the DateName function

    select
      datename(month,Mydate)
    From
      MyTable
    

提交回复
热议问题