[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 334
  • Last Modified:

sql query

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
romeiovasu
Asked:
romeiovasu
  • 8
  • 8
  • 4
  • +1
3 Solutions
 
YveauCommented:
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
 
imitchieCommented:
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
 
YveauCommented:
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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
imitchieCommented:
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
 
PaultheBrokerCommented:
surely you can do better than:
convert(int,left(convert(varchar, timeout-timein, 108),2)) AS [Total Hours]
0
 
YveauCommented:
... 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
 
imitchieCommented:
yveau: datediff & http:#20407565
paul: I'm all ears!
0
 
imitchieCommented:
romeiovasu: I changed it to
WHERE timein BETWEEN '2007-12-01' and '2007-12-03'
because yyyy-mm-dd never needs conversion
0
 
PaultheBrokerCommented:
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
 
PaultheBrokerCommented:
@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
 
imitchieCommented:
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
 
PaultheBrokerCommented:
..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
 
imitchieCommented:
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
 
PaultheBrokerCommented:
@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
 
PaultheBrokerCommented:
...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
 
PaultheBrokerCommented:
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
 
imitchieCommented:
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
 
PaultheBrokerCommented:
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
 
romeiovasuAuthor Commented:
Guys here i tried everyone and it worked fine how should i give point now. becasue everyone is correct.
0
 
imitchieCommented:
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
 
YveauCommented:
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

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 8
  • 8
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now