SQL 服务器 - 在我的动态 @VARIABLE 之后添加“%”

SQL Server - ADD '%' after my dynamic @VARIABLE

我的任务是想出一种翻译以下数据的方法: 来自

2017-04-01 12:16:58.5080000
2017-04-02 18:11:53.3090000
2017-04-02 18:34:18.3090000
2017-04-02 20:42:28.8570000
2017-04-02 21:10:36.7070000
2016-11-16 10:37:57.5970000
2016-11-16 10:38:07.7850000
2016-11-16 10:38:55.7010000
2016-11-16 10:48:19.0390000
2016-11-16 10:48:19.2990000
2016-11-16 10:48:29.3030000
2016-11-16 11:09:04.7960000
2016-11-16 11:15:08.1390000
2016-11-16 11:15:08.4190000
2016-11-16 11:14:27.4950000
.
.
.

'2017-03%', '2017-02%', '2017-01%', '2016-12%', '2016-11%', '2016-10%'

I have already written a code, where I get almost everything corect. But i don't know how to "inject" the percent % after the '2016-12' .

DECLARE @DatesFor NVARCHAR(MAX) = ''

SELECT
    @DatesFor +=
    QUOTENAME(LEFT(CONVERT(varchar, dateadd(MONTH, datediff(MONTH, 0, [OccuredAtUtc])-1 , 0),120),7), '''') + ', '
FROM [DATABASE]
GROUP BY dateadd(mm, datediff(mm, 0, [OccuredAtUtc])-1 , 0)
ORDER BY dateadd(mm, datediff(mm, 0, [OccuredAtUtc])-1 , 0) DESC
SET @DatesFor = LEFT(@DatesFor, LEN(@DatesFor)- 1) -- delete the last comma
PRINT @DatesFor

I get data without the % after month-->

'2017-03', '2017-02', '2017-01', '2016-12', '2016-11', '2016-10', '2015-01' 

有什么办法,如何解决这个问题? #IBelieveThereIsAlwaysAWay

我需要一个输出保留在@DatesFor 变量中,因为在此之后我将能够轻松地将结果从“2017-03%”修改为“[OccuredAtUtc” ] LIKE '2017-03%' OR" 并在我的动态数据透视查询中使用它

Declare @YourTable table (OccuredAtUtc datetime2)
Insert Into @YourTable values
('2017-04-01 12:16:58.5080000'),
('2017-04-02 18:11:53.3090000'),
('2017-04-02 18:34:18.3090000'),
('2017-04-02 20:42:28.8570000'),
('2017-04-02 21:10:36.7070000'),
('2016-11-16 10:37:57.5970000'),
('2016-11-16 10:38:07.7850000'),
('2016-11-16 10:38:55.7010000'),
('2016-11-16 10:48:19.0390000'),
('2016-11-16 10:48:19.2990000'),
('2016-11-16 10:48:29.3030000'),
('2016-11-16 11:09:04.7960000'),
('2016-11-16 11:15:08.1390000'),
('2016-11-16 11:15:08.4190000'),
('2016-11-16 11:14:27.4950000')

DECLARE @DatesFor NVARCHAR(MAX) = '>>>'

Select @DatesFor = replace(@DatesFor+concat('OR [OccuredAtUtc] LIKE ''',S,'%'' '),'>>>OR ','')
 From (
        Select Distinct Top 100 Percent S = Format(OccuredAtUtc,'yyyy-MM') From @YourTable Order By 1
      ) A

Returns

 [OccuredAtUtc] LIKE '2016-11%' OR [OccuredAtUtc] LIKE '2017-04%'