How MySQL DateTime works - mysql

I need to manipulate my database. I'm stuck at datetime field. For example, it is 08-Apr-2015 21:13:49 and SQL translate it into 1428527629. How SQL translate the d-M-Y H:i:s format?

1428527629 unix timestamp, stands for seconds since Jan 01 1970. (UTC)
You have several SQL functions to work with dates, such as: FROM_UNIXTIME(1428527629)

Related

How to get Time from Mysql Date Format DD/MM/YYYY?

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.

MySql Convert to UTC

I am trying to convert CET date to UTC Format in MySql. Whats wrong with this?
SELECT
CONVERT_TZ('2018-01-26T06:15:00+01:00', ##session.time_zone, '+00:00');
Expected:
2018-01-26T05:15:00Z
Actual:
2018-01-26T06:15:00Z
I guess it is not taking the +01:00 component
MySQL's date format doesn't have a timezone. It is interpreting your '2018-01-26T06:15:00+01:00' as '2018-01-26 06:15:00'.
The 1-st parameter is the time, the 2-nd parameter is the time zone for the 1-st and the 3-rd is the result time zone.
SELECT
CONVERT_TZ('2018-01-26T06:15:00', '+01:00', '+00:00');

Convert timestamp (int) to smalldatetime (t-sql)

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.

How to get a date in My SQL in MM/dd/yyyy hh:mm:ss.S edt/est format

Is there a way to get the date to display in My sql in the formats shown in the following examples?
"02/23/2012 13:46:20.5 EST"
OR
"03/23/2012 13:46:20.5 EDT"
Basically I am looking for something that can include time zone in the formatted string
Store all times in UTC (Zulu time, aka GMT) and then use CONVERT_TZ in MySQL to convert to whatever time zone you want.

my str_to_date in mysql is returning null

I ran this statement in mysql and it returns null on every row:
SELECT STR_TO_DATE('Fri, 22 Jun 2012 03:35:24 +0000', '%a, %e %b %Y %H:%i:%s %%%%%%%%%%') FROm t2;
I don't know the right specifier for the +0000. What should it be?
The +0000 is a time zone offset.
mySQL doesn't understand the concept of time zones, and apparently neither does STR_TO_DATE: the list of date/time format placeholders doesn't mention time zones at all.
If you expect dates from varying time zones, this doesn't seem solvable with the help of mySQL only. You would have to preprocess the date elsewhere to turn it into one that is always UTC (or your local time zone, whatever applies), or if it already is always UTC, ManseUK's suggestion (adding +0000 to your format string so mySQL ignores it) should work.