[英]for every row get all dates between columns and insert into column
I have table variable that I fill with all dates between a particular range (and the dw,wk,mm,yy,dd values) 我有表变量,我用特定范围(和dw,wk,mm,yy,dd值)之间的所有日期填充
I then join this to my inspection records by the start date. 然后,我在开始日期之前将其加入我的检查记录。
SELECT CalDate
, DayN
, WeekN
, YearN
, DayOfMonthN
, [Start Date]
, [End Date]
, [Inspection Number]
FROM @Calendar AS dateInfo
LEFT JOIN [Inspection Records] AS IR
ON IR.[Start Date] = dateInfo.CalendarDate
This works fine, what I am now needing to do (and i have to do it this way to pass to a 3rd party application...) is get all the dates between the [Start Date] and [End Date] and add them into a column as a comma separated list. 效果很好,我现在需要做的(我必须通过这种方式传递给第三方应用程序...)是获取[开始日期]和[结束日期]之间的所有日期并添加它们列为逗号分隔的列表。
I've come across this answer t-sql get all dates between 2 dates that seems to get the dates perfectly for what I need. 我遇到过这个答案T-SQL获取2个日期之间的所有日期 ,似乎完全可以满足我的需要。
I'm stuck on how to structure my script to use that script to get all the dates between my [Start Date] and [End Date] and add them into a column. 我坚持如何构造脚本以使用该脚本获取[开始日期]和[结束日期]之间的所有日期并将它们添加到列中。
NOTE: I can not add anything to the database. 注意:我无法将任何内容添加到数据库。
Example: (Trying to get the dates column) 示例:(尝试获取日期列)
<table> <tr> <td>CalDate</td> <td>DayN</td> <td>WeekN</td> <td>YearN</td> <td>DayOfMonthN</td> <td>Start Date</td> <td>End Date</td> <td>Inspection Number</td> <td>Dates</td> </tr> <tr> <td>07-08-2014</td> <td>5</td> <td>32</td> <td>2014</td> <td>7</td> <td>07-08-2014</td> <td>11-08-2014</td> <td>A0001</td> <td>07-08-2014,08-08-2014,09-08-2014,10-08-2014,11-08-2014</td> </tr> </table>
SELECT CalDate
, DayN
, WeekN
, YearN
, DayOfMonthN
, [Start Date]
, [End Date]
, [Inspection Number]
, [Dates] = STUFF((
SELECT ',' + CONVERT(VARCHAR(10), C.CalDate,105)
FROM @Calendar AS C
WHERE C.CalDate BETWEEN IR.[Start Date] AND IR.[End Date]
FOR XML PATH('')
), 1, 1, '')
FROM @Calendar AS dateInfo
LEFT JOIN [Inspection Records] AS IR
ON IR.[Start Date] = dateInfo.CalendarDate
This will put all your dates into a variable named @csv. 这会将您的所有日期放入名为@csv的变量中。 I'm sure you'll be able to modify to suit your needs....... 我相信您将能够进行修改以满足您的需求。
DECLARE @csv nVarchar(max)
SELECT @csv = COALESCE(@csv + ', ', '') + CONVERT(Nvarchar(20), yourDateColumn)
FROM yourDateTable
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.