SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Help with Datediff
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

jmthome
Starting Member

USA
2 Posts

Posted - 02/10/2013 :  20:15:18  Show Profile  Reply with Quote
Hi I want to use datediff/diffdate, whatever it's called. I don't know much about SQL programming.

Basically I need help with the code. I have this so far:

SELECT DATEDIFF(day,'lease_start_date','lease_end_date') AS DiffDate;

Help.

I have two fields called lease_start_date and lease_end_date in my form and they are connected to the database. I need a new field, lease_length to calculate the date difference in months and days.

James K
Flowing Fount of Yak Knowledge

3761 Posts

Posted - 02/10/2013 :  21:40:40  Show Profile  Reply with Quote
Use the query without the single quotes to get the difference in number of days:
SELECT DATEDIFF(day,lease_start_date,lease_end_date) AS DiffDate
FROM TheTable
Now, if you want to turn that into months and days, how you would do that depends on the rules that you want to use. For example, if you make a simple rule that 30 days = 1 month, then you can calculate the number of months and days as follows:
SELECT 
	DATEDIFF(day,lease_start_date,lease_end_date)/30 AS Months,
	DATEDIFF(day,lease_start_date,lease_end_date)%30 AS Days
FROM TheTable
Perhaps that is not what you want, you want to consider calendar months?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 02/11/2013 :  00:31:00  Show Profile  Reply with Quote
also see

http://www.sqlteam.com/article/datediff-function-demystified

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

jmthome
Starting Member

USA
2 Posts

Posted - 02/11/2013 :  02:20:39  Show Profile  Reply with Quote
quote:
Originally posted by James K

Use the query without the single quotes to get the difference in number of days:
SELECT DATEDIFF(day,lease_start_date,lease_end_date) AS DiffDate
FROM TheTable
Now, if you want to turn that into months and days, how you would do that depends on the rules that you want to use. For example, if you make a simple rule that 30 days = 1 month, then you can calculate the number of months and days as follows:
SELECT 
	DATEDIFF(day,lease_start_date,lease_end_date)/30 AS Months,
	DATEDIFF(day,lease_start_date,lease_end_date)%30 AS Days
FROM TheTable
Perhaps that is not what you want, you want to consider calendar months?




hi James, thank you, I want to do calendar months. So January 1 2012 to July 1 2012 would be 6 months 0 days.

What do I put further down in the text box I created to call this function?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 02/11/2013 :  03:20:14  Show Profile  Reply with Quote
for months it should be

SELECT 
	DATEDIFF(month,lease_start_date,lease_end_date) AS MonthsElapsed
FROM TheTable


if you want days elapsed also use James earlier suggestion

didnt understand what you mean by
What do I put further down in the text box I created to call this function?
you just need to bring the resultset in your page and call the field by its name (Months in earlier query or MonthsElapsed in mine)

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3761 Posts

Posted - 02/11/2013 :  09:12:32  Show Profile  Reply with Quote
The example you provided - Jan 1 to July 1 is rather straightforward - 6 months 0 days is the obvious answer. But what would you like to get in the two examples below?
Jan 25,2012 to July 5, 2012  -- perhaps 5 months and 6 days?
Jan 10,2012 to July 20, 2012 -- Should it be 5 months and 41 days, or 6 months and 11 days, 
                             -- or something else?
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3761 Posts

Posted - 02/11/2013 :  09:13:20  Show Profile  Reply with Quote
The example you provided - Jan 1 to July 1 is rather straightforward - 6 months 0 days is the obvious answer. But what would you like to get in the two examples below?
Jan 25,2012 to July 5, 2012  -- perhaps 5 months and 6 days?
Jan 10,2012 to July 20, 2012 -- Should it be 5 months and 41 days, or 6 months and 11 days, 
                             -- or something else?
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000