MySQL convert date string to Unix timestamp - mysql

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

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

Change DateTime string into Date

I have a DateTime column, which (because of some reasons) has changed my DateTime values from DateTime to Strings. Now I want to change them back to Date only. How to do this?
e.g my date column has values like: "Sep 2, 2019 8:00:00 PM" (in string format)
select str_to_date('Date','%d-%m-%Y') as Date
from table
This code gives blank cells as a result
Your date's format is: '%b %e, %Y %h:%i:%s %p'.
Read more about the parameters you can use in str_to_date() here.
So do the conversion like this:
select str_to_date(
'Sep 2, 2019 8:00:00 PM',
'%b %e, %Y %h:%i:%s %p'
) as Date
See the demo.
Have you tried using the CONVERT function?
SELECT CONVERT(table.Date, DATE) AS Date FROM table;

Converting dates in 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

How to convert the UNIX TIME into SQL datetime format

select COUNT(DISTINCT devices) AS "Devices" from measure_tab where
measure_tab.time >= 1375243200 and
measure_tab.time < 1375315200;
The output of the above sql query gives a table with a column named "Devices" with number of devices. I want the time which is one of the attributes of measure_tab should also get displayed in another column of the output table with the UNIX TIME which is 1375243200 in this query converted into the SQL datetime format which is Thu Aug 1 00:00:00 UTC 2013.
Can someone help me out?
Thanks
You can use function as below:
select FROM_UNIXTIME(UNIX_TIMESTAMP(),'%a %b %d %H:%i:%s UTC %Y');
output will be:
'Wed Feb 05 05:36:16 UTC 2014'
In your query
select COUNT(DISTINCT devices) AS "Devices",
FROM_UNIXTIME(measure_tab.time,'%a %b %d %H:%i:%s UTC %Y') as d from measure_tab where
measure_tab.time >= 1375243200 and
measure_tab.time < 1375315200;
For more info you can check documentation: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_from-unixtime
You can see sql fiddle:http://sqlfiddle.com/#!2/a2581/20357
In Mysql you can use from_unixtime() function to convert unix timestamp to Date:
select COUNT(DISTINCT devices) AS "Devices" from measure_tab where
measure_tab.time >= from_unixtime(1375243200) and
measure_tab.time < from_unixtime(1375315200);
you could use FROM_UNIXTIME inside DATE_FORMAT, but luckily,
FROM_UNIXTIME also accepts a format string, so you could just use it
by itself
Like this
SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(),'%Y %D %M %h:%i:%s %x')
DATE_FORMAT(NOW(),'%b %d %Y %h:%i %p')
DATE_FORMAT(NOW(),'%m-%d-%Y')
DATE_FORMAT(NOW(),'%d %b %y')
DATE_FORMAT(NOW(),'%d %b %Y %T:%f')
As detailed in the other answers, FROM_UNIXTIME is the function you are looking for. Please be aware that this implicitly takes into account the local time zone setting on the machine running MySQL.
There's lots of useful information here:
Should MySQL have its timezone set to UTC?
if you need to find out how to check/set the time zone.

Select records where datetime is greater than the specified date

Problem
I am trying to fetch all the records from table where date_time field is greater than 'Thu, 11 Jul 2013' by running the below mentioned query. Value in the date_time field is stored in this format => Thu, 11 Jul 2013 08:29:37. Any help will be great.
Datatype of field date_time is varchar
Query
SELECT * FROM table_name
WHERE username = 'mark#example.com'
AND STR_TO_DATE(date_time, '%a, %e %b %Y %H:%i:%s') >= 'Thu, 11 Jul 2013 00:00:00';
Here is yet another great example of why you should implement date/time fields in MySQL using the date, datetime, or timestamp field types and let your application deal with how to format the date for output.
Right now you are going to need to do something like:
SELECT * FROM table_name
WHERE username = 'mark#example.com'
AND STR_TO_DATE(date_time, '%a, %e %b %Y %H:%i:%s') >= STR_TO_DATE('Thu, 11 Jul 2013 00:00:00', '%a, %e %b %Y %H:%i:%s');
This query will not be able to use any index you have on your date_time field, so the query will be very inefficient. It will need to perform a full table scan, converting the value of each row in order to make the comparison.
What you should be doing is:
SELECT * FROM table_name
WHERE username = 'mark#example.com'
AND date_time >= STR_TO_DATE('Thu, 11 Jul 2013 00:00:00', '%a, %e %b %Y %H:%i:%s');
Here if you have your field in the MySQL datetime format, you just need to convert the input to a this format for matching. Since your field data is already in this format, you will be able to utilize an index for the search.
You are trying to compare a date with a String.
The str_to_date function application is correct, but you are not comparing to a date.
The right way to do it is:
select * from yourTable
where STR_TO_DATE(date_time, '%a, %e %b %Y %H:%i:%s') >= '2013-07-11 00:00:00'
Notice that the date format is YYYY-MM-DD HH:mm:ss (which is MySQL default date format).
Of course, you can also compare to str_to_date results:
... where STR_TO_DATE(date_time, '%a, %e %b %Y %H:%i:%s') >= STR_TO_DATE('Thu, 11 Jul 2013 00:00:00', '%a, %e %b %Y %H:%i:%s')