Question

  Asked: Apr 1 2008   6:38 AM GMT
  Asked by: Barend IT


DATETIME calculation


SQL, DateTime, Development

Hi there, this is probably very simple but i just can't get it working. I want to subtract one month from a variable month. So if the datetime variable is equal to 10 Feb 2008 the result should be 10 Jan 2008. Please help. Thanks

Subscribe to Alerts! Get questions and answers delivered to your Inbox.


E-mail me updates on this question



   SUBSCRIBE

hidden modal window

Answer Wiki (Improve, edit or add to this answer)


 RATE THIS ANSWER
0
Click to Vote:
  •   0
  •  0



You did not mention which database system you're using, and this is an important item of information, as all of them seem to have a different set of date functions.

The standard SQL way of doing it is with [i]intervals[/i] --

SELECT foo FROM bar WHERE datecolumn = CURRENT_DATE - INTERVAL 1 MONTH

MySQL supports this, as do several others.

In Microsoft SQL Server, you have to use the proprietary GETDATE and DATEADD functions --

SELECT foo FROM bar WHERE datecolumn = DATEADD(MONTH,-1,GETDATE())
  • AddThis Social Bookmark Button

Browse more Questions and Answers on Development.

Looking for relevant Development Whitepapers? Visit the SearchWinDevelopment.com Research Library.


Discuss This Answer


You must be logged-in to discuss a question. Log-in/Register