I am working on a time system that require manual input of the coming and going times. (not my system) I am building a dashboard for this system that will show average time on site and more. Because it requires a manually entered coming and going time, mistakes can happen. If someone checks in at 18:00hours but forgets to clock out, the system automatically leaves the clock out time at 0:00:00 hours.
When calculating my averages, if the above occurs, then it calculates the average time spent on site and adds in a -18:00 hours. This obviously breaks the whole calculation. Is there a way to have the query ignore any negatives to avoid this?
SELECT id, TIMEDIFF(`booking_time_out`, `booking_time`) AS 'Time_Spent'
FROM `table_name`
The negative result criteria is a result of the 0:00:00 booking out time so append the exclusion of that row in the where criteria like:
where booking_time_out != '0:00:00'
You seem to want a case expression:
select id,
case when timediff(`booking_time_out`, `booking_time`) < 0
then 0
else timediff(`booking_time_out`, `booking_time`)
end as time_spent
from tablename
Side note: do not surround identifiers with single quotes (as in as 'Time_spent'). In standard SQL, single quotes stand for literal strings. On the other hand, you usually do not need to quote an identifier (unless its name is really badly chosen) - and if you need to, the quoting character of MySQL is backticks.
Related
Ok this should be a relatively easy thing to do, yet I'm at the head desk stage trying to figure out the insanity here.
I have a table called tblPersonnel. I'm tracking two document expiration dates in date/time fields called CED and PPED. When I run a query against tblPersonnel I need it to look at PPED, determine if that document is expired and if so use CED instead. I have a few fields in the query that need to use this concept to determine what the output value is, but I am hitting a wall here trying to get the query to spit out the correct value. Here's what I'm using for one of the fields - Document Expiration Date: IIf([PPED]-Now()<0,[CED],[PPED]). What's happening is that the expression is constantly popping as false, so PPED is getting used regardless if it's an expired date or not. Does anyone have any ideas as to what I'm doing wrong here?
I've also tried to set this up as its own field in tblPersonnel, but that's even more aggravating. If I try to set the field to just a text field - IIf([PPED]-Now()<0,"Yes","No"), the formula will accept the use of Now(), but it doesn't like the reference to the other fields in the table. If I set it as a calcuated column, I can reference the other fields but it doesn't like Now(). I'm at a loss here.
If PPED is less than Date(), it is expired. Don't need to subtract. Assuming CED and PPED are just date parts, no time, consider:
IIf([PPED] < Date(), [CED], [PPED])
If PPED could be null:
IIf(Nz([PPED],0) < Date(), [CED], [PPED])
Ok finally fixed it here. I had another issue in that I wasn't accounting for how Access would handle a Null or blank value in PPED. The functioning formula is Document Expiration Date: IIf(Len([PPED])>0,IIf([PPED]<Date(),[CED],[PPED]),[CED]) Thanks to June7 for helping me simplify the expression, as I was using DateDiff('d',[PPED],Date())<0 but their answer is just so much cleaner and quicker to type.
Alright so I'm re-writing some scripts that were provided to me and part of the script is constructing a date from two different columns, one of which could contain data such as this 3|15 and then that constructed date is being compared to today's date.
Original query section:
CASE WHEN rs.RecurrencePattern = 'Monthly' AND DAYDIFF((CreateDate(GetYear(rs.ws_wg_mig_start_date),GetMonth(rs.ws_wg_mig_start_date), toint(StrParts(rs.RecurrencePatternParms, '|',1)))),(now())) < 0 THEN GetMonth(rs.ws_wg_mig_start_date)+1
I think this is MySQL but I'm not real sure. I'm trying to recreate this in SQL Server and so far I'm using DATEFROMPARTS and stringing everything together. I'm using a NULLIF(CHARINDEX to get the value after the | but if it doesn't exist, then the whole date field is null. I need another pair of eyes on this before I go insane.
Current SQL Code:
DATEFROMPARTS(datepart(year,rs.ws_wg_mig_start_date),datepart(month,rs.ws_wg_mig_start_date),Convert(int,Right(recurrence_pattern_params, NULLIF(CHARINDEX('|', reverse(recurrence_pattern_params)),0)-1)))
I want to make a simple timer for processes that I run with a Macro. I'm using SetTempVar to record the start and end times, and a simple query to calculate the elapsed time.
The macro is:
Then the query is simply:
SELECT [tempvars]![ProcessStart] AS Start, [TempVars]![ProcessEnd] AS [End], DateDiff("s",[start],[end]) AS Seconds;
But the output is strange:
The 2 fields from SetTempVar display in some strange font. However, the elapsed time of 84 seconds is correct.
How can I display the start and end times correctly?
I can reproduce this. It's an odd issue.
The TempVar gets interpreted as a string, though it contains a date. The binary date data gets interpreted as UTF-16 characters, displaying random characters (often Chinese since there are many Chinese characters in UTF-16).
I'd consider this a bug in Access. Queries should correctly determine variable type, and that's apparently somehow going wrong.
To display the date value, use either Format or CDate.
If you're interested in a time difference, I recommend formatting it as Long Time:
SELECT Format([tempvars]![ProcessStart], "Long Time") AS Start, Format([TempVars]![ProcessEnd], "Long Time") AS [End], DateDiff("s",[start],[end]) AS Seconds;
I created a MS Access database for tracking some items at work. Users enter data which is then aggregated in queries (using count or sum) to calculate the actual value for each area, joined using UNION, then compared to goals for that area. I attempted to enter an IIF statement to conditionally calculate the percentage of [Act]/[Goal], leaving it zero if [Act] is blank or 1 if [Act] is greater than [Goal] so there is nothing over 100%. The issue is that it works most of the time, but other times fails with no obvious logic error or reason why that I can figure out.
Sometimes it can't tell that [Act] > [Goal] even though looking at it, it's obvious. The numbers are all integers, nothing crazy or formatting differences. The formula in [Met] is what I hope to achieve. I added the [TEST] field to trace back where it might not be working, which shows Access just isn't always returning the correct answer to [Act] > [Goal].
My Query:
What comes out (just the broken part):
As you can see, it works correctly for most rows, but then thinks 149 is less than 52, and 128 is less than 3. Because of this, it generates [Met] values over 100%.
Has anyone had this happen before or have any suggestions? I have tried using refresh, clicking in the cell to hit enter, everything I can think of.
I think that although your columns are Ints, they are being converted to strings in the variables (or at least one of the variables) Goal and Met.
If you look at the data, you'll see that if you compare them the results of Test are correct for a string comparison.
E.g. "3" > "128" (Because the first character has a higher char value).
In your query, try surrounding the variables with Val() when you are comparing them, as follows:
IIf(IsNull([Act]),0,IIf(Val([Act])>Val([Goal]),1,Round([Act]/[Goal],2)))
I have a problem to show age in a view with rails
i think this to solve:
show.html.slim
`=#people.date_born - Date.today / 365,25`
`end`
What i need to do?
It's probably just an order of operations thing, but your syntax was displayed funny. Ignoring the unnecessary quotes:
= ((Date.today - #person.date_born) / 365).to_i
First, since today is a greater date than the date of birth, you want it first to avoid a negative number. You need to wrap it in parens to do the subtraction first, and then divide, and for legibility, change it back to an integer.