0

I have 882 rows in my Table the columns are (StartDate, EndDate, Capacity, PitchType_Skey, Site_Skey) and these rows are based on 2010 dates, what I would like to do is insert another 882 rows for the year 2011, but I cannot figure how to do this, can you help. I have started my Insert Statement:

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

Thanks in Advance

Wayne

Mahmoud Gamal
  • 78,257
  • 17
  • 139
  • 164
wafw1971
  • 361
  • 3
  • 7
  • 17
  • Oh and whilst there is a `Year()` function to get the year part from a datetime, it's more performant to a range check with `BETWEEN`. – Bridge Feb 14 '13 at 08:33

2 Answers2

3

Assuming that the StartDate and EndDate are of data type date or date time, then you can use the DATEADD function to add increment the year by 1 like this:

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;

Note that: As noted by @Bridge, @brykneval the YEAR function is not sargable, therefore the the SQL Server optimizer can't use an index on that field inside the YEAR function. For better performance, use the predicate BETWEEN, or <=, >= instead.

Community
  • 1
  • 1
Mahmoud Gamal
  • 78,257
  • 17
  • 139
  • 164
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
DevelopmentIsMyPassion
  • 3,541
  • 4
  • 34
  • 60