urjudo
asked on
Convert hours to Day(s) in query in MS Access
Hi Experts,
I have a query question about convert from hours to day
attached is my DB, I have two queries, one is "qrySub" this is list all the days and hrs of the time had been taking. "qrySub1" is group by all the time had been taking. I'm having trouble to convert them into days and hrs.
for ex: in qrySub1 that list
Doe, Jane 25.5 hr(s) CT
9 hrs SL
11HRS VL
9 SL -- this is days
7 VL -- this is days
what I want to do is covert to : (8 hrs work in a day)
Doe, Jane 3 days 1.5 hrs CT
10 days 1 hr SL
8 days 3 hrs VL
Is possible to convert to like this? or any better suggestion
Thanks,
Time.mdb
I have a query question about convert from hours to day
attached is my DB, I have two queries, one is "qrySub" this is list all the days and hrs of the time had been taking. "qrySub1" is group by all the time had been taking. I'm having trouble to convert them into days and hrs.
for ex: in qrySub1 that list
Doe, Jane 25.5 hr(s) CT
9 hrs SL
11HRS VL
9 SL -- this is days
7 VL -- this is days
what I want to do is covert to : (8 hrs work in a day)
Doe, Jane 3 days 1.5 hrs CT
10 days 1 hr SL
8 days 3 hrs VL
Is possible to convert to like this? or any better suggestion
Thanks,
Time.mdb
ASKER
@Daniel,
I look at the query, how to I get the 1/2 SL (half day) as 4 hours as I still trying to understand your queries? Thanks
I look at the query, how to I get the 1/2 SL (half day) as 4 hours as I still trying to understand your queries? Thanks
Shouldn't 1/2 SL be SL and 1/2 VL be VL?
I would setup Leave to be
SL
SL Hrs
VL
VL Hrs
CT
and the actual value be in the Day field (which in fact should be renamed more appropriately to something like amnt, ...). If it true is Day, then Hrs values should be /8, thus 3.5 Hrs would be .4375. Then everything would be on a common value base and you could more easily sum the values to extract the Days/Hrs you seek.
Also, you should never use words like Name, Date, Weekday, Day, Time, ... as field,control,object names as they are all reserved words and may lead to strange behaviors and issues. You can learn more at http://www.devhut.net/2017 /06/21/ms- access-res erved-word -checker/
I would setup Leave to be
SL
SL Hrs
VL
VL Hrs
CT
and the actual value be in the Day field (which in fact should be renamed more appropriately to something like amnt, ...). If it true is Day, then Hrs values should be /8, thus 3.5 Hrs would be .4375. Then everything would be on a common value base and you could more easily sum the values to extract the Days/Hrs you seek.
Also, you should never use words like Name, Date, Weekday, Day, Time, ... as field,control,object names as they are all reserved words and may lead to strange behaviors and issues. You can learn more at http://www.devhut.net/2017
This query will do:
SELECT
DaysOffChild.Name,
Right([Leave],2) AS Type,
Sum(Val(Replace([leave],"1/2","0.5"))*IIf(InStr([Leave],"hrs"),1,8)) AS TotalHours,
Sum(Val(Replace([leave],"1/2","0.5"))*IIf(InStr([Leave],"hrs"),1,8))\8 AS Days,
Sum(Val(Replace([leave],"1/2","0.5"))*IIf(InStr([Leave],"hrs"),1,8))-(Sum(Val(Replace([leave],"1/2","0.5"))*IIf(InStr([Leave],"hrs"),1,8))\8)*8 AS Hours
FROM
DaysOffChild
GROUP BY
DaysOffChild.Name,
Right([Leave],2);
See the attached, please.
ASKER
@Gustav,
I tried your query, some are counting correctly but some of the calculation are counting double
TotalHours Leave
2 2 HRS CT
2.5 2.5 HRS CT
28 3.5 HR CT - this should be 3.5 instead 28
4 4 HRS CT
5.5 5.5 HRS CT
8 8 HRS CT
3 1.50 HRS SL -- this should be 1.5 instead 3
8 1/2 SL -- this should be 4 instead 8
6 2 HRS SL -- this should be 2 instead 6
0 SL -- this should be 8 instead 0
8 1/2 VL - this should be 4
6 2 HRS VL -- this should be 2
5 5 HRS VL
0 VL -- this should be 8
I tried your query, some are counting correctly but some of the calculation are counting double
TotalHours Leave
2 2 HRS CT
2.5 2.5 HRS CT
28 3.5 HR CT - this should be 3.5 instead 28
4 4 HRS CT
5.5 5.5 HRS CT
8 8 HRS CT
3 1.50 HRS SL -- this should be 1.5 instead 3
8 1/2 SL -- this should be 4 instead 8
6 2 HRS SL -- this should be 2 instead 6
0 SL -- this should be 8 instead 0
8 1/2 VL - this should be 4
6 2 HRS VL -- this should be 2
5 5 HRS VL
0 VL -- this should be 8
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
While this question should get a solution the real question should be why there is such kind of mix between numbers and alphanumeric...
If you need to put some kind of "note" of what kind of day/hours just put it on a separate column...and let the numbers do their thing and the "notes" their own..
Would it be better :
Leave (hours) TypeOfLeave
8 Normal
3 Medical
2 Parental
If you need to put some kind of "note" of what kind of day/hours just put it on a separate column...and let the numbers do their thing and the "notes" their own..
Would it be better :
Leave (hours) TypeOfLeave
8 Normal
3 Medical
2 Parental
This single query will list your 33 records and the calculated total hours for each:
This single query will sum the total hours, days, and hours:
Time.mdb
SELECT
DaysOffChild.Name,
Right([Leave],2) AS Type,
DaysOffChild.Leave,
(Val(IIf(Len([Leave])=2,1,Replace([leave],"1/2","0.5")))*IIf(InStr([Leave],"hr"),1,8)) AS TotalHours,
(Val(IIf(Len([Leave])=2,1,Replace([leave],"1/2","0.5")))*IIf(InStr([Leave],"hr"),1,8))\8 AS Days,
(Val(IIf(Len([Leave])=2,1,Replace([leave],"1/2","0.5")))*IIf(InStr([Leave],"hr"),1,8))-((Val(IIf(Len([Leave])=2,1,Replace([leave],"1/2","0.5")))*IIf(InStr([Leave],"hr"),1,8))\8)*8 AS Hours
FROM
DaysOffChild;
This single query will sum the total hours, days, and hours:
SELECT
DaysOffChild.Name,
Right([Leave],2) AS [Type],
Sum(Val(IIf(Len([Leave])=2,1,Replace([leave],"1/2","0.5")))*IIf(InStr([Leave],"hr"),1,8)) AS TotalHours,
Sum(Val(IIf(Len([Leave])=2,1,Replace([leave],"1/2","0.5")))*IIf(InStr([Leave],"hr"),1,8))\8 AS Days,
Sum(Val(IIf(Len([Leave])=2,1,Replace([leave],"1/2","0.5")))*IIf(InStr([Leave],"hr"),1,8))-(Sum(Val(IIf(Len([Leave])=2,1,Replace([leave],"1/2","0.5")))*IIf(InStr([Leave],"hr"),1,8))\8)*8 AS Hours
FROM
DaysOffChild
GROUP BY
DaysOffChild.Name,
Right([Leave],2)
Time.mdb
John is right though. If you have any control over the table structures, that is what truly should be address as it will subsequently simplify the rest of your work once it is fixed.
ASKER
@Gustav,
your queries work perfect for the hours, I forgot to mention that we also have minutes like 15 mins SL or 30 mins VL, 45mins CT, 50 mins OT etc. how do I convert the mins to hours? please be patience with me because I really stuck on this kind of calculations. thanks
your queries work perfect for the hours, I forgot to mention that we also have minutes like 15 mins SL or 30 mins VL, 45mins CT, 50 mins OT etc. how do I convert the mins to hours? please be patience with me because I really stuck on this kind of calculations. thanks
ASKER
@John,
I will consider your suggestion to make changes of the DB. Thanks
I will consider your suggestion to make changes of the DB. Thanks
Keeping data clean will keep away from troubles that shouldn't exist in the 1st place...
please be patience with
OK, but please have in mind, that we only the sample data you provide.
15 minutes is
15/60 = 0.25
Similar
50/60 = 0.83
Just do the division with the leftovers (minutes) and add to the hours
15/60 = 0.25
Similar
50/60 = 0.83
Just do the division with the leftovers (minutes) and add to the hours
ASKER
I converted the mins to hr but having trouble to add them to the "Total Hour" & hour. I create a new field in qyrNew called "MinToHr", please advise.
Timenew.mdb
Timenew.mdb
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you experts!!
You're welcome. Glad to help out!
Time.mdb