Converting dates in MySQL - mysql

I have a column in a MySQL database that has dates in the form:
Tue Oct 25 2016. I am trying to get it in the form 10/25/2016.
I did some research and tried this:
SELECT DATE_FORMAT(Date, '%d/%m/%Y') FROM table;
But it is returning null
Any help would be greatly appreciated.

Firstly, you will need to convert your date string to MySQL date format ('YYYY-MM-DD'), using STR_TO_DATE function. To convert from string, we have to specify the current format of the date string. In your case, it is '%a %b %d %Y'. Note that the % character is required before format specifier characters.
Details:
%a Abbreviated weekday name (Sun to Sat)
%b Abbreviated month name (Jan to Dec)
%d Day of the month as a numeric value (01 to 31)
%Y Year as a numeric, 4-digit value
Now, you can utilize DATE_FORMAT function to convert the MySQL date into the desired date string format. In your case, it will be: '%m/%d/%Y'
Details:
%d Day of the month as a numeric value (01 to 31)
%m Month name as a numeric value (00 to 12)
%Y Year as a numeric, 4-digit value
Try the following query:
SELECT DATE_FORMAT(STR_TO_DATE(Date, '%a %b %d %Y'), '%m/%d/%Y')
FROM table;
Complete list of available format specifiers can be seen at: https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-format

Related

convert full date string to date mysql

I have database which contains string like this
22 Jan 2019 11:03
I would like to convert this string to date so I apply this query
select DATE_FORMAT(STR_TO_DATE('22 Jan 2019 11:03','%d-%m-%Y') ,'%d-%m-%Y');
but I get a null result
All you have to do is change small letter m to big letter M in your str_to_date function.
select STR_TO_DATE('22 Jan 2019 11:03','%d %M %Y');
so the final query would be:
select DATE_FORMAT(STR_TO_DATE('22 Jan 2019 11:03','%d %M %Y') ,'%d-%m-%Y');
Here is a demo
SELECT STR_TO_DATE('22 Jan 2019 11:03','%d,%m,%Y');
Basically you first need to understand the values to pass in STR_TO_DATE() and DATE_FORMAT() functions
STR_TO_DATE(my date string , current format of my date string)
Now what you are not doing right is that in STR_TO_DATE() you are passing the format '%d-%m-%Y' , this format says that the input string has hyphen separated date month and year values which is not true.
Now in your case the actual format of your date string is the following
'%d %M %Y %h:%i'
STR_TO_DATE('22 Jan 2019 11:03','%d %M %Y %h:%i')
%d - day
%M - month
%Y - year
%h - hour
%i - minute
For more info on formats click
Now that we have a complete valid value from string to date including hour and minutes, we can convert this date into any of our required format using correct parameters
SELECT DATE_FORMAT(STR_TO_DATE('22 Jan 2019 11:03','%d %M %Y %h:%i') ,'%d-%m-%y %h:%i');
You can try out various examples here
https://www.mysqltutorial.org/tryit/query/mysql-str_to_date/#1

Inserting date with month name in datetime field column

I am trying to insert a date & time value into table
Here is my query
Insert INTO tableName(attDate) VALUES ("22-Sep-2019 19:28:10")
Here attDate column is having type datetime, i have tried above query using DATE_FORMAT function but no luck. And also i am not suppose to use the date value in a variable. Will anybody guide me to proper way.
You can use Str_To_Date() function to convert a datetime value to MySQL datetime format (YYYY-MM-DD HH:MM:SS)
Insert INTO tableName(attDate)
VALUES (STR_TO_DATE('22-Sep-2019 19:28:10', '%d-%b-%Y %T'))
Details:
%d Day of the month as a numeric value (01 to 31)
%b Abbreviated month name (Jan to Dec)
%Y Year as a numeric, 4-digit value
%T Time in 24 hour format (hh:mm:ss)

How to convert and use varchar data type as a datetime in mysql

