splitting a datetime column into year, month and week

前端 未结 3 1830
时光说笑
时光说笑 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:10

    check this

    select datepart(Month,DateColumn) Mnth,datename(Month,DateColumn) MnthName,datepart(Year,DateColumn) Year1,((day(DateColumn)-1) / 7) + 1 week from dbo.Table
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2021-01-12 08:25

    Here is another way. Use SQL Servers YEAR() and MONTH() functions. For week, I use datepart(week,Mydate) as noted by @DMK.

    SELECT YEAR(MyDate), MONTH(MyDate), DATEPART(WEEK,Mydate) From YourTable
    
    0 讨论(0)
提交回复
热议问题