mysql string to date - mysql

How can I convert a string/varchar like '12-Mar-2013' to date like 2013-03-12?
I tried
SELECT STR_TO_DATE('12-Mar-2013','%Y-%m-%d');
SELECT DATE_FORMAT('12-Mar-2013','%Y-%m-%d');
but both return null.
My current database version is 5.5.7-rc.

Use STR_TO_DATE.
Try
SELECT STR_TO_DATE('12-Mar-2013','%d-%M-%Y');
-> 2013-03-12

This should do the trick
SELECT DATE_FORMAT(STR_TO_DATE('12-Mar-2013','%d-%b-%Y'), '%Y-%d-%m');

Related

Can't change date format SQL

I have the current date format [DD_MM_YYY] in a column on my table and would like to change it to [YYYY_MM_DD]. Can't seem to figure out the correct syntax.
This one should do for date fields:
SELECT DATE_FORMAT(NOW(), '%Y-%m-%d');
You can use the convert function in sql.
SELECT CONVERT (data_type(length)),Date, DateFormatCode)
To convert in this format [YYYY_MM_DD] if the date format is in varchar:
Select CONVERT(varchar,column_name,23)
If you are using Exasol, you can use to_date():
select to_date(datecoll, 'DD_MM_YYYY')
I would recommend leaving this as a date. But if you want to convert this back to a string:
select to_char(to_date(datecoll, 'DD_MM_YYYY'), 'YYYY-MM-DD')

Can I add time with date in mysql?

Can I add time with date in mysql ?
( e.g. date=2017-04-05 00:00:39 and time=00:10:00
result should be 2017-04-05 00:10:39 )
I have tried
SELECT dateadd(start_time,max_bidding_time) from product;
Yes, you can use ADD_TIME() function:
SELECT ADDTIME('2017-04-05 00:00:39', '00:10:00');
yes. you can change the structure of your table date field. Select type like "datetime" in database.
SELECT
ADDTIME(start_time, DATE_FORMAT(time,'%H:%i:%s')) AS updatetime
FROM product;

MySQL how to get Month and Year with separation like date format

I have "datetime" field on my database,
but what I want is only take YEAR-MONTH e.g ("2012-02")
I've tried using function YEAR_MONTH but the output without "-" which is similar like "201202".
Is there any way I could make that select?
You can use the date_format function to specify how to format your columns:
SELECT DATE_FORMAT(my_datetime_field, '%Y-%m')
FROM my_table
You want to give False in second Param in Select
select("DATE_FORMAT(now(), '%Y-%m') AS dated_now", FALSE);
Try this:
SELECT DATE_FORMAT(now(),'%Y-%m');
Try this
SELECT DATE_FORMAT(now(),'%Y %m');
You can also check the below link
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

Converting date from the column to the unix_timestamp

I got date, stored in MySQL column like this: 09-11-2012 (format is DD/MM/YYYY), but when I do:
SELECT UNIX_TIMESTAMP(t.date) FROM table t;
It does returns 0 for every single row.
How can I convert this format date to the unix timestamp? I would like to convert this date just like the PHP's strtotime is doing it.
I guest t.date is a string right? try converting it to DATE first using STR_TO_DATE
SELECT UNIX_TIMESTAMP(STR_TO_DATE(t.date, '%d-%m-%Y'))
FROM table t;
SQLFiddle Demo

MySQL: Convert INT to DATETIME

I have a UNIX-type timestamp stored in an INT column in MySQL. What is the proper way to retrieve this as a MySQL DATETIME?
(I found the answer when re-scanning the MySQL Date functions, but didn't see the answer on SO. Figured it should be here.)
FROM_UNIXTIME()
select from_unixtime(column,'%Y-%m-%d') from myTable;
SELECT FROM_UNIXTIME(mycolumn)
FROM mytable
The function STR_TO_DATE(COLUMN, '%input_format') can do it, you only have to specify the input format.
Example : to convert p052011
SELECT STR_TO_DATE('p052011','p%m%Y') FROM your_table;
The result : 2011-05-00
This works for sure.
select from_unixtime(column) from table