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;
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.
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 ;