Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to be able to find all employees whose review is due in the next 45 days. I have the MS SQL:
Select *
from Employee
where DATEDIFF(yy,HireDate,GetDate()+45) > DATEDIFF(yy,HireDate,GetDate())
How does this convert to MySQL?
try:
Select *
from Employee
where DATEDIFF(HireDate,DATE_ADD(CURDATE(), INTERVAL 45 DAY)) > DATEDIFF(HireDate,CURDATE())
I think you can just use CurDate instead of GetDate, so it'd just be:
Select * from Employee where DATEDIFF(HireDate,CurDate()) > DATEDIFF(HireDate,CurDate())
You should only need to check DATEDIFF once. Just get the number of days and compare.
SELECT *
FROM Employee
WHERE DATEDIFF(CURDATE(), HireDate) <= 45
Docs for MySQL's DATEDIFF (and other date/time functions): http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 2 years ago.
Improve this question
I have a column name called last_activity that is of type DATETIME. (so. 2021-01-03 00:00:00). I want to find all rows which are within 5 minutes of now. I attempted the following:
SELECT *
FROM users
WHERE last_activity > DATE_SUB(NOW(),INTERVAL 5 MINUTE)
I get 0 rows, even though the activity is within 5 minutes (I know because the database says so). How can I fix this?
Use the function TIMESTAMPDIFF() to get the difference in minutes:
SELECT *
FROM users
WHERE ABS(TIMESTAMPDIFF(MINUTE, last_activity, NOW())) <= 5
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
i need to insert reg_id of patient form first table (registration_table) to second table (patient_status) for those patient whose registration is done 30 days before
my query
INSERT INTO patient_status(reg_id) SELECT reg_id FROM registration_table
WHERE DATEDIFF(day,reg_date,getdate()) >= 30
it says
Incorrect parameter count in the call to native function 'DATEDIFF'
DateDIFF return the number of DAY so you need simply and in mysql you should use CURDATE() (getdate() should be from sql-server)
INSERT INTO patient_status(reg_id)
SELECT reg_id FROM registration_table
WHERE DATEDIFF(curdate(), reg_date ) >= 30
you're supplying a third parameter to your DATEDIFF(start,end) function.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
In my database in table_a every row has a date_created like "2011-04-17"
Now some of these dates are in the past, but my question is how can I retrieve the latest date that has not yet passed?
Try this one
SELECT * FROM table_a WHERE CURDATE() <= date_created
CURDATE()
Returns the current date as a value in 'YYYY-MM-DD' or YYYYMMDD
format, depending on whether the function is used in a string or
numeric context.
Can you try this,
SELECT * FROM table_a WHERE date_created >= CURDATE()
IF date_created is a date datatype then you can use
SELECT *
FROM table_a
WHERE date_created >= NOW()
LIMIT 1
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to create a query in mysql to retrieve data where CREATED_DATE range should be 20th of current month to 20th of previous month. Please suggest..thanks
You can get dynamic records using current month, by the following query
SELECT * FROM table_name WHERE CREATED_DATE BETWEEN
DATE_FORMAT(NOW(),'%Y-%m-20') - INTERVAL 1 MONTH
AND DATE_FORMAT( NOW(),'%Y-%m-20')
Check the live result at the fiddle http://sqlfiddle.com/#!2/4668c/7
SELECT
*
FROM
your_table
WHERE
`CREATED_DATE` BETWEEN "2013-10-20" AND "2013-11-20";
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am trying to write following query but i find id defecult for me.
select id,comment,comment_date from comments;
Here I want to add a field, if current time is more that 5 minutes of comment_date
then it should show no else yes.
How to write query for above.
One uses the date_sub function for date delta, taking a time and an interval, as follows:
select id
, comment
, comment_date
, case
when comment_date < date_sub(now(), interval 5 minute)
then 'yes'
else 'no'
end as answer
from comments;
SELECT
id,
comment_date,
CASE WHEN TIMESTAMPDIFF(MINUTE, comment_date, NOW()) > 5 THEN 'No'
ELSE 'Yes'
END AS isnew
FROM comments;