Solved

sql query

Posted on 2007-12-04
21
292 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
  • 8
  • 8
  • 4
  • +1
21 Comments
 
LVL 18

Assisted Solution

by:Yveau
Yveau earned 166 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 167 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
 
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 167 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now