DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   ASP.NET (http://www.daniweb.com/forums/forum18.html)
-   -   Subtracting DateTime Data Type in SQL Server (http://www.daniweb.com/forums/thread130051.html)

fayenot Jun 18th, 2008 10:38 pm
Subtracting DateTime Data Type in SQL Server
 
I have a table for projects, it has two fields for project_start and project_end both have datetime data type. what should i do so that when the user insert dates on both field it will automatically subtract and place the answer on the duration field... please help.. oh before i forgot, the textbox for the fields are located in a formview...how can i subtract project_start and project_end and put its difference to duration field

StephenP Jun 19th, 2008 12:59 am
Re: Subtracting DateTime Data Type in SQL Server
 
You can try using DATEDIFF, I think it's built into MS SQL. It's not terribly accurate though, in terms of defining boundaries between days, months, years, so you have to tweak around with it a bit - for each unit (seconds, minutes), you have to divide the value it returns, then put them together again.

For example:
take your:
x = DATEDIFF(@project_start, @project_end);

seconds = x / 60;
minutes = (x / 60) / 60;
And so on.

Hope I helped,
Stephen

OldDude Jun 19th, 2008 1:00 am
Re: Subtracting DateTime Data Type in SQL Server
 
Try this
http://keentiger.com/2008/01/22/sql-...tween-2-dates/

or Google SQL server datediff

sbv Jun 23rd, 2008 5:02 am
Re: Subtracting DateTime Data Type in SQL Server
 
Quote:

Originally Posted by OldDude (Post 630266)
Try this
http://keentiger.com/2008/01/22/sql-...tween-2-dates/

or Google SQL server datediff

nice link. thanks.


All times are GMT -4. The time now is 5:06 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC