| Author |
Topic  |
|
|
jmthome
Starting Member
USA
2 Posts |
Posted - 02/10/2013 : 20:15:18
|
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
1483 Posts |
Posted - 02/10/2013 : 21:40:40
|
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? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
India
47035 Posts |
|
|
jmthome
Starting Member
USA
2 Posts |
Posted - 02/11/2013 : 02:20:39
|
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? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
India
47035 Posts |
Posted - 02/11/2013 : 03:20:14
|
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/
|
 |
|
|
James K
Flowing Fount of Yak Knowledge
1483 Posts |
Posted - 02/11/2013 : 09:12:32
|
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? |
 |
|
|
James K
Flowing Fount of Yak Knowledge
1483 Posts |
Posted - 02/11/2013 : 09:13:20
|
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? |
 |
|
| |
Topic  |
|
|
|