我需要SQL Server中日期时间的月份+年份,如'2008年1月'。我按月,年分组查询。我搜索并找到了像datepart,convert等函数,但它们似乎都没有用。我在这里错过了什么吗?这有功能吗?
答案 0 :(得分:168)
select
datepart(month,getdate()) -- integer (1,2,3...)
,datepart(year,getdate()) -- integer
,datename(month,getdate()) -- string ('September',...)
答案 1 :(得分:71)
如果你的意思是你希望它们以字符串形式返回,那就是
SELECT
CONVERT(CHAR(4), date_of_birth, 100) + CONVERT(CHAR(4), date_of_birth, 120)
FROM customers
答案 2 :(得分:36)
从SQL Server 2012开始,您可以使用:
SELECT FORMAT(@date, 'yyyyMM')
答案 3 :(得分:11)
有趣的是,我只是在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
答案 4 :(得分:11)
使用:
select datepart(mm,getdate()) --to get month value
select datename(mm,getdate()) --to get name of month
答案 5 :(得分:8)
在SQL Server 2012中,可以使用以下
选择FORMAT(getdate(),' MMM yyyy')
这给出了确切的" 2016年6月"
答案 6 :(得分:6)
这个怎么样?
Select DateName( Month, getDate() ) + ' ' + DateName( Year, getDate() )
答案 7 :(得分:5)
( Month(Created) + ',' + Year(Created) ) AS Date
答案 8 :(得分:4)
该格式不存在。你需要做两件事的组合,
select convert(varchar(4),getdate(),100) + convert(varchar(4),year(getdate()))
答案 9 :(得分:4)
最好的方法是:
dateadd(month,datediff(month,0,*your_date*),0)
它将保留您的日期时间类型
答案 10 :(得分:2)
返回完整的月份名称, - ,全年,例如March-2017
CONCAT(DATENAME(mm, GetDate()), '-', DATEPART(yy, GetDate()))
答案 11 :(得分:1)
将日期转换为月份中的第一个允许您按单个属性分组依据和排序,并且我的体验更快。
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)
答案 12 :(得分:1)
问题是关于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
答案 13 :(得分:1)
cast(cast(sq.QuotaDate as date) as varchar(7))
给出“2006-04”格式
答案 14 :(得分:1)
---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
答案 15 :(得分:1)
我遇到了同样的问题,环顾四周后发现了这个问题:
SELECT DATENAME(yyyy, date) AS year
FROM Income
GROUP BY DATENAME(yyyy, date)
它运作得很好!
答案 16 :(得分:0)
function coinPowers(n, m) {
if (n < 1 || m < 1) return 0;
if (n < m || m == 1) return 1;
var power = Math.floor(Math.log(n) / Math.log(m));
var memo = [];
for (var i = 0; i < n; i++) memo[i] = [];
return partition(n, m, power);
function partition(n, m, power) {
var count = memo[n - 1][power];
if (count) return count;
var coin = Math.pow(m, power), count = 1;
for (var p = power; p > 0; coin /= m, p--) {
if (coin < n) count += partition(n - coin, m, p)
else if (coin == n) ++count;
}
return (memo[n - 1][power] = count);
}
}
document.write(coinPowers(200, 3) + "<BR>");
document.write(coinPowers(200, 2) + "<BR>");
document.write(coinPowers(10000, 2) + "<BR>");
答案: 月_ 一月 一月 九月 十月 十二月 十月 九月
答案 17 :(得分:0)
是的,您可以使用datename(month,intime)
获取文字中的月份。
答案 18 :(得分: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
答案 19 :(得分:-3)
以下作品完美无缺!我只是用它,试一试。
date_format(date,'%Y-%c')