I am working on a Time Query with the following logic. But I am not familiar with time query. Can anyone help?
CASE WHEN (07:00:00) TO (07:10:00) is between [STARTTIME] AND [STOPTIME] THEN 'YES'
Start Time and Stop Time are both DateTime Field
Why not
CASE WHEN '07:00:00' >= [STARTTIME] AND '07:10:00' <= [STOPTIME] THEN 'YES'
if I understand clearly what you want to do.
HERE is the query :- I used #start_Time(for STARTIME) and #end_Time (for STOPTIME) as local variable you can replace them. (As you said between i am taking in between those values if you need to include those time too then instead of > you need to put >= and same with < replace with <=).
select CASE WHEN ((time_to_sec(timediff('07:00:00', time(#Start_Time))))>0
AND (time_to_sec(timediff('07:00:00', time(#end_Time))))<0)
AND
((time_to_sec(timediff('07:10:00', time(#Start_Time))))>0
AND (time_to_sec(timediff('07:10:00', time(#end_Time))))<0) =1
THEN 'YES' else 'NO' END
Hope it helps you !!
Related
I am at the final stage of my project and have the problem to find if a job is overdue. I link this to priority for example if a job has a priority of 1 it must be complete in 1 day, a priority of 4 then 4 days.
I have come up with a CASE however this doesn't seem to work any help would be appreciated.
SELECT `defect_Id`,`Overtasked`
WHERE
CASE DATEDIFF(DD,`date_Investigaton` - `CURRENT_DATE()`) >= `priority` AS Overtasked
THEN `Overtasked` == 'YES'
ELSE `Overtasked` == 'NO'
END
Solution
`SELECT defect_Id,
CASE WHEN DATEDIFF(date_Investigated, CURDATE()) >= priority
THEN 'YES'
ELSE 'NO'
END AS Overtasked
FROM defect_report
WHERE defect_Id = '82'`
Appreciate the guidance you guys give!
You are completely mixing up SQL dialects and even there are syntax errors.
Assuming you are talking about MS SQL Server let's try this:
SELECT defect_Id,
CASE WHEN DATEDIFF(DD, date_Investigaton, getdate()) >= priority
THEN 'YES'
ELSE 'NO'
END AS Overtasked
FROM <YourTable>
WHERE <YourWhereIfAny>
If date_Investigation is a DATE column, the subtraction date_Investigation - CURRENT_DATE() produces the number of days you need.
Otherwise (if it is a DATETIME, for example) both operands are converted to float and the result is something you are totally not expecting. For such situations use the DATEDIFF() function. It interprets its arguments as DATE (ignores the time part) and returns the integer number of days between the two dates.
Your query should be like:
SELECT
`defect_Id`,
IF (DATEDIFF(`date_Investigaton`, CURRENT_DATE()) >= `priority`, 'YES', 'NO')
AS `Overtasked`
FROM [...your table name here...]
WHERE [...conditions...]
Replace the parts in square brackets ([...]) with the name of the table where to get the data from and some conditions to limit the number of returned rows (otherwise it will get the entire table which, most probably, is not what you want).
Btw, CURRENT_DATE() is also a function. If you write it in backquotes (``), MySQL will try to find a column with this name and it will fail.
Read the accepted answer for this question. It explains when to use back ticks, single quotes or double quotes in MySQL (and partially in PHP).
I was granted access to a legacy database in order to do some statistics work.
I've so far gotten everything I need out of it, except I am trying to calculate a distance in time, using 5 values, stored in 4 columns (ARGGGHHH)
Above is a subsection of the database.
As you can see, I have start and stop date and time.
I would like to calculate the distance in time from str_date + str_time to stp_date + stp_time
The issue I have is, the calculation should be performed differently depending on the second value in stp_time.
IFF second value = "DUR".... THen I can just take the first value "01:04:51" in this scenario
IFF second value = anything else. stp_time represents a timecode and not a duration. This must then calculate stp_time - str_time (accounting for date if not same date)
All data is 24 hour format. I have done work with conditional aggregation, but I have not figured this one out, and I have never worked with a malformed column like this before.
Any and all advice is welcome.
Thanks for reading
SELECT
CASE WHEN RIGHT(stp_time,3)="DUR"
THEN
TIMEDIFF(LEFT(stp_time,8), '00:00:00')
ELSE
TIMEDIFF(
STR_TO_DATE(CONCAT(stp_date," ",LEFT(stp_time,8)), '%d/%b/%Y %H:%i:%s'),
STR_TO_DATE(CONCAT(str_date," ",LEFT(str_time,8)), '%d/%b/%Y %H:%i:%s')
)
END AS diff
FROM so33289063
Try this out, you might want a where condition for the subquery
With left and right:
SELECT IF(dur,stp,timediff(str,stp)) FROM(
SELECT STR_TO_DATE(CONCAT(str_date," ",LEFT(str_time,8)), 'd%/%b/%Y %H:%i:%s') as str,
STR_TO_DATE(CONCAT(stp_date," ",LEFT(stp_time,8)), 'd%/%b/%Y %H:%i:%s') as stp,
if(RIGHT(stp_time,3)="DUR",1,0) as dur
FROM my_table
) AS times
I have a table call Trn_EmployeeLeave How to get the Status depends upon todays date?
The Data Look Like
EmpNo start End Type
1 09-Jun-15 30-Jun-15 Justified
1 01-Jul-15 19-Aug-15 Sick Leave
1 20-Aug-15 20-Sep-15 Annual
Means suppose todays date in between 09-Jun-15 to 30-Jun-2015 then i will get the status "Justified" its depends on Todays date.
Please help on this.
Thanks
Basit.
MS SQL
suppose you want to get data from Trn_EmployeeLeave table
SELECT EmpNo,start,End,'Justified' Type
FROM Trn_EmployeeLeave
WHERE GETDATE() BETWEEN start AND End;
You can use CURDATE() for getting today's date. And Between can be used to find if a date/number lies in a range:
select Type from Trm_EmployeeLeave where curdate() BETWEEN start AND end;
Or you simply use greater than and less than operators
select Type from Trm_EmployeeLeave where start <= curdate() AND end >= curdate();
Use CASE like this
SELECT EmpNo,start,End,
CASE
WHEN GETDATE() BETWEEN startd AND end
THEN 'Justified'
WHEN --Other conditions THEN 'Sick Leave' ELSE 'Annual' END AS Type
FROM Trn_EmployeeLeave
I have the following line of code that can either return one of 2 conditions ('Expired','Active'). I needed to use DATEDIFF to figure out the dates I needed.
However, now I need to alter this code so that I can add a sort of else if condition if neither of the 2 conditions are met and result to 'Unknown'.
I have this so far:
SELECT
IF(DATEDIFF(#endDate:=ADDDATE(h.StartDate,Interval h.NumMonth Month),NOW())<0,'Expired',
IF(DATEDIFF(#endDate,NOW())<120,'Expires in 120 days or less','Active')) AS bActive
FROM ...
So, for now, I have Expired, Active but I also want to include else 'Unknown' to be included as option. How would I alter this logic? Do I need to use ELSEIF or CASE? What should I use?
Use case instead of if. First, case is ANSI standard, so it works across databases. Second, it handles multiple conditions better. I think your logic is:
SELECT (CASE WHEN DATEDIFF(ADDDATE(h.StartDate, Interval h.NumMonth Month), NOW()) < 0
THEN 'Expired'
WHEN DATEDIFF(ADDDATE(h.StartDate, Interval h.NumMonth Month), NOW()) < 120
THEN 'Expires in 120 days or less'
ELSE 'Active'
END) AS bActive
I am trying to pull data from my DB and have it organized by DATE by using the UNIX_TIMESTAMP but it just seems to not work properly. This is just one of my failed attempts. the issue that I am encountering is that I am trying to convert a Field in my table to a UnixTimeStamp, but it doesn't seem to work. the purpose of this query is to pull all future birthdays, and if the birthday has passed, we give it a '0' value in the hasItPassed column, if not, we give it a '1' value. the way I am trying to organize this table is have all upcoming birthdays listed (have the closest birthday at the top) and have all birthdays that have passed at the bottom of the list.
(EXAMPLE) bDay.date format --> '10/07/2013'
So my question is, does anyone know the best way to convert a date field in your DB to a unixTime format?? what happens in this example is in my field hasItPassed, everything gets turned into 0 when in fact some of the dates haven't passed(so some should have a 1 instead of a 0). Therefore I know I'm having issues with the conversion.
SELECT bDay.id, bDay.userid, bDay.summary,
bDay.time_create, bDay.Address, bDay.date, bDay.time,
CASE WHEN UNIX_TIMESTAMP(bDay.date) >= NOW( )
THEN '1'
ELSE '0'
END AS hasItPassed
FROM
BirthdayTable
WHERE
ORDER BY hasItPassed DESC ,
CASE WHEN hasItPassed =1
THEN date
END ASC ,
CASE WHEN hasItPassed =0
THEN date
END DESC
If there is any confusion, please let me know and I will reformat the question. thank yall!!!
Either make your comparison compare dates or timestamps - you have it mixed up.
e.g.
bDay.date >= date(NOW())
or
UNIX_TIMESTAMP(bDay.date) >= UNIX_TIMESTAMP(NOW())