Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi
I am having a problem as i am trying to calculate the defference between 2
max(startdate) and min(enddate) using a stored procedure in sqlserver 2008. my datatype for startdate and enddate is varchar(50)

SQL
SELECT  
 convert(datetime,min(startDate)) - convert(datetime,max(endDate))
from projects

How can i perform this calculation
Posted
Updated 30-May-12 1:00am
v3

Use DateDiff
SQL
SELECT DATEDIFF(DAY, Convert(datetime,MIN([date])),CONVERT(datetime,MAX([date]))) AS NumberOfDays from tablename
 
Share this answer
 
Comments
kolisa 30-May-12 7:19am    
My startdate and enddate are 2 columns name from project table and i want to get difference(subtract startdate from endate like this startdate-enddate)then get the difference
uspatel 30-May-12 7:27am    
use as
SELECT DATEDIFF(DAY, Convert(datetime,MIN([startdate])),CONVERT(datetime,MAX([enddate]))) AS NumberOfDays from tablename
kolisa 30-May-12 7:52am    
Thanks alot
Maciej Los 30-May-12 7:57am    
Good answer, my 5!
To get absolute (positive) value, use ABS[^] function:
SQL
SELECT ABS(DATEDIFF(DAY, MIN([startdate]),MAX([enddate])) AS NumberOfDays from tablename
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900