# how to calculate Time Diffrence between Two columns in Sql Using Asp.net [Answered]RSS

## 3 replies

Last post Aug 02, 2016 01:09 PM by DDNaidu

• DDNaidu

Member

44 Points

82 Posts

### how to calculate Time Diffrence between Two columns in Sql Using Asp.net

Aug 01, 2016 11:09 AM|DDNaidu|LINK

How to calculate Time Diffrence Between Two columns in sql

I want the result like:

7hours 15min like this....

how to i write this Query..?? and

How to i show it in Gridview With Hours and Minitus..??

• march11

Contributor

2155 Points

2142 Posts

### Re: how to calculate Time Diffrence between Two columns in Sql Using Asp.net

Aug 01, 2016 01:38 PM|march11|LINK

You need to calculate what is called the TIMESPAN, it is a type and when both of your time values are properly defined, you can simply request the span between the two like this....

```        Dim dt1 As DateTime = "10/2/2008 8:05:03 AM"
Dim dt2 As DateTime = "10/1/2008 9:12:03 PM"

Dim tSpan As System.TimeSpan = dt1 - dt2```

• Brando ZWZ

Star

9461 Points

2955 Posts

### Re: how to calculate Time Diffrence between Two columns in Sql Using Asp.net

Aug 02, 2016 12:41 PM|Brando ZWZ|LINK

Hi DDNaidu,

#### DDNaidu

I want the result like:

7hours 15min like this....

how to i write this Query..?? and

According to your description, I suggest you could use 'DATEDIFF' method in sql to calculate Time Diffrence Between Two columns.

More details, you could refer to follow link:

https://msdn.microsoft.com/en-us/library/ms189794.aspx

Besides, if you want to show the result as '7hours 15min ', you could refer to follow codes:

```SELECT
+ CAST(DATEDIFF(second, StartDate, EndDate) / 60 / 60 / 24 / 7 AS NVARCHAR(50)) + ' weeks '
+ CAST(DATEDIFF(second, StartDate, EndDate) / 60 / 60 / 24 % 7 AS NVARCHAR(50)) + ' days '
+ CAST(DATEDIFF(second, StartDate, EndDate) / 60 / 60 % 24  AS NVARCHAR(50)) + ' hours '
+ CAST(DATEDIFF(second, StartDate, EndDate) / 60 % 60 AS NVARCHAR(50)) + ' minutes' as Datetime1 from [DateTimeTest]```

#### DDNaidu

How to i show it in Gridview With Hours and Minitus..??

You could refer to follow codes:

```<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DateTimeGrid.aspx.cs" Inherits="Grid.DateTimeGrid" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="Datetime1" HeaderText="Datetime1" SortExpression="Datetime1" />

</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%\$ ConnectionStrings:DemoConnectionString %>" SelectCommand="SELECT
+ CAST(DATEDIFF(second, StartDate, EndDate) / 60 / 60 / 24 / 7 AS NVARCHAR(50)) + ' weeks '
+ CAST(DATEDIFF(second, StartDate, EndDate) / 60 / 60 / 24 % 7 AS NVARCHAR(50)) + ' days '
+ CAST(DATEDIFF(second, StartDate, EndDate) / 60 / 60 % 24  AS NVARCHAR(50)) + ' hours '
+ CAST(DATEDIFF(second, StartDate, EndDate) / 60 % 60 AS NVARCHAR(50)) + ' minutes' as Datetime1  from [DateTimeTest]"></asp:SqlDataSource>
</div>
</form>
</body>
</html>
```

Datetable:

Result:

Best Regards,

Brando

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
• DDNaidu

Member

44 Points

82 Posts

### Re: how to calculate Time Diffrence between Two columns in Sql Using Asp.net

Aug 02, 2016 01:09 PM|DDNaidu|LINK

Wow...Exactly my Requirement..

Thank you Brando ZWZ....