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;
Related
I have the data stored as this in my mysql table
http://prntscr.com/tbwiiq
I am trying to get the data based upon the order by clause
SELECT * FROM cw_books order by STR_TO_DATE(BookingTime, '%a, %d %b %Y %T') desc
but it is always giving me wrong order, what the best i can do here
The format that you use is wrong.
Use this:
SELECT *
FROM cw_books
order by STR_TO_DATE(BookingTime, '%m/%d/%Y %h:%i:%s %p') desc
See the demo.
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;
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.
Hello all,
This is the format of my my-sql data type "rdate".
Apr 1 2011 01:13:00:000PM
I want to use the order by rdate and i can't make it right order as the data type of rdate is varchar, So i want to convert it to date time , But no success.
I am trying to use date_format(str_to_date(rdate, '%m/%d/%Y'), '%Y%m');
Thanks
Mypixel
Try doing:
ORDER BY str_to_date(rdate,'%M %d %Y %h:%i:%s')
From the docs:
Your Date is in the Following format:
%M Month name (January..December)
%d Day of the month, numeric (00..31)
%Y Year, numeric, four digits
...
You have to tell str_to_date the format that your string is in. This means the way the specific parts of the date are displayed, spaces, etc.
sqlfiddle demo
In your str_to_date function call, you need to specify what the format IS, not what you want it to be. Try this:
str_to_date(rdate, '%M %d %Y %h:%i:%s'));
UPDATE table SET rdate=str_to_date(rdate,'%M %d %Y %h:%i:%s')
Just convert your column for good to datetime.
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;