首页 文章

从SQL Server 2005中的日期时间获取月份和年份

提问于
浏览
75

我需要SQL Server中日期时间的月份,如'2008年1月' . 我按月,年分组查询 . 我搜索并找到了像datepart,convert等函数,但它们似乎都没有用 . 我在这里错过了什么吗?这有功能吗?

20 回答

  • 2

    如果你的意思是你希望它们以字符串形式返回,那就是那种格式;

    SELECT 
      CONVERT(CHAR(4), date_of_birth, 100) + CONVERT(CHAR(4), date_of_birth, 120) 
    FROM customers
    

    Here are the other format options

  • 4
    select 
    datepart(month,getdate()) -- integer (1,2,3...)
    ,datepart(year,getdate()) -- integer
    ,datename(month,getdate()) -- string ('September',...)
    
  • 11

    从SQL Server 2012开始,您可以使用:

    SELECT FORMAT(@date, 'yyyyMM')
    
  • 8

    使用:

    select datepart(mm,getdate())  --to get month value
    select datename(mm,getdate())  --to get name of month
    
  • 1

    有趣的是,我只是在SQL Server和LINQ中编写同样的查询 .

    SELECT 
        DATENAME(mm, article.Created) AS Month, 
        DATENAME(yyyy, article.Created) AS Year, 
        COUNT(*) AS Total 
    FROM Articles AS article 
    GROUP BY 
        DATENAME(mm, article.Created), 
        DATENAME(yyyy, article.Created) 
    ORDER BY Month, Year DESC
    

    它产生以下输出(例子) .

    Month | Year | Total
    
    January | 2009 | 2
    
  • 1

    在SQL Server 2012中,可以使用以下内容

    选择FORMAT(getdate(),'MMM yyyy')

    这给出了精确的“2016年6月”

  • 0
    ( Month(Created) + ',' + Year(Created) ) AS Date
    
  • 34

    这个怎么样?

    Select DateName( Month, getDate() ) + ' ' + DateName( Year, getDate() )
    
  • 69

    那种格式不存在 . 你需要做两件事的组合,

    select convert(varchar(4),getdate(),100)  + convert(varchar(4),year(getdate()))
    
  • 5

    最好的方法是:

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

    它会保留你的日期时间类型

  • 5

    返回完整的月份名称, - ,全年,例如 March-2017

    CONCAT(DATENAME(mm, GetDate()), '-', DATEPART(yy, GetDate()))
    
  • 166

    我有同样的问题,环顾四周后我发现了这个:

    SELECT DATENAME(yyyy, date) AS year
    FROM Income
    GROUP BY DATENAME(yyyy, date)
    

    它工作得很棒!

  • 0

    将日期转换为月中的第一个允许您按单个属性分组依据和排序,并且我的体验更快 .

    declare @mytable table(mydate datetime)
    declare @date datetime
    set @date = '19000101'
    while @date < getdate() begin
        insert into @mytable values(@date)
        set @date = dateadd(day,1,@date)
    end
    
    select count(*) total_records from @mytable
    
    select dateadd(month,datediff(month,0,mydate),0) first_of_the_month, count(*) cnt
    from @mytable
    group by dateadd(month,datediff(month,0,mydate),0)
    
  • 11
    ---Lalmuni Demos---
    create table Users
    (
    userid int,date_of_birth date
    )
    ---insert values---
    insert into Users values(4,'9/10/1991')
    
    select DATEDIFF(year,date_of_birth, getdate()) - (CASE WHEN (DATEADD(year, DATEDIFF(year,date_of_birth, getdate()),date_of_birth)) > getdate() THEN 1 ELSE 0 END) as Years, 
    MONTH(getdate() - (DATEADD(year, DATEDIFF(year, date_of_birth, getdate()), date_of_birth))) - 1 as Months, 
    DAY(getdate() - (DATEADD(year, DATEDIFF(year,date_of_birth, getdate()), date_of_birth))) - 1 as Days,
    from users
    
  • 4
    cast(cast(sq.QuotaDate as date) as varchar(7))
    

    给出“2006-04”格式

  • 1

    是的,您可以使用 datename(month,intime) 获取文本中的月份 .

  • -3

    问题是关于SQL Server 2005,这里的许多答案都是针对更高版本的SQL Server .

    select convert (varchar(7), getdate(),20)
    --Typical output 2015-04
    

    SQL Server 2005没有日期函数which was introduced in SQL Server 2008

  • 0
    ,datename(month,(od.SHIP_DATE)) as MONTH_
    

    答案:MONTH_ 1月1月9月10月12月10月9月

  • 0

    这很棒 .

    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
    
  • 1

    以下工作完美!我只是用它,试一试 .

    date_format(date,'%Y-%c')
    

相关问题