?
Solved

sql query

Posted on 2007-12-04
21
Medium Priority
?
329 Views
Last Modified: 2010-03-19
hi all,

i have a query written to calculate total hours for employees logged in. but with my query it is giving round figure. is there anyway i get decimal also. my query goes like this

SELECT employeeid, storeid, CONVERT(VARCHAR,timein,101) AS date, CONVERT(VARCHAR,timein,108)AS [clock in], CONVERT(VARCHAR,timeout,108) AS [Clock Out],
DATEDIFF(hour,timein,timeout)AS [Total Hours], ((DATEDIFF(minute,timein,timeout))/60)AS [Total minutes] FROM hrhours
WHERE timein IS NOT NULL AND
(timein BETWEEN CONVERT(DATETIME, '12/1/2007', 101) AND CONVERT(DATETIME, '12/3/2007', 101))
0
Comment
Question by:romeiovasu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 8
  • 4
  • +1
21 Comments
 
LVL 18

Assisted Solution

by:Yveau
Yveau earned 664 total points
ID: 20407523
Try this:

(changed from 60 to 60.0)

Hope this helps ...

SELECT  employeeid
,       storeid
,       CONVERT(VARCHAR,timein,101) AS date
,       CONVERT(VARCHAR,timein,108)AS [clock in]
,       CONVERT(VARCHAR,timeout,108) AS [Clock Out]
,       DATEDIFF(hour,timein,timeout)AS [Total Hours]
,       ((DATEDIFF(minute,timein,timeout))/60.0)AS [Total minutes] 
FROM    hrhours
WHERE   timein IS NOT NULL 
AND     (timein BETWEEN CONVERT(DATETIME, '12/1/2007', 101) AND CONVERT(DATETIME, '12/3/2007', 101))

Open in new window

0
 
LVL 25

Accepted Solution

by:
imitchie earned 668 total points
ID: 20407565
did you know... that DATEDIFF ignores all other fields?

i.e. DATEDIFF(hour, '12:59', '13:01') returns 1 ??

I think you need to revisit your query
0
 
LVL 18

Expert Comment

by:Yveau
ID: 20407567
I think you are looking for this:

(changed / to %)

Hope this helps ...
SELECT  employeeid
,       storeid
,       CONVERT(VARCHAR,timein,101) AS date
,       CONVERT(VARCHAR,timein,108)AS [clock in]
,       CONVERT(VARCHAR,timeout,108) AS [Clock Out]
,       DATEDIFF(hour,timein,timeout)AS [Total Hours]
,       ((DATEDIFF(minute,timein,timeout))%60)AS [Total minutes] 
FROM    hrhours
WHERE   timein IS NOT NULL 
AND     (timein BETWEEN CONVERT(DATETIME, '12/1/2007', 101) AND CONVERT(DATETIME, '12/31/2007', 101))

Open in new window

0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
LVL 25

Expert Comment

by:imitchie
ID: 20407618
SELECT employeeid, storeid,
 CONVERT(VARCHAR,timein,101) AS date,
 CONVERT(VARCHAR,timein,108)AS [clock in],
 CONVERT(VARCHAR,timeout,108) AS [Clock Out],
 convert(int,left(convert(varchar, timeout-timein, 108),2)) AS [Total Hours],
 convert(decimal(19,10),timeout-timein)*24*60 % 60 AS [Total minutes]
FROM hrhours
WHERE timein BETWEEN '2007-12-01' and '2007-12-03'
0
 
LVL 6

Assisted Solution

by:PaultheBroker
PaultheBroker earned 668 total points
ID: 20407687
surely you can do better than:
convert(int,left(convert(varchar, timeout-timein, 108),2)) AS [Total Hours]
0
 
LVL 18

Expert Comment

by:Yveau
ID: 20407718
... as we're at it ... try:

... or wait for SQL 2008 which has a time data type :-)


SELECT  employeeid
,       storeid
,       CONVERT(VARCHAR,timein,101) AS date
,       CONVERT(VARCHAR,timein,108)AS [clock in]
,       CONVERT(VARCHAR,timeout,108) AS [Clock Out]
,       DATEDIFF(hour,timein,timeout)AS [Total Hours]
,       cast(((DATEDIFF(hour,timein,timeout))%60)as varchar(2)) + ':'
+       right('00'+cast(((DATEDIFF(minute,timein,timeout))%60)as varchar(2)),2) + ':'
+       right('00'+cast(((DATEDIFF(second,timein,timeout))%60)as varchar(2)),2)AS [Total time] 
FROM    hrhours
WHERE   timein IS NOT NULL 
AND     (timein BETWEEN CONVERT(DATETIME, '12/1/2007', 101) AND CONVERT(DATETIME, '12/31/2007', 101))
 
--

Open in new window

0
 
LVL 25

Expert Comment

by:imitchie
ID: 20407990
yveau: datediff & http:#20407565
paul: I'm all ears!
0
 
LVL 25

Expert Comment

by:imitchie
ID: 20408007
romeiovasu: I changed it to
WHERE timein BETWEEN '2007-12-01' and '2007-12-03'
because yyyy-mm-dd never needs conversion
0
 
LVL 6

Expert Comment

by:PaultheBroker
ID: 20408628
well, I don't want to burst y'all's bubble, but check this out....
declare @timein datetime, @timeout datetime
set @timein = getdate()
set @timeout = dateadd(ms,12345346,getdate())
print @timein 
print @timeout 
 
print datepart(hour,@timeout -@timein)
print datepart(minute,@timeout -@timein)

Open in new window

0
 
