473,471 Members | 4,648 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Months Between

Hi, I am trying to determine the amount of months between today and a
date stored in the database. But I cannot seem to figure out get the
difference between the two dates. Thanks in advance.
Jan 23 '08 #1
2 2934
You can use the DATEDIFF function to calculate period of time between dates.
Here is example for months:

CREATE TABLE Foo (mydate DATETIME)

INSERT INTO Foo VALUES ('20010106')
INSERT INTO Foo VALUES ('20020506')
INSERT INTO Foo VALUES ('20070901')
INSERT INTO Foo VALUES ('20071201')
INSERT INTO Foo VALUES ('20080101')

SELECT DATEDIFF(month, mydate, CURRENT_TIMESTAMP)
FROM Foo

HTH,

Plamen Ratchev
http://www.SQLStudio.com

Jan 23 '08 #2
Plamen Ratchev (Pl****@SQLStudio.com) writes:
You can use the DATEDIFF function to calculate period of time between
dates.
Here is example for months:

CREATE TABLE Foo (mydate DATETIME)

INSERT INTO Foo VALUES ('20010106')
INSERT INTO Foo VALUES ('20020506')
INSERT INTO Foo VALUES ('20070901')
INSERT INTO Foo VALUES ('20071201')
INSERT INTO Foo VALUES ('20080101')

SELECT DATEDIFF(month, mydate, CURRENT_TIMESTAMP)
FROM Foo
For mets19 we should point out that datediff counts the number of cross
boundaries, so datediff(MONTH, '20080131', '200800201') returns 1, which
may or may not be what you want.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Jan 23 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Joseph S | last post by:
I am having problems with the following countdown script. For some reason, the number of months is occasionally wrong. I can't quite put my finger on the problem. Any assistance or...
2
by: lpeterson | last post by:
I'm trying to calculate the number of months between dates that begin in 2003 and end in 2004(ex. 1/2/2003 - 7/2/2004). I need the total months in 2003 and in 2004 both of which should add up to...
2
by: Mary | last post by:
I am trying to develop a query which will determine the average costs using a rolling average of the past 12 months of data. In other words, if I entered the Ship Month of January and the Ship...
3
by: MMFBprez | last post by:
I am trying to compute storage charges by getting the number of months between dates and multiplying it by a rate. I cannot get a correct number of months if the date is greater than a year ago. ...
3
by: jr | last post by:
A perplexing one this. I Am trying to design a query or series of queries which will firstly identify a condition. If column A value is less than column B value make column C value =1 , else...
6
by: carl.barrett | last post by:
Hi, I have a continuous form based on a query ( I will also be creating a report based on the same query). There are 2 fields: Date Obtained and Date Of Expiry I want a further 3 columns...
5
by: Juan | last post by:
Hi everyone, is there a function that calculates the exact amount of Years, Months, and Days between two days? TimeDiff is not good enough, since it calculates, for example: Date 1: Dec....
2
by: bufbec | last post by:
I have worked on this for hours and can't come up with a solution. Hope someone can help me. I have a table called TMBS_HMAUDIT_PARMS. this table contains data to tell me how often a person is...
5
by: RandyG | last post by:
how do you pull the current months data without having to manually input the date each time?
10
by: karunajo | last post by:
hi, I want to calculate tne months between two dates.Actually i am using Asp.net2.0.Is it possible to calculate using Sql or Asp,net..I want to list the months betweeen two dates.Help me...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.