Get time from Timestamp stored as varchar - mysql

I have timestamps in varchar(20) like '01-11-2012 11:36:53.122'
If I get only date by
SELECT STR_TO_DATE("01-11-2012 11:36:53.122", '%d-%m-%Y')
it gives this output: "2012-11-01".
But by this
SELECT STR_TO_DATE("01-11-2012 11:36:53.122", '%H:%i:%s')
it gives the output "null" instead of 11:36:53.
What is query to get only the time?

give this a try
DATE_FORMAT(STR_TO_DATE(`column`, '%d-%m-%Y %H:%i:%s'), '%H:%i:%s')
SQLFiddle Demo
Other sources,
STR_TO_DATE
DATE_FORMAT

SELECT date_format(STR_TO_DATE("01-11-2012 11:36:53.122", '%d-%m-%Y %H:%i:%s.%f'), '%H:%i:%s')
SQLFiddle demo

Change your field format to datetime.
That's the only proper way to store datetime values.

Related

Format a date in mysql

To get a date I can do:
select date(now())
And it will give me:
2018-11-30
How would I format it as:
YYYYMMDD
So it gives me:
20181130
Is there a better way than:
select replace(date(now()), '-','')
SELECT DATE_FORMAT(NOW(), '%Y%m%d')
Manual:
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format

MySQL Filter Records Set Using DD-MM-YYYY Date Format

I'm a bit confused on how to filter records by date formats.
I have date column of date(yyyy-mm-dd) data type in date table.
Ex:
date
-----
2017-01-29
2017-01-30
I'm want to change the format (dd-mm-yyyy). I'm using this code
SELECT
DATE_FORMAT(`date`, '%d-%m-%Y') as date
FROM
dim_date;
date
-----
29-01-2017
30-01-2017
I want to filter the records with the(dd-mm-yyyy) format. So I tried this code.
SELECT
DATE_FORMAT(`date`, '%d-%m-%Y') date
FROM
dim_date
WHERE DATE_FORMAT(`date`, '%d-%m-%Y') BETWEEN '20-04-2015' AND '06-09-2017';
Results
Nothing
But if I try to filter with the original format (yyyy-mm-dd) It Works.
SELECT DATE_FORMAT(`date`, '%d-%m-%Y') date
FROM dim_date
WHERE DATE_FORMAT(`date`, '%Y-%m-%d') BETWEEN '2015-04-20' AND '2017-01-07';
Why is this weird behavior in Mysql? Am I missing something here?
I also tried with this format DATE_FORMAT(date, '%d-%c-%Y') , DATE_FORMAT(date, '%d-%l-%Y') & DATE_FORMAT(date, '%e-%l-%Y')
No happy face, Please let me known.
Thanks
Max
with your query you convert the date in string and then you are comparing values using between in wrong order ('20' > '06' ) so don't work.
Between require first the min value and second the max value.
If you are working with date you sould convert the string date
so the where between work correcly and avoid the string behavior is filter
SELECT date
FROM dim_date
WHERE date BETWEEN str_to_dat('20-04-2015', '%d-%m-%Y')
AND str_to_date('06-09-2017', '%d-%,-%Y');

Retrieve time from MySQL as HH:MM format

I used DATETIME datatype to store date and time in my Database. It saved date as
YY-MM-DD HH:MM:SS (2012-11-06 10:45:48)
I can retrieve the time from database as HH:MM:SS format but I want to retrieve it in HH:MM format.
How can I do this?
Check the DATE_FORMAT Function.
SELECT DATE_FORMAT(date_column, '%H:%i') FROM your_table
You will want to use DATE_FORMAT():
select date_format(yourDateColumn, '%h:%i') yourTime
from yourtable
See SQL Fiddle with Demo
SELECT DATE_FORMAT( NOW() , '%H:%i' ) AS time_column

mysql order by closest date/hour before or after

I have a mysql table that has a column that stores dates but isn't in the date format, it's a varchar.
The column is called data_hora and have dates in the dd/mm/yy format, example: 06/09/2012 15:00, so I had to convert to date format in mysql query.
And I need to get the closest date and hour before or after the current time, I came up with the following code, but for some reason it seems to get only closest date but not hour, weird?!?!
SELECT str_to_date(data_hora, '%d/%m/%Y %H:%i') AS data_hora
FROM requisicoes
ORDER BY abs(DATE_FORMAT(NOW(),'%d/%m/%Y %H:%i') - data_hora) LIMIT 1
Help :(
try this:
Your ORDER BY Clause has to be changed
SELECT str_to_date(data_hora, '%d/%m/%Y %H:%i') AS data_hora
FROM requisicoes
ORDER BY abs(TIMEDIFF( NOW() , str_to_date(data_hora, '%d/%m/%Y %H:%i'))) LIMIT 1
You shouldn't format NOW() to a string: it is already a TIMESTAMP value; instead, take the (absolute) difference between the present UNIX_TIMESTAMP() and that of the data_hora alias for your selected STR_TO_DATE() column:
ORDER BY ABS(UNIX_TIMESTAMP() - UNIX_TIMESTAMP(data_hora))
If at all possible, I would advise altering your schema so that your data_hora column is stored as a TIMESTAMP: it will greatly improve the performance of queries of this sort.

How to convert timestamp to datetime in MySQL?

How to convert 1300464000 to 2011-03-18 16:00:00 in MySQL?
Use the FROM_UNIXTIME() function in MySQL
Remember that if you are using a framework that stores it in milliseconds (for example Java's timestamp) you have to divide by 1000 to obtain the right Unix time in seconds.
DATE_FORMAT(FROM_UNIXTIME(`orderdate`), '%Y-%m-%d %H:%i:%s') as "Date" FROM `orders`
This is the ultimate solution if the given date is in encoded format like 1300464000
To answer Janus Troelsen comment
Use UNIX_TIMESTAMP instead of TIMESTAMP
SELECT from_unixtime( UNIX_TIMESTAMP( "2011-12-01 22:01:23.048" ) )
The TIMESTAMP function returns a Date or a DateTime and not a timestamp, while UNIX_TIMESTAMP returns a unix timestamp
You can use
select from_unixtime(1300464000,"%Y-%m-%d %h %i %s") from table;
For in details description about
from_unixtime()
unix_timestamp()
SELECT from_unixtime( UNIX_TIMESTAMP(fild_with_timestamp) ) from "your_table"
This work for me