LVL 6

Expert Comment

by:PaultheBroker
ID: 20408654
@Mitch -for someone that can write SQL code with two '=' signs in the same equation, I'm kinda disappointed in you    ....   :) :)
SELECT  employeeid
,       storeid
,       CONVERT(VARCHAR,timein,101) AS date
,       CONVERT(VARCHAR,timein,108)AS [clock in]
,       CONVERT(VARCHAR,timeout,108) AS [Clock Out]
,       DATEPART(hour,timein-timeout) [Total Hours]
,       DATEPART(minute,timein-timeout) [Total minutes] 
FROM    hrhours
WHERE   timein IS NOT NULL 
AND     timein BETWEEN '20071201' AND '20070101''--if you use 20071231 here, then the whole day of dec 31 will be missing, as this takes it to midnight only....

Open in new window

0
 
LVL 25

Expert Comment

by:imitchie
ID: 20408655
Good one Paul!
My mind was elsewhere, I knew of   datetime-datetime  for a quick diff as opposed to datediff, but forgot datepart.
You might as well post something that the questioner can use entirely
0
 
LVL 6

Expert Comment

by:PaultheBroker
ID: 20408696
..got my in's and out's mixed up (hey - no ones perfect :)  )
SELECT  employeeid
,       storeid
,       CONVERT(VARCHAR,timein,101) AS date
,       CONVERT(VARCHAR,timein,108)AS [clock in]
,       CONVERT(VARCHAR,timeout,108) AS [Clock Out]
,       DATEPART(hour,timeout-timein) [Total Hours]
,       DATEPART(minute,timeout-timein) [Total minutes] 
FROM    hrhours
WHERE   timein IS NOT NULL 
AND     timein BETWEEN '20071201' AND '20070101''--if you use 20071231 here, then the whole day of dec 31 will be missing, as this takes it to midnight only.... 

Open in new window

0
 
LVL 25

Expert Comment

by:imitchie
ID: 20408749
careful with  timein "IS NOT NULL "
that part's redundant. sometimes SQL Server goes crazy when it sees IS NULL or IS NOT NULL criteria, which will cause full table scan.
0
 
LVL 6

Expert Comment

by:PaultheBroker
ID: 20408889
@Mitch - really? .. haven't heard that one...(anyway, its not necessary as timein between will eliminate the NULLS anyway)

As I'm on a roll....my solution will fail if the shift is longer than 24 hours (for a 25 hour shift, the day will be 1, the hour will be 1 - so the above will return just 1 (not 25 !!).  Anyway, that got me to thinking....this is a more robust method.  


hour = floor(datediff(second,@intime,@outtime)/3600)
minute =  (datediff(second,@intime,@outtime)%3600)/60 

Open in new window

0
 
LVL 6

Expert Comment

by:PaultheBroker
ID: 20408900
...and I've just had a horrible thought, but maybe all that is wanted is this:
hours_minutes = convert(varchar,@outtime-@intime,108)

Open in new window

0
 
LVL 6

Expert Comment

by:PaultheBroker
ID: 20408931
hmmm - or maybe the answer being looked for is the number of hours expressed as a decimal (so it can be multiplied by a per-hour rate, for instance....)...which is actually almost back at Yveau's original suggestion.

(so the solution is NOT to divide by an integer, as that will return an integer - you have to add a couple of spurious decimal places....._
hours_including_decimal = datediff(minute,@intime,@outtime)/60.00

Open in new window

0
 
LVL 25

Expert Comment

by:imitchie
ID: 20409610
Paul: I think you've finally come around. The problem with Yveau's suggestions are that they don't take in to account the problem with datediff(hour as pointed out in http:#20407565
0
 
LVL 6

Expert Comment

by:PaultheBroker
ID: 20410858
Yes, well I only jumped into this question because of
convert(int,left(convert(varchar, timeout-timein, 108),2)) AS [Total Hours]  !!!!!!!!!
But your point was a good one for the hours column - 1 hour +/- 1 hour isn't very useful.....of course when it comes to the 'minutes' calculation, 4.25 hours +/1 one minute isn't too bad.... (which is what Yveaus original solution actually was) and of course datediff(second,@intime,@outtime)/3600.00 would be accurate +/- one second, which is probably overkill for this application....However, I also appreciated the IS NULL insight into performance - so thanks for that too !!! :)

Anyway, I'm sure Yveau nailed it at the beginning - so the general point for romeiovasu to remember is that MSSQL tends to prefer integers, and will degrade numbers to integers if if possibly can .... Rule of thumb is in mathematical operations like this, always stick some zeroes after the decimal point....even you are dividing by a whole number....
0
 

Author Comment

by:romeiovasu
ID: 20411808
Guys here i tried everyone and it worked fine how should i give point now. becasue everyone is correct.
0
 
LVL 25

Expert Comment

by:imitchie
ID: 20414487
May I suggest give to every correct solution. However, we have no idea which one solved your exact problem. Did  you really want hours, minutes the common sense way in two separate columns? i.e.

start: 9:50am
end: 12:10pm
worked [hours]: 2
worked [minutes]: 20
?
0
 
LVL 18

Expert Comment

by:Yveau
ID: 20430466
Split them up as you feel is fair ... we're just happy to help you guys out in the big bad SQL jungle :-)
0

Featured Post

Does Your Cloud Backup Use Blockchain Technology?

Blockchain technology has already revolutionized finance thanks to Bitcoin. Now it's disrupting other areas, including the realm of data protection. Learn how blockchain is now being used to authenticate backup files and keep them safe from hackers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question