What time is it in MySQL: 1343821692?
Few aplications that I use store it in that format.
Also I have time stored like this: 2012-08-01 13:41:1 - how to convert those two time types into each another?
It is a unix timestamp:
select FROM_UNIXTIME(1343821692)
SELECT UNIX_TIMESTAMP('2012-08-01 13:41:1');
See from_unixtime and unix_timestamp
Related
I want to get time from mysql dd/mm/YYYY H:M:S format.
I have tried,
SUBSTRING_INDEX(field, 'delimiter', index)
but am looking for a better solution.
have tried, DATE_FORMAT(field, "%H:%i:%s") but it returns NULL because my date format was not native (YYYY-mm-dd)
it was 02/05/2019 19:38:27
How to get time from this above format in a better way?
NOTE: I am storing date like above.. this fetching form SQL Server
I guess you can first use STR_TO_DATE followed by CAST(... AS time). Casting instead of formatting allows you to use the result in date/time calculations.
SELECT CAST(STR_TO_DATE('02/05/2019 19:38:27', "%d/%m/%Y %H:%i:%s") AS TIME)
Ideally you should teach SQL Server to export dates in yyyy-MM-dd hh:mm:ss format.
This is how i Resolved,
TIME(STR_TO_DATE(d.in_punch, "%d/%m/%Y %H:%i:%s"))
also as per #Salman A
CAST(STR_TO_DATE('02/05/2019 19:38:27', "%d/%m/%Y %H:%i:%s") AS TIME)
this also worked.
I have a column that is the time stored as for example 0545 (5:45 am) and a date column stored as 20180801. I want to combine these into one value of 2018/08/01 05:45 am) I am doing this over an ODBC connection if that makes a difference. I can use STR_TO_DATE on the date column but I can't figure out the time column. Thanks.
Use str_to_date() on the whole thing:
select str_to_date(concat(datecol, timecol), '%Y%m%d%h%i')
I have a MySQL-Database with several rows. In that environment, I stored the current time as a Timestamp (int).
Now I need to migrate my data from a MySQL to a T-SQL-Database. I'm running SQL-Server 2008.
I checked several approaches, but couldn't come up with a way which transforms my int into a smalldatetime format.
Is there a build-in function for this? Is this even doable alone in a statement? I really don't want to write a PHP-snippet, which converts the timestamp to the desired format.
Thanks in advance
As per the documentation, smalldatetime uses the following format:
DECLARE #smalldatetime smalldatetime = '1955-12-13 12:43:10';
So, we need to convert the MySQL timestamp into date and format it in the above format to get smalldatetime. This can be done by using FROM_UNIXTIME and DATE_FORMAT functions of MySQL, e.g.:
DATE_FORMAT(FROM_UNIXTIME(timestamp_column), '%e %b %Y') AS 'smalldatetime';
Here's the SQL Fiddle.
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.
Is there a function in MySQL like in Postgres where I can convert the current timestamp to another timezone?
In Postgres, I do this like these:
SELECT now() AT TIME ZONE 'PST'
and it converts it on its own. How do I do this in MySQL? is there anyway?
Thanks
Use the CONVERT_TZ() for this.
Syntax:
CONVERT_TZ(dt,from_tz,to_tz)
CONVERT_TZ() converts a datetime value dt from the time zone given by from_tz to the time zone given by to_tz and returns the resulting value.
Try this:
SELECT CONVERT_TZ(now(),##session.time_zone,'-08:00');
Here is the reference.
now() -the current time
##session.time_zone -gives the current time zone(local)
-08:00 -required format in PST.
I actually solved! HAH!
Anyway, I used this:
SELECT CONVERT_TZ(NOW(),SUBSTRING(TIMEDIFF(NOW(), UTC_TIMESTAMP), 1, 6),'-05:00') AS est_timezone
Basically the query interprets as:
SELECT CONVERT_TZ(NOW(),'-08:00','-05:00') AS est_timezone
I finally got my current timezone which is -08:00 and will convert it to -05:00. This way, I can now proceed with my previous query to making it a VIEW.