Click here to Skip to main content
15,893,968 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I want a value of time data type from datediff of two dates in sql server 2008.
Posted
Updated 28-Mar-12 3:32am
v2

Please use the
SQL
datepart paramter in DATEDIFF ( datepart , startdate , enddate )

SQL
datepart
Is the part of startdate and enddate that specifies the type of boundary crossed. The following table lists all valid datepart arguments. User-defined variable equivalents are not valid.

http://msdn.microsoft.com/en-us/library/ms189794.aspx[^]
 
Share this answer
 
C#
DateTime startdts = new DateTime(29, 3, 2012, 14, 0, 0);
TimeSpan span = DateTime.Now - startdts;


Then you can use span.Total...... to get the total time difference of the two DateTime objects.

Hope this helps.
 
Share this answer
 
Comments
mayankkarki 28-Mar-12 8:53am    
Thanks,
I am using view in database,there is two datetime columns and on their datediff I have to calculate time.
V. 28-Mar-12 8:57am    
You mean this: http://www.dotnetspider.com/forum/148751-How-get-difference-between-two-times-sql.aspx?
TimeSpan diff = DateTime.Now.AddHours(2) - DateTime.Now;


TimeSpan has properties for time, hours, minutes, seconds, etc. What more do you need?
 
Share this answer
 
Comments
mayankkarki 28-Mar-12 8:52am    
Thanks,
I am using view in database,there is two datetime columns and on their datediff I have to calculate time.
[no name] 28-Mar-12 9:08am    
You tagged this as C# not database. Provide accurate information up front and you will get better answers. DATEDIFF has time. What is it that you want?
Try this

SQL
SELECT DATEDIFF(dd, '3/26/2012', '3/28/2012') [Day Difference]


Result

SQL
Day Difference
--------------
2


Other datepart types

SQL
SELECT DATEDIFF(HH, '3/26/2012 3:30 PM', '3/26/2012 5:30 PM') [Hour Difference]
SELECT DATEDIFF(MINUTE, '3/26/2012 3:30 PM', '3/26/2012 5:30 PM') [minute difference]


Similary you can add keywords like Month, SS (Second), MS (millisecond), MCS (microsecond) for differences
 
Share this answer
 
v2
Comments
mayankkarki 29-Mar-12 2:32am    
I am doing like that where tasksDurationDays column has no. of days calculated by datediff of startdate and enddate.Is there some other way in c# or sql,I am using view to fill datatable.
dataSet.Tables["Tasks"].Columns.Add("TaskDuration", typeof(TimeSpan));
foreach (DataRow dr in dataSet.Tables["Tasks"].Rows)
{
dr["TaskDuration"] = TimeSpan.FromDays(dr["TaskDurationDays"].ToString().ToInt32());
}
Sure this help you..


if time in second or Minutes then this help you.. to calculate.

select CAST(CallEndTime as datetime) - CAST( CallStTime AS datetime)as Actualtime from DB_Table/View.
 
Share this answer
 
Comments
mayankkarki 29-Mar-12 6:35am    
Thanks,
But this is not working.
Tarun Dutt 29-Mar-12 8:19am    
post some values from your table/view then tell me what value exact you want..
[no name] 30-Mar-12 23:42pm    
please try datediff

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