473,407 Members | 2,314 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

subtract two datetime values

12
Hi,

I am searching for a way to find the total no of hours:min between two dates.

My table design is as below:

EmployeeId | StartTime | EndTime | BreakPeriod |

Sample data:
100 | 2011-09-14 08:00:00.000 | 2011-09-14 15:30:00.000 | 30

I need to find the total no of hours:mm between these two datetime values. (excluding the breakperiod).

-----

I tried the following query. But it includes the BreakPeriod
Expand|Select|Wrap|Line Numbers
  1. select
  2.     case when ltrim(rtrim(cast(floor(datediff(n,StartTime,EndTime)/60) as char(3)))) = 0 then '00'
  3.     else ltrim(rtrim(cast(floor(datediff(n,StartTime,EndTime)/60) as char(3)))) end + ':' +        
  4.     case when ltrim(rtrim(cast(datediff(n,StartTime,EndTime) % 60 as char(3)))) = 0 then '00'
  5.     else ltrim(rtrim(cast(datediff(n,StartTime,EndTime) % 60 as char(3)))) end As TotalHrs
  6.     from 
  7.  tblEmployee
  8. where id = 5
  9.  
  10.  

I want to find solution for both scenarios. That is when (i) the breakperiod is in minutes for eg: 100 minutes and also (ii) when the breakperiod is in hh:mm eg: 1:45


Can anyone please help me with this problem. This is very urgent.
Jan 28 '12 #1

✓ answered by dha lak

I solved using this query.

SELECT
convert(varchar(5),convert(datetime,cast(((DATEDIF F(minute, StartTime, EndTime) - cast(BreakPeriod as int)) / 60) as char(5)) + ':' + cast(((DATEDIFF(minute, StartTime, EndTime) - cast(BreakPeriod as int)) % 60) as char(5))),108) as TotalCalculatedHours
from tblEmployee

1 4171
dha lak
12
I solved using this query.

SELECT
convert(varchar(5),convert(datetime,cast(((DATEDIF F(minute, StartTime, EndTime) - cast(BreakPeriod as int)) / 60) as char(5)) + ':' + cast(((DATEDIFF(minute, StartTime, EndTime) - cast(BreakPeriod as int)) % 60) as char(5))),108) as TotalCalculatedHours
from tblEmployee
Jan 30 '12 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Thomas Bartkus | last post by:
MySQL Version 4.0.20 on a Linux server. How does one get the elapsed time between (2) DateTime values? I need the answer to the nearest minute. Is upgrading to Ver 5 with its more robust...
1
by: Shai Zohar | last post by:
We have encountered the following problem with DOT.NET 2003 development environment. Sometimes while debugging, we the environment does not display the values of DateTime variables and fields. ...
2
by: Echo 8 | last post by:
Is there a simple way of compare dates? I am trying to create a list of perishable stock in an inventory that is near or past its expiration date. I've tried telling it to compare a node's value...
1
by: sandip | last post by:
Hi all, I am trying to load data from a Sybase table with 'datetime' column into a DB2 table with 'Date' type column? I used the following command : db2 "IMPORT FROM tabname.dat OF DEL...
1
by: Bruce Vander Werf | last post by:
I need to format a DateTime in the following format: "Mmm dd hh:mm:ss" Where Mmm is the three-letter abbreviation for the month. What's the best way to do this in C#? --Bruce
2
by: Jonesgj | last post by:
Hi, I have connected to a SQL Server table using dataAdapter/Dataset to fill a data grid. On SQL Server a column is a DateTime datatype (its actually a 'LastLogon' column ) and has both Date and...
2
by: Joe Van Meer | last post by:
Hi all, Do dates & time values always get inserted according to the user's main language setting in their browser? Like, if I had an application that somehow relied on comparing dates and...
5
by: A.M | last post by:
Hi, I have a datetime value and want to format it to "June 1, 2006" shape. How can I do that? Thank you,
2
by: deepbright | last post by:
I'm having trouble understanding the MySQL docs on how to subtract two DATETIME values. I have two tables that have a DATETIME column. In my SELECT I am doing a JOIN to bring in both sets of rows....
1
by: remya1000 | last post by:
I’m using VB.net 2003 application program. I am trying to do a select statement whereby I'm searching between 2 datetime values that are being stored as datetime. records are stored inside...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.