我现有的查询是:
SELECT @str = coalesce(@str + ']' + ', ', '') + '[' + monthyear
FROM (SELECT DISTINCT ( monthyear), ExpectedDate
FROM table) s
order by ExpectedDate
我需要按日期
按顺序添加唯一的monthyear
字符串
ExpectedDate monthyear
2015-11-01 Nov-15
2015-11-01 Nov-15
2015-11-01 Nov-15
2015-11-01 Nov-15
2015-11-01 Nov-15
2015-11-01 Nov-15
2015-11-01 Nov-15
2015-11-01 Nov-15
2015-11-01 Nov-15
2015-11-01 Nov-15
2015-12-01 Dec-15
2015-12-01 Dec-15
2015-12-01 Dec-15
2015-12-01 Dec-15
2015-12-01 Dec-15
2015-12-01 Dec-15
2015-12-01 Dec-15
2015-12-01 Dec-15
2015-12-02 Dec-15
2015-12-30 Dec-15
2016-01-01 Jan-16
2016-01-01 Jan-16
2016-01-01 Jan-16
2016-01-01 Jan-16
2016-01-01 Jan-16
2016-01-01 Jan-16
2016-01-01 Jan-16
2016-01-01 Jan-16
2016-01-01 Jan-16
2016-01-01 Jan-16
2016-01-01 Jan-16
2016-01-01 Jan-16
2016-01-01 Jan-16
2016-01-01 Jan-16
2016-01-04 Jan-16
2016-01-04 Jan-16
2016-01-04 Jan-16
2016-01-04 Jan-16
2016-02-01 Feb-16
2016-02-01 Feb-16
2016-02-01 Feb-16
2016-02-01 Feb-16
2016-02-01 Feb-16
2016-02-01 Feb-16
2016-02-01 Feb-16
2016-02-01 Feb-16
2016-02-01 Feb-16
2016-03-01 Mar-16
2016-03-01 Mar-16
2016-03-01 Mar-16
2016-03-01 Mar-16
2016-03-01 Mar-16
2016-03-01 Mar-16
2016-03-01 Mar-16
2016-04-01 Apr-16
2016-04-01 Apr-16
2016-04-01 Apr-16
2016-04-01 Apr-16
2016-04-01 Apr-16
2016-06-01 Jun-16
2016-06-01 Jun-16
2016-07-01 Jul-16
2016-10-01 Oct-16
2016-10-01 Oct-16
2016-12-01 Dec-16
2016-12-30 Dec-16
答案 0 :(得分:1)
使用XML PATH
DECLARE @t table(ExpectedDate date, monthdate char(8))
INSERT @t values
('2015-11-01', 'Nov-2015'),
('2015-11-01', 'Nov-2015'),
('2015-12-01', 'Dec-2015'),
('2015-12-01', 'Dec-2015'),
('2016-01-01', 'Jan-2016')
SELECT
STUFF((
SELECT ',[' + STUFF(monthdate,4,1, '') + ']'
FROM @t t1
GROUP BY ExpectedDate, monthdate
ORDER BY ExpectedDate
for xml path(''), type
).value('.', 'varchar(max)'), 1, 1, '') [values]
结果:
[Nov2015],[Dec2015],[Jan2016]
答案 1 :(得分:0)
尝试使用我MonthName()
提供的Year()
和MySQL
内置函数。
MonthName(ExpectedDate), Year(ExpectedDate)
Ex:Month(2016-02-01)
给你Februray
然后你可以结果。
答案 2 :(得分:0)
如果monthyear
列为varchar
类型,您可以尝试:
SELECT '[' + SUBSTRING(monthyear, 1, 3) + CAST(YEAR(PAY_DATE) AS VARCHAR) + ']'
FROM Your_Table
如果monthyear
列为date
或datetime
,请尝试以下操作:
SELECT '[' + SUBSTRING(DateName(month, DateAdd(month, MONTH(Pay_Date), 0) - 1 ), 1, 3)
+ CAST(YEAR(PAY_DATE) AS VARCHAR) + ']'
FROM Your_Table