user1007103
user1007103

Reputation: 425

add +- 1 year in SQL Server

I try to select the Products that has a yearmodel between +1 and -1 the current year. And I only want the Year (ex 2011) not the full date and time.

SELECT ProductName FROM tblProduct WHERE Year BETWEEN 
year(getdate()+1) AND year(getdate()-1)

Does not work, but something similar maybe...

Upvotes: 18

Views: 65951

Answers (2)

patjuh1978
patjuh1978

Reputation: 211

SELECT dateadd(year, -1, getdate())

Would have done:

SELECT productname 
FROM tblproduct 
WHERE [year] between (select dateadd(year, -1, getdate()) AND (select dateadd(year, +1, getdate())

Upvotes: 21

Sebastian Piu
Sebastian Piu

Reputation: 8008

You are adding the 1 to getdate() so you are adding 1 day

   SELECT ProductName FROM tblProduct WHERE Year BETWEEN 
    (year(getdate()) -1) AND (year(getdate()) + 1)

Upvotes: 21

Related Questions