This week's book giveaway is in the Beginning Java forum.
We're giving away four copies of Get Programming with Java (MEAP only) and have Peggy Fisher on-line!
See this thread for details.
Win a copy of Get Programming with Java (MEAP only) this week in the Beginning Java 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

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
 
Rancher
Posts: 3812
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: 23977
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;
 
Let nothing stop you! Not even this tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!