Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.75/5 (4 votes)
See more:
I have to count days between two dates in stored procedure.
How to do this?
Posted
Updated 10-Feb-13 22:32pm
v2
Comments
Orcun Iyigun 11-Feb-13 2:48am    
in sql or in c#?
Member 9511889 11-Feb-13 2:49am    
in stored procedure.

Well, if I was using SQL Server, i would use DATEDIFF.
 
Share this answer
 
For C#:
C#
DateTime d1=DateTime.MinValue;
DateTime d2=DateTime.MaxValue;
TimeSpan span=d2-d1;

For SQL;
The basic interpretation for your third column would be as follows (the "d" tag measures the date difference in days):
SQL
DateDiff("d",[DateReceived],[DateClosed])

If you want to add an exception that handles records with no closed date, use this one (optionally, if you just want to look at records where there is a closed date, add "Is Not Null" as the criteria for ClosedDate and use the top expression):
SQL
IIf(IsNull([DateClosed]),"Not Closed Message",DateDiff("d",[DateReceived],[DateClosed]))


Good luck,
OI
 
Share this answer
 
 
Share this answer
 
SQL
select DateDiff(DAY,'01-Jan-2012','05-Jan-2012')
 
Share this answer
 
Comments
[no name] 11-Feb-13 4:15am    
+5
Example
C#
DateTime dt = DateTime.Parse("01/01/1980");
   DateTime dt1 = DateTime.Parse("05/01/1980");
   TimeSpan sp = dt1 - dt;
   int dayDiffrence = sp.Days;
 
Share this answer
 
v2

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