Difference in month year and days using DateDiff in MSSQL queryWe can use DateDiff function in MSSQL query to get difference in dates between two date and time fields. Here we will discuss how to use DateDiff inside a MSSQL query.
DateDiff(return type , date1, date2)Here we can specify the return type like month , days, year, weeks etc. Date1 and date2 are two dates between which the difference is to be calculated.
We will use the same table what we have used in update date and time field example. So let us find out the query required to collect difference in days between two dates.
rs1.open " select book_id,DateDiff(mm,issue_dt,return_dt) as dif, issue_dt, return_dt from dt " , connUsing the above query we can find out difference in days. The rest of the code remains same for any other record display script. We will display the total script at the end of this tutorial.
Now let us go for finding out difference in years
rs1.open " select book_id,DateDiff(yyyy,issue_dt,return_dt) as dif, issue_dt, return_dt from dt " , connYou can see the difference in formatting the DateDiff function in above SQL. See the list of different formats for DateDiff here. Here is the total code.
Table structure is here