0

我的表中有 882 行,这些列是(StartDate、EndDate、Capacity、PitchType_Skey、Site_Skey),这些行基于 2010 年的日期,我想做的是在 2011 年再插入 882 行,但我想不出如何做到这一点,你能帮忙吗?我已经开始了我的插入语句:

INSERT INTO Cap1 (StartDate, EndDate, Capacity, PitchType_Skey, Site_Skey)

SELECT
Add 1 YEAR to StartDate,
Add 1 YEAR to EndDate,
Capacity,
Pitch_Type_Skey,
Site_Skey
FROM Cap1
WHERE YEAR StartDate = 2010

提前致谢

韦恩

4

2 回答 2

3

假设StartDateandEndDate的数据类型为日期或日期时间,那么您可以使用该DATEADD函数将年份增加 1,如下所示:

INSERT INTO Cap1 (StartDate, EndDate, Capacity, PitchType_Skey, Site_Skey)

SELECT
  DATEADD(yy, 1, StartDate),
  DATEADD(yy, 1, EndDate),
  Capacity,
  Pitch_Type_Skey,
  Site_Skey
FROM Cap1
WHERE YEAR(StartDate) = 2010;

请注意:正如@Bridge 所指出的,@brykneval 该YEAR函数不是sargable,因此 SQL Server 优化器不能在YEAR函数内的该字段上使用索引。为了获得更好的性能,请使用谓词BETWEEN, or <=,>=代替。

于 2013-02-14T08:32:28.840 回答
0
INSERT INTO Cap1 (StartDate, EndDate, Capacity, PitchType_Skey, Site_Skey)

                 SELECT
                     DateAdd(yy,1,StartDate),
                     DateAdd(yy, 1,EndDate),
                     Capacity,
                     Pitch_Type_Skey,
                     Site_Skey
                     FROM Cap1
                      WHERE YEAR(StartDate) = 2010
于 2013-02-14T08:34:54.987 回答