Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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...

like image 516
user1007103 Avatar asked Dec 16 '11 12:12

user1007103


People also ask

How do I add 12 months to a date in SQL?

ADD_MONTHS() function returns a date with a given number of months added (date plus integer months). A month is defined by the session parameter NLS_CALENDAR. A datetime value or any value that can be implicitly converted to DATE. An integer or any value that can be implicitly converted to an integer.

How do I add a financial year in SQL?

This is a way to get current financial year using SQL query: DECLARE @FIYear VARCHAR(20) SELECT @FIYear = (CASE WHEN (MONTH(GETDATE())) <= 3 THEN convert(varchar(4), YEAR(GETDATE())-1) + '-' + convert(varchar(4), YEAR(GETDATE())%100)

How do I get next year in SQL?

You can use dateadd function. dateadd(year,1,thedate).


1 Answers

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)
like image 99
Sebastian Piu Avatar answered Sep 21 '22 03:09

Sebastian Piu