how to compare time with current time but with AM pm format also compare in mysql select query.
I added Database image where stored time in this formate and i compare to current time but i didn't get success.
You need to convert expectedDeliveryDateTime to a date so you can compare it with NOW(). To do that you need to use STR_TO_DATE with the format '%d/%m/%Y %h:%i %p' which matches the data in your image. So try:
SELECT *
FROM yourtable
WHERE STR_TO_DATE(expectedDeliveryDateTime, '%d/%m/%Y %h:%i %p') >= NOW()
I have assumed you are looking for dates in the future, if you want dates in the past just change >= NOW() to <= NOW().
use now()and format date using date_formatas your column is varchar so you have to convert it date by using STR_TO_DATE function
select * from t where
STR_TO_DATE(expectedDeliveryDateTime, '%d/%m/%Y %h:%i %p') >=date_format(now(), '%d/%m/%Y %h:%i %p')
Please, check my time zone. Are we the same?
SELECT UTC_TIMESTAMP() from dual;
If the same, then you compare is correct.
Related
I'm working with a quite old database... And dates are stored as VARCHAR, and I need to keep dates as VARCHAR :S but expanding it with year=2020, hour=0, minute=0, and second=0 if that fields are not set.
Dates have all kind of weird formats, such as '01/08', '01/08/20', '01/08/2020' and so on. I'm dealing good with all formats, except when year is not set, I have tried with this query, but it set the year to 0 and causes an error with STR_TO_DATE() function
UPDATE people
SET date =
IF (
YEAR(STR_TO_DATE(date, '%d/%m/%Y %H:%i:%s')) > 0 ,
DATE_FORMAT(STR_TO_DATE(date, '%d/%m/%Y %H:%i:%s'), '%d/%m/%Y %H:%i:%s') ,
DATE_FORMAT(STR_TO_DATE(date, '%d/%m/%Y %H:%i:%s') + INTERVAL 2020 YEAR, '%d/%m/%Y %H:%i:%s')
)
Any idea dudes?
MariaDB [supplier monitoring]> select time as "h:mm:a" from delivers;
I just wanna ask how I can I cast this time into "h:mm:a" like this 1:00pm
If you have a column of time datatype, you can use time_format():
select time_format(mycol, '%h:%i %p') from delivers;
If the column is of datetime datatype, then you want date_format():
select date_format(mycol, '%h:%i %p') from delivers;
I have a string '10/01/2016 00:00' that I want convert to DATETIME
I tried like this:
select STR_TO_DATE('10/01/2016 00:00', '%d/%m/%Y %h:%i');
But it's not working. what i am doing wrong?
The problem is that %h expects an hour in the format 01-12 and you are providing an hour that is 00. You can use %H that expects an hour in the format 00-23, try with this:
select STR_TO_DATE('10/01/2016 00:00', '%d/%m/%Y %H:%i');
I have this query
SELECT * FROM tracklogs.sms_outbound
WHERE gsmno = 'rk4#*******.com.ph'
AND cdate > cast('2013/11/14 09:44:48 PM' as datetime)
where cdate format is in %Y-%m-%d %h:%i:%s %p.
I have tried converting the date into that format then cast it as datetime but still doesn't working.
Use STR_TO_DATE() to correctly convert the datetime literal you have provided to a proper DATETIME value. It seems that your cdate column is a char() or varchar() column. So you will also need to convert that to DATETIME to compare it.
What you need is this:
That works like this (http://sqlfiddle.com/#!2/d41d8/48741/0)
STR_TO_DATE(cdate, '%Y-%m-%d %h:%i:%s %p') >
STR_TO_DATE('2013/11/14 09:44:48 PM', '%Y/%m/%d %h:%i:%s %p')
Converting these strings to DATETIME data items ensures that the comparison handles both the date and the time correctly. See this fiddle (http://sqlfiddle.com/#!2/d41d8/48743/0)
But, you should consider changing your cdate item to a DATETIME, because then you'll be able to index it and speed up your search.
SELECT * FROM tracklogs.sms_outbound
WHERE gsmno = 'rk4#*******.com.ph'
AND UNIX_TIMESTAMP(str_to_date(cdate,'%Y-%m-%d %H:%i:%s')) > UNIX_TIMESTAMP('2013-11-14 09:44:48')
I have a mysql table that has a column that stores dates but isn't in the date format, it's a varchar.
The column is called data_hora and have dates in the dd/mm/yy format, example: 06/09/2012 15:00, so I had to convert to date format in mysql query.
And I need to get the closest date and hour before or after the current time, I came up with the following code, but for some reason it seems to get only closest date but not hour, weird?!?!
SELECT str_to_date(data_hora, '%d/%m/%Y %H:%i') AS data_hora
FROM requisicoes
ORDER BY abs(DATE_FORMAT(NOW(),'%d/%m/%Y %H:%i') - data_hora) LIMIT 1
Help :(
try this:
Your ORDER BY Clause has to be changed
SELECT str_to_date(data_hora, '%d/%m/%Y %H:%i') AS data_hora
FROM requisicoes
ORDER BY abs(TIMEDIFF( NOW() , str_to_date(data_hora, '%d/%m/%Y %H:%i'))) LIMIT 1
You shouldn't format NOW() to a string: it is already a TIMESTAMP value; instead, take the (absolute) difference between the present UNIX_TIMESTAMP() and that of the data_hora alias for your selected STR_TO_DATE() column:
ORDER BY ABS(UNIX_TIMESTAMP() - UNIX_TIMESTAMP(data_hora))
If at all possible, I would advise altering your schema so that your data_hora column is stored as a TIMESTAMP: it will greatly improve the performance of queries of this sort.