In postgres database (9.4) i have data saved as JSON object. Including some datetime elements. In order to do time comparision in sql query I have to convert this string to timestamp. I have tried this query:
SELECT to_timestamp('2015-05-22T10:56:04.949Z','YYYY-MM-DD HH:MI:SS.MS');
-- dummy query just to test converting
OUTPUT: 2015-05-22 10:56:04.949+03
It works basically fine, except, I cant figure out how to include timezone parameter there. This string is in UTC time 10:56AM, if I run this query, I'll get 10:56 in +03 timezone (+03 is my local timezone). According to Postgres documentation, TZ should be added to format string to get timezone as well, but it does not work.
How to format this string so that it includes also timezone from initial date-time string ?
As far as I know it's not possible to parse timezone from string.
However if you know your timestamp is always in UTC timezone you can use:
SELECT to_timestamp('2015-05-22T10:56:04.949Z','YYYY-MM-DD HH:MI:SS.MS')::timestamp at time zone '00:00'
Related
Is there a way to convert a string such as "-1 week" or "-5 minutes" into a datetime value in MySQL similar to php's extremely convenient strtotime() function?
I have a table that stores a human-readable time interval (such as "2 minutes") in one column and a datetime in another column.
I would like to select the rows where more than the amount of time specified in interval has elapsed since datetime.
MySQL doesn't have an equivalent of PHP's strtotime() in the sense that there is nothing that will automatically attempt to parse and determine the format of a date string using by assuming multiple formats.
What it does have is STR_TO_DATE(str,format) which requires you specify the format of your date, time or date + time string. It is the equivalent of PHP's date_create_from_format(format, str) function (though the format of the format parameter are different).
Here are some examples given from the MySQL documentation. They show a date being passed along with the format string that lets it know how the date string is to be interpreted:
SELECT STR_TO_DATE('01,5,2013','%d,%m,%Y');
SELECT STR_TO_DATE('May 1, 2013','%M %d,%Y');
Alternatively, you can cast a string to a date, time or datetime type, but they require a specific format (YYYY-MM-DD hh:mm:ss.fraction) for it to work:
SELECT CAST("2019-11-21" AS DATE);
If you deviate too far from that format it will make a few assumptions but could produce an incorrect date.
Hi I'm writing queries for MySQL, and now my database has a column containing the timestamp in this format: 7/14/2015 7:57:49 AM, but I need to use the DATEDIFF function, so how can I convert the timestamp into the format like: 2015-7-14 (or 2015-07-14, I'm not sure which one is correct; just the date)?
This should convert your string to just the date in a date format, then you can use DATEDIFF on the date fields in question:
SELECT STR_TO_DATE(LEFT(t,LOCATE(' ',t) - 1), '%m/%d/%Y') FROM my_table;
The LEFT function will take the substring to the left of the space, which is just your date, then STR_TO_DATE will convert that substring to a date the system can use.
(Not knowing your field and table names, I used t and my_table.)
You don't need to. The way MySQL displays timestamps has nothing to do with the way they're stored internally; as long as it's TYPE TIMESTAMP or some compatible type, the DATEDIFF() function will know what to do with it.
TIMESTAMPs are actually stored as a really huge integer representing (I think) milliseconds from Midnight UTC, January 1st, 1970. The display format is determined by a system global variable, and has nothing to do with the actual value.
Converting from a string to a DATETIME or TIMESTAMP is actually also fairly straightforward using the STR_TO_DATE() function; in your case the format string would be something like
STR_TO_DATE('%c/%e/%Y %l:%i:%s %p', datecol)
although you might have to experiment a bit to make it work reliably.
I can't figure out how to get SQLAlchemy to return times in UTC. I have a model with a datetime field with timezone=True. I know that Postgres stores all timezones internally in UTC, but when the date comes back out it's shifted to the timezone I'm. The python datetime object has it's tzinfo set to psycopg2.tz.FixedOffsetTimezone(offset=-240, name=None) when SQLAlchemy retrieves it from Postgres. I know I could just convert to UTC once I get the date out of SQLalchemy, but that just sounds tedious since I'll be using datetimes everywhere in my code.
Are you sure the postgres database doesn't have a timezone set? Log in to postgres and run:
test_db=> show timezone;
TimeZone
---------
US/Central
This will cause all times output from SQLAlchemy to have central timezone. You want to make sure this value is something like this instead:
test_db=> show timezone;
TimeZone
----------
UTC
By default, SQLAlchemy will return times in UTC. You should remove timezone=True (or set it to False).
I need to convert varchar string data to time format in mysql.
For example,
I have a varchar column in table which stores time. The accepted values should be like 9:30 AM or 1200 PM. But currently it has either blank values or it has values like 9.30am or 12:00
There are many records like this, so cannot update manually.
Ithere any work around or function or procedure to do so?
please help.
Thanks
You can use the STR_TO_DATE() MySQL function to convert any string to a date.
Additionally you can use TIME() to extract the time portion of a datetime. A combination of both function is used to convert an arbitrary date string to a datetime and then you can extract the time portion from it as a valid MySQL TIME.
By default MySQL functions follow standard format but custom format can be specified and if your values don't use the international formats you'll need to check with the documentation and provide the format your system is using.
For my one of project, I have to convert a mysql database into Mongodb.But i confused with what i can use the data type instead of Timestamp data type in Mysql.please can i get to know, what is the data type i can use for mongodb.(I'm creating Mongodb by using php).
Date will be the most appropriate
BSON Date is a 64-bit integer that represents the number of
milliseconds since the Unix epoch (Jan 1, 1970). The official BSON
specification refers to the BSON Date type as the UTC datetime.
You can get number of seconds from Epoch in MySQL using
SELECT UNIX_TIMESTAMP(yourTimeStampColumn) AS secondsFromEopch FROM yourTable;
You don't need to care about timezones here, because MySQL TIMESTAMP is always internally stored as UTC. You can then use this value in PHP to create a MongoDate object, which you will use to create a BSON document.
$mongoDate = new MongoDate($secondsFromEpoch);