Hello all,
This is the format of my my-sql data type "rdate".
Apr 1 2011 01:13:00:000PM
I want to use the order by rdate and i can't make it right order as the data type of rdate is varchar, So i want to convert it to date time , But no success.
I am trying to use date_format(str_to_date(rdate, '%m/%d/%Y'), '%Y%m');
Thanks
Mypixel
Try doing:
ORDER BY str_to_date(rdate,'%M %d %Y %h:%i:%s')
From the docs:
Your Date is in the Following format:
%M Month name (January..December)
%d Day of the month, numeric (00..31)
%Y Year, numeric, four digits
...
You have to tell str_to_date the format that your string is in. This means the way the specific parts of the date are displayed, spaces, etc.
sqlfiddle demo
In your str_to_date function call, you need to specify what the format IS, not what you want it to be. Try this:
str_to_date(rdate, '%M %d %Y %h:%i:%s'));
UPDATE table SET rdate=str_to_date(rdate,'%M %d %Y %h:%i:%s')
Just convert your column for good to datetime.

epoch to date on mysql

I've tried using
FROM_UNIXTIME(`date`)
and got a yyyy/mm/dd hour
how can I reverse it, that is have it as
hh:mm:secs mm/dd/yyyy
Thanks
Add specifiers to FROM_UNIXTIME or use DATE_FORMAT:
Specifiers
%T Time, 24-hour (hh:mm:ss)
%m Month, numeric (00..12)
%d Day of the month, numeric (00..31)
%Y Year, numeric, four digits
SELECT DATE_FORMAT(FROM_UNIXTIME(`date`), '%T %m/%d/%Y)
Or
SELECT FROM_UNIXTIME(`date`, '%T %m/%d/%Y')
Use DATE_FORMAT
DATE_FORMAT(FROM_UNIXTIME(`date`), "%T %m/%d/%Y")
This will return hh:mm:ss dd/mm/YYYY from unix time stored in database.
you have to define your custom date/time format. please have a look at:
http://dev.mysql.com/doc/refman/4.1/en/date-and-time-functions.html#function_from-unixtime
SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(), '%h:%i:%s %M/%D/%Y');

MySQL convert date string to Unix timestamp

How do I convert the following format to unix timestamp?
Apr 15 2012 12:00AM
The format I get from DB seems to have AM at the end.
I've tried using the following but it did not work:
CONVERT(DATETIME, Sales.SalesDate, 103) AS DTSALESDATE,
CONVERT(TIMESTAMP, Sales.SalesDate, 103) AS TSSALESDATE
where Sales.SalesDate value is Apr 15 2012 12:00AM
Here's an example of how to convert DATETIME to UNIX timestamp:
SELECT UNIX_TIMESTAMP(STR_TO_DATE('Apr 15 2012 12:00AM', '%M %d %Y %h:%i%p'))
Here's an example of how to change date format:
SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(STR_TO_DATE('Apr 15 2012 12:00AM', '%M %d %Y %h:%i%p')),'%m-%d-%Y %h:%i:%p')
Documentation: UNIX_TIMESTAMP, FROM_UNIXTIME
You will certainly have to use both STR_TO_DATE to convert your date to a MySQL standard date format, and UNIX_TIMESTAMP to get the timestamp from it.
Given the format of your date, something like
UNIX_TIMESTAMP(STR_TO_DATE(Sales.SalesDate, '%M %e %Y %h:%i%p'))
Will gives you a valid timestamp. Look the STR_TO_DATE documentation to have more information on the format string.
If you want to create a timestamp as returned by java's Date.getTime() you should multiply by 1000.
SELECT UNIX_TIMESTAMP(STR_TO_DATE('Apr 15 2012 12:00AM', '%M %d %Y %h:%i%p'))*1000
Now for a more standard date format use:
SELECT UNIX_TIMESTAMP(STR_TO_DATE('2022-12-14 20:58:00', '%Y-%m-%d %H:%i:%s'))*1000
From http://www.epochconverter.com/
SELECT DATEDIFF(s, '1970-01-01 00:00:00', GETUTCDATE())
My bad, SELECT unix_timestamp(time) Time format: YYYY-MM-DD HH:MM:SS or YYMMDD or YYYYMMDD. More on using timestamps with MySQL:
http://www.epochconverter.com/programming/mysql-from-unixtime.php