dbTalk Databases Forums  

Months Between

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Months Between in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
mets19@gmail.com
 
Posts: n/a

Default Months Between - 01-23-2008 , 01:06 PM






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.

Reply With Quote
  #2  
Old   
Plamen Ratchev
 
Posts: n/a

Default Re: Months Between - 01-23-2008 , 02:09 PM






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


Reply With Quote
  #3  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: Months Between - 01-23-2008 , 05:25 PM



Plamen Ratchev (Plamen (AT) SQLStudio (DOT) com) writes:
Quote:
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, esquel (AT) sommarskog (DOT) 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


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.