Convert SQL string field into unix timestamp - mysql

I have a field, 'mydate' in my database which displays the date as a string like this
'8/1/2015 12:35:22 PM'
I am trying to convert this into a unix timestamp i have tried
cast(UNIX_TIMESTAMP('mydate',"yyyy-MM-dd HH:mm:ss.SSSS")) AS new_date
This has resulted in the query not completing completely. How can i convert this 'mydate' field into a unix timestamp.

Try something like this:
UNIX_TIMESTAMP(STR_TO_DATE(mydate, '%M %e %Y %h:%i%p'))

You can simply check in mysql string format -
SELECT STR_TO_DATE('8/1/2015 11:35:22 PM','%d/%m/%Y %h:%i:%s %p');

Related

MySQL comparing date string into date column

I have this query
SELECT * FROM tracklogs.sms_outbound
WHERE gsmno = 'rk4#*******.com.ph'
AND cdate > cast('2013/11/14 09:44:48 PM' as datetime)
where cdate format is in %Y-%m-%d %h:%i:%s %p.
I have tried converting the date into that format then cast it as datetime but still doesn't working.
Use STR_TO_DATE() to correctly convert the datetime literal you have provided to a proper DATETIME value. It seems that your cdate column is a char() or varchar() column. So you will also need to convert that to DATETIME to compare it.
What you need is this:
That works like this (http://sqlfiddle.com/#!2/d41d8/48741/0)
STR_TO_DATE(cdate, '%Y-%m-%d %h:%i:%s %p') >
STR_TO_DATE('2013/11/14 09:44:48 PM', '%Y/%m/%d %h:%i:%s %p')
Converting these strings to DATETIME data items ensures that the comparison handles both the date and the time correctly. See this fiddle (http://sqlfiddle.com/#!2/d41d8/48743/0)
But, you should consider changing your cdate item to a DATETIME, because then you'll be able to index it and speed up your search.
SELECT * FROM tracklogs.sms_outbound
WHERE gsmno = 'rk4#*******.com.ph'
AND UNIX_TIMESTAMP(str_to_date(cdate,'%Y-%m-%d %H:%i:%s')) > UNIX_TIMESTAMP('2013-11-14 09:44:48')

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.

How to convert date with time in mysql

I have a table with type DATE. How do i convert the below date to be able to insert it into the table.
15-JUL-12 3:09pm
I tried the following, but it keeps saying Incorrect date time values.
STR_TO_DATE('15-JUL-12 3:09pm', '%d-%m-%y %h:%i%p')
STR_TO_DATE() is the correct function to use, but there is a problem with the format string.
Use %b for abbreviated month name (or generally %M for month names) in your format string. E.g.:
STR_TO_DATE('15-JUL-12 3:09pm', '%d-%b-%y %h:%i%p')
See:
http://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html#function_date-format
for documentation of format symbols

Convert a obscure? date format in a sql query to a human readable one

I've got a date format in a bigint field in this format "20130314123743" - YYYYMMDDHHMMSS and i need to do a mysql query on it and get it back to the user in something like yyyy-mm-dd hh:mm:ss.
Is there a native mysql function that will take that date format and return it to something human readable?
One approach:
select cast(cast(bigintdateval as char(14)) as datetime)
SQLFiddle here.
To convert to a datetime you can use MySQL function FROM_UNIXTIME
FROM_UNIXTIME (unix_timestamp, [format ])
SELECT FROM_UNIXTIME(20130314123743, '%Y-%m-%d %h:%i:%s');
Exemple;
SELECT FROM_UNIXTIME(birthDay/100000, "%Y-%m-%d %H:%i:%s") AS date
FROM person;
source
FROM_UNIXTIME
Hope that help

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