我在一列中有日期&列中的年份,我需要连接并获得2017年1月的答案,这应该是在实际的月份oder
答案 0 :(得分:0)
SELECT CONCAT(column1, ',', column2) AS Date,
FROM table;
答案 1 :(得分:0)
这很简单。我假设您的两个列数据类型都是DateTime或Date。请尝试以下方法:
SELECT CONCAT(SUBSTRING(DATENAME(month, YourMonthColumn), 0,4), '-', Year(YourYearColumn))
FROM YourTableName
答案 2 :(得分:0)
兼容Sql Server 2008 :(无concat()
且无format()
)
使用convert()
样式106(或113),只需取mon yyyy
部分,并将' '
替换为'-'
,
使用截断到月份的日期,我们可以使用dateadd(month, datediff(month, 0, [Date] ),0)
select MonthYear = replace(stuff(convert(varchar(12)
,dateadd(month, datediff(month, 0, [Date]), 0)
,106),1,3,''),' ','-')
, Days = count(*)
from dates
group by dateadd(month, datediff(month, 0, [Date]), 0)
order by dateadd(month, datediff(month, 0, [Date]), 0)
rextester演示:http://rextester.com/DFF74396
返回:
+-----------+------+
| MonthYear | Days |
+-----------+------+
| Dec-2016 | 31 |
| Jan-2017 | 31 |
| Feb-2017 | 28 |
| Mar-2017 | 10 |
+-----------+------+
使用datename(month,date)
代替月份并连接年份:
select MonthYear = left(datename(month,dateadd(month, datediff(month, 0, [Date]), 0)),3)
+ '-'
+ convert(char(4),year(dateadd(month, datediff(month, 0, [Date]), 0)))
, Days = count(*)
from dates
group by dateadd(month, datediff(month, 0, [Date]), 0)
order by dateadd(month, datediff(month, 0, [Date]), 0)