I use the below method to convert Unix Time stored in the hltime column of a MySQL DB to a readable format - "YYYY DD MM hh:mm:ss"
For Example:
UTC date and time stored format in MySQL DB: 1500473820
MySQL Function used to convert to readable format:
from_unixtime(hltime,'%Y %D %M %h:%i:%s')
Result: "2017 19th July 07:47:00"
The date and time stored in the hltime column are in GMT. Thus how could I use the from_unixtime() function (or any other function) to convert the values shown into IST (Indian Standard Time).
I have tried using convert_tz(from_unixtime(hltime,'%Y-%D-%M %h:%i:%s'),'+00:00','+06:00'), but I get NULL values.
Date time format used to convert is not correct
Change:
'%Y %D %M %h:%i:%s'
To:
'%Y-%m-%d %h:%i:%s'
And it should be working
Example:
SELECT #ut:= 1500473820 AS ut
, #ts:=FROM_UNIXTIME(#ut,'%Y-%m-%d %h:%i:%s') AS gmt
, CONVERT_TZ(CAST( #ts AS DATETIME ),'+00:00','+05:30') AS ist;
Result:
---------- ------------------- ---------------------
ut gmt ist
---------- ------------------- ---------------------
1500473820 2017-07-19 07:47:00 2017-07-19 13:17:00
Related
I have a table which pulls data from RSS Feeds and saves. The RSS feed format of representing publication date and time seems difficult to be converted into a MYSQL DATE(TIME) format. It comes as Thu, 14 Jan 2021 17:11:05 +0000 for example. So when I use STR_TO_DATE to try converting it, it returns null since STR_TO_DATE doesn't go with such formats. Also, I can't even trim parts of the string and make it work with STR_TO_DATE. Of course, other functions like UNIX_TIMESTAMP won't work with that kind of string. How can I convert such a complex string to a MYSQL date format? I'm trying to do something like:
SELECT * FROM tbl WHERE 1 ORDER BY STR_TO_DATE('Thu, 14 Jan 2021 17:11:05 +0000', '%d-%m-%Y')
You need to complete your date format.
You can truncate the parts you don't want afterwards.
As an alternative you could use substring before convertion.
select str_to_date(
'Thu, 14 Jan 2022 17:11:05 +0000',
'%a, %d %b %Y %H:%i:%s +%f') formatted_date
| formatted_date |
| :------------------------- |
| 2022-01-14 17:11:05.000000 |
db<>fiddle here
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
How to query data in specific time like: I want to select all records stored between 1 pm and 5 pm during a month , when the time stored in datetime column in Unix timestamp format like so "1403830861".
The FROM_UNIXTIME function can convert a unix timestamp to a date. The %k format (hour represented as an in 0..23) seems to fit the bill nicely. The month could be easily extracted in the same fashion, using the %m format and the year using %Y. E.g., the following query would return only results from November 2014:
SELECT *
FROM my_table
WHERE FROM_UNIXTIME (timestamp_column, '%k') BETWEEN 13 AND 17 AND
FROM_UNIXTIME (timestamp_column, '%m') = 11 AND
FROM_UNIXTIME (timestamp_column, '%Y') = 2014
I want to convert a field that represents a date-time but is currently a VARCHAR to a DATETIME field.
Currently the representation looks like: 'Sun May 20 01:04:39 +0000 2012'
I want to do this operation in a query.
Use STR_TO_DATE
SELECT STR_TO_DATE('Sun May 20 01:04:39 +0000 2012', '%a %M %d %H:%i:%S +0000 %Y')
SQLFiddle Demo
Date Format
Use the DATE function.
This does the job and can be used inside queries.
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