Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
485 views
in Technique[技术] by (71.8m points)

sql - 从SQL Server 2005中的日期时间获取月份和年份(Get month and year from a datetime in SQL Server 2005)

I need the month+year from the datetime in SQL Server like 'Jan 2008'.

(我需要SQL Server中日期时间的月份+年份,如'2008年1月'。)

I'm grouping the query by month, year.

(我按月,年分组查询。)

I've searched and found functions like datepart, convert, etc., but none of them seem useful for this.

(我搜索并找到了像datepart,convert等函数,但它们似乎都没有用。)

Am I missing something here?

(我在这里错过了什么吗?)

Is there a function for this?

(这有功能吗?)

  ask by Malik Daud Ahmad Khokhar translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
select 
datepart(month,getdate()) -- integer (1,2,3...)
,datepart(year,getdate()) -- integer
,datename(month,getdate()) -- string ('September',...)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...