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;
Related
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.
I have a MYSQL table which has a column name called timestamp stored in string format. the timestamps in this column are in the following format e.g. '20/10/2014 05:39 PM'
Now how can select a row and convert the timestamp column to 24HR format on the fly.
basically I want something like this.
SELECT id, user, STR_TO_DATE(timestamp, '%d/%m/%Y %h:%i %p') as timestamp FROM mytable WHERE user="bob";
but this does not work. looks like its not recognizing the timestamp variable inside STR_TO_DATE sql function and its retuning NULL for the timestamp column.
Please help.
Looks fine to me:
mysql> SELECT STR_TO_DATE('20/10/2014 05:39 PM', '%d/%m/%Y %h:%i %p');
+---------------------------------------------------------+
| STR_TO_DATE('20/10/2014 05:39 PM', '%d/%m/%Y %h:%i %p') |
+---------------------------------------------------------+
| 2014-10-20 17:39:00 |
+---------------------------------------------------------+
(I know this should be a Comment, but is an Answer in order to get formatting.)
use date_format function instead of str_to_date:
SELECT id, user, date_format(timestamp, '%d/%m/%Y %h:%i %p') as timestamp FROM mytable WHERE user="bob";
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 am working in cakephp framework,
In that I am storing date string in varchar field. I need to convert it timezone after that need to change the format. here is the query.
SELECT DATE_FORMAT(cast( CONVERT_TZ(`leads`.`221`,
'UTC',
'America/Los_Angeles') as datetime), '%b %d %Y %h:%i %p')
from leads order by DATE_FORMAT(cast( CONVERT_TZ(`leads`.`221`,
'UTC',
'America/Los_Angeles') as datetime), '%b %d %Y %h:%i %p') desc.
Here timezone covert, date format everything working fine. but while sorting this field it works like alphabetical ordering not like date ordering.
I know we need to remove extra mysql functions from order by field. then only it works when we give like
`order by `leads`.`221` desc`
But I cannot able to give like this. because it is created by cakephp framework. I cannot able override.
Please give a solution.
Using AS in your query will let you order by the operation you are doing.
SELECT DATE_FORMAT(cast( CONVERT_TZ(`leads`.`221`,
'UTC',
'America/Los_Angeles') as datetime), '%b %d %Y %h:%i %p')
as `new_date` from `leads` order by `new_date` DESC;
generally when i make a small select I would do something like this
SELECT id,DATE_FORMAT(updated,'%M %e, %Y %l:%i %p'),title from tablename;
and that would give me a date format that I like.
Now I have an instance where I have a select where I don't want to specify every column because I know I want them all. However I also need to format dates the way i did in the previous example.
is there a way to format say just the updated column and all other feilds and still keep there order ?
i tried this
SELECT *,DATE_FORMAT(updated,'%M %e, %Y %l:%i %p') as updated from tablename
but that doesn't seem to be doing anyhting
I did not tested but you could try with this
SELECT t.*,DATE_FORMAT(updated,'%M %e, %Y %l:%i %p')
as updated from tablename as t;