Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

SQL Question  RSS feed

 
Ranch Hand
Posts: 213
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a way to create a SQL query that can find the current system date minus one year taking a leap year into account?
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SELECT DATE_SUB( CURDATE(),INTERVAL 1 YEAR);

DATE_SUB  for subtract date
CUR_DATE  get current date
 
Master Rancher
Posts: 3630
40
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Depends on the database, but they usually have an equivalent to the DATE_SUB that Joe mentions.
 
Sheriff
Posts: 23706
50
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And as for the leap-year issue (yes, that could be an important issue which needs to be addressed) -- you would have to read the database's documentation for that function. It should explain what it does with Feb 28/29 and that may vary between databases.
 
Fred Victa
Ranch Hand
Posts: 213
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These examples are for Oracle SQL:

--Subtract one year from current date while taking a leap year into account.
SELECT ADD_MONTHS(SYSDATE,-12) FROM DUAL;

--returns 31-MAR-07
select add_months('28-FEB-2007', 1) from dual;

--returns 28-MAR-08
select add_months('28-FEB-2008', 1) from dual;
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!