I need to get last six month values from the database. Here is my Fiddle. I need to get the values dynamically. Now February. So I need August to Januar
For MYSQL: you may use date_add
:
SELECT * FROM ratepersqft
WHERE date < Now() and date > DATE_ADD(Now(), INTERVAL- 6 MONTH);
For SQL Server:, dateadd :
SELECT * FROM ratepersqft
WHERE date < Now() and date > DATEADD(Month, -6, Now());
http://www.sqlfiddle.com/#!2/1f8029/48
**Please wrap your date column name with backticks given it is a reserved key word. **
MYSQL update:
SQLFIDDLE DEMO
SELECT *
FROM ratepersqft
WHERE date_format(date,'%Y-%m') <
date_format(now(),'%Y-%m')
and date_format(date,'%Y-%m') >=
date_format(now() - interval 6 month,'%Y-%m')
order by date desc;