Get month and year from a datetime in SQL Server 2005

后端 未结 20 1453
[愿得一人]
[愿得一人] 2020-12-12 15:39

I need the month+year from the datetime in SQL Server like \'Jan 2008\'. I\'m grouping the query by month, year. I\'ve searched and found functions like datepart, convert, e

相关标签:
20条回答
  • 2020-12-12 16:23

    the best way to do that is with :

    dateadd(month,datediff(month,0,*your_date*),0)
    

    it will keep your datetime type

    0 讨论(0)
  • 2020-12-12 16:23

    It's work great.

    DECLARE @pYear VARCHAR(4)
    
    DECLARE @pMonth VARCHAR(2)
    
    DECLARE @pDay VARCHAR(2)
    
    SET @pYear  = RIGHT(CONVERT(CHAR(10), GETDATE(), 101), 4)
    
    SET @pMonth = LEFT(CONVERT(CHAR(10), GETDATE(), 101), 2)
    
    SET @pDay   = SUBSTRING(CONVERT(CHAR(10), GETDATE(), 101), 4,2)
    
    SELECT @pYear,@pMonth,@pDay
    
    0 讨论(0)
  • 2020-12-12 16:24

    Use:

    select datepart(mm,getdate())  --to get month value
    select datename(mm,getdate())  --to get name of month
    
    0 讨论(0)
  • 2020-12-12 16:24
    ( Month(Created) + ',' + Year(Created) ) AS Date
    
    0 讨论(0)
  • 2020-12-12 16:24
      ,datename(month,(od.SHIP_DATE)) as MONTH_
    

    Answer: MONTH_ January January September October December October September

    0 讨论(0)
  • 2020-12-12 16:25

    returns the full month name, -, full year e.g. March-2017

    CONCAT(DATENAME(mm, GetDate()), '-', DATEPART(yy, GetDate()))
    
    0 讨论(0)
提交回复
热议问题