Mysql Select statement to and convert timestamp on the fly - mysql

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

Related

Convert time into "h:mm:a" in mysql

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 current time with 26/09/2018 04:10 AM

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.

Converting Varchar (1/24/15 1:30:00 PM) to date format (2015-01-24 13:30:00)

I have this date format : 1/24/15 1:30:00 PM(Varchar), but I want to convert it changing into datetime format (2015-02-24 13:30:00) using query, how can I do it?
SELECT STR_TO_DATE('1/24/15 1:30:00 PM', '%m/%d/%y %h:%i:%s %p')
FROM yourTable
If you don't like the format of the output this gives you, you can force it to what you want by wrapping STR_TO_DATE() in a call to DATE_FORMAT():
SELECT DATE_FORMAT(STR_TO_DATE('1/24/15 1:30:00 PM', '%m/%d/%y %h:%i:%s %p'),
'%Y-%m-%d %h:%i:%s')
FROM yourTable

how to convert varchar to datetime format in mysql

I am trying to convert varchar to date time
This is what I have now, and it is not working for me. I always get the have value
STR_TO_DATE(REPLACE(LEFT('5/16/2011 20:14 PM', LOCATE('M' , '5/16/2011 20:14 PM')-3), '/',','),'%m-%d-%Y %T')
This following code returns 5,16,2011 20:14
select REPLACE(LEFT('5/16/2011 20:14 PM', LOCATE('M' , '5/16/2011 20:14 PM')-3), '/',',')
my current output is emply string now. it should be 2011-05-16 20:14:00
How can i get this to work?
thanks
If your varchar is like this:
5/16/2011 20:14 PM
you can convert it to datetime using this:
SELECT STR_TO_DATE('5/16/2011 20:14 PM', '%c/%e/%Y %H:%i')
or this to format it like you want:
SELECT DATE_FORMAT(STR_TO_DATE('5/16/2011 20:14 PM', '%c/%e/%Y %H:%i'), '%Y-%m-%d %H:%m:%s')

write a query that converts a timestamp to a date and add hours

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 ;