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.
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.
select COUNT(DISTINCT devices) AS "Devices" from measure_tab where
measure_tab.time >= 1375243200 and
measure_tab.time < 1375315200;
The output of the above sql query gives a table with a column named "Devices" with number of devices. I want the time which is one of the attributes of measure_tab should also get displayed in another column of the output table with the UNIX TIME which is 1375243200 in this query converted into the SQL datetime format which is Thu Aug 1 00:00:00 UTC 2013.
Can someone help me out?
Thanks
You can use function as below:
select FROM_UNIXTIME(UNIX_TIMESTAMP(),'%a %b %d %H:%i:%s UTC %Y');
output will be:
'Wed Feb 05 05:36:16 UTC 2014'
In your query
select COUNT(DISTINCT devices) AS "Devices",
FROM_UNIXTIME(measure_tab.time,'%a %b %d %H:%i:%s UTC %Y') as d from measure_tab where
measure_tab.time >= 1375243200 and
measure_tab.time < 1375315200;
For more info you can check documentation: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_from-unixtime
You can see sql fiddle:http://sqlfiddle.com/#!2/a2581/20357
In Mysql you can use from_unixtime() function to convert unix timestamp to Date:
select COUNT(DISTINCT devices) AS "Devices" from measure_tab where
measure_tab.time >= from_unixtime(1375243200) and
measure_tab.time < from_unixtime(1375315200);
you could use FROM_UNIXTIME inside DATE_FORMAT, but luckily,
FROM_UNIXTIME also accepts a format string, so you could just use it
by itself
Like this
SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(),'%Y %D %M %h:%i:%s %x')
DATE_FORMAT(NOW(),'%b %d %Y %h:%i %p')
DATE_FORMAT(NOW(),'%m-%d-%Y')
DATE_FORMAT(NOW(),'%d %b %y')
DATE_FORMAT(NOW(),'%d %b %Y %T:%f')
As detailed in the other answers, FROM_UNIXTIME is the function you are looking for. Please be aware that this implicitly takes into account the local time zone setting on the machine running MySQL.
There's lots of useful information here:
Should MySQL have its timezone set to UTC?
if you need to find out how to check/set the time zone.
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;
I hope someone can help me with this. My knowledge of sql and programming is pretty rudimentary.
I've written a query that looks like this.
SELECT FROM_UNIXTIME(EventStartDate)
FROM mytable
This does what I need -converting a timestamp to a readable date, but the time is 5 hours off. I guess my timestamp is GMT and I'm in -5:00. Is there some way to write this that adds 5 hours as well as converting to a readable date?
I appreciate any help I can get with this.
Try this:
SELECT DATE_FORMAT(CAST(EventStartDate AS datetime), '%Y %M %D') AS Date FROM mytable;
SELECT DATE_FORMAT(CAST(EventStartDate AS datetime), '%Y %m %d') AS Date FROM mytable;
SELECT DATE_FORMAT(CONVERT_TZ(EventStartDate,'+00:00','-05:00'),'%Y %m %d') AS Date FROM mytable;
SELECT DATE_FORMAT(CONVERT_TZ(EventStartDate,'+00:00','-05:00'),'%Y %M %D') AS Date FROM mytable;
-- with time
SELECT DATE_FORMAT(CAST(EventStartDate AS datetime), '%Y %M %D %h:%i:%s') AS Date_time FROM mytable;
SELECT DATE_FORMAT(CAST(EventStartDate AS datetime), '%Y-%m-%d %h:%i:%s') AS Date_time FROM mytable;
SELECT DATE_FORMAT(CONVERT_TZ(EventStartDate,'+00:00','-05:00'),'%Y-%m-%d %h:%i:%s') AS Date_time FROM mytable;
SELECT DATE_FORMAT(CONVERT_TZ(EventStartDate,'+00:00','-05:00'),'%Y %M %D %h:%i:%s') AS Date_time FROM mytable;
A readable format is somewhat unspecific so I have tried some formats that your might like. You might even use a different language so this is very difficult to answer. For example you could use SET lc_time_names = 'es_ES';SELECT DATE_FORMAT(CONVERT_TZ(EventStartDate,'+00:00','-05:00'),'%Y %M %D') FROM mytable; to get your date in spanish (PHP/MySQLi: SET lc_time_names and DATE_FORMAT() into a mysqli query?). Or if you use PHP you could use it to reformat in your own language.
SQL FIDDLE DEMO
In the you title you say you need to convert timestamp into a date. However, you use from_unixtime so I have the impression that you might be using VARCHAR or INT as you column type. In that case you should try this:
SELECT CONVERT_TZ(FROM_UNIXTIME(EventStartDate_INT),'+00:00','-05:00') AS Date_time FROM mytable;
SELECT CONVERT_TZ(FROM_UNIXTIME(EventStartDate_VARCHAR),'+00:00','-05:00') AS Date_time FROM mytable;
SELECT date_format(CONVERT_TZ(FROM_UNIXTIME(EventStartDate_INT),'+00:00','-05:00'),'%Y-%m-%d %h:%i:%s') AS Date_time FROM mytable;
SELECT date_format(CONVERT_TZ(FROM_UNIXTIME(EventStartDate_VARCHAR),'+00:00','-05:00'), '%Y-%m-%d %h:%i:%s') AS Date_time FROM mytable;
SQL FIDDLE DEMO
According to this page you should be able to add 5 hours to a date like so:
SELECT FROM_UNIXTIME(EventStartDate) + INTERVAL 5 HOUR FROM mytable ;
or
SELECT DATE_ADD(FROM_UNIXTIME(EventStartDate), INTERVAL 5 HOUR) FROM mytable ;
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;