Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
jmthome
Starting Member
2 Posts |
Posted - 2013-02-10 : 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
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-02-10 : 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 DiffDateFROM 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 DaysFROM TheTable Perhaps that is not what you want, you want to consider calendar months? |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
jmthome
Starting Member
2 Posts |
Posted - 2013-02-11 : 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 DiffDateFROM 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 DaysFROM 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
52326 Posts |
Posted - 2013-02-11 : 03:20:14
|
for months it should be SELECT DATEDIFF(month,lease_start_date,lease_end_date) AS MonthsElapsedFROM TheTable if you want days elapsed also use James earlier suggestiondidnt 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 MVPhttp://visakhm.blogspot.com/ |
|
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-02-11 : 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
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-02-11 : 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? |
|
|
|
|
|
|
|