Mysql - Convert local date in UTC format - mysql

Good day,
I am having mysql dates stored in this format :
In my sql query I want to get these dates in UTC format.
I have tried CONVERT_TZ(dt,from_tz,to_tz)function but I cannot determine how can I get from_tz for the dates. I know that to_tz will be 'UTC' or '+0000'

Try this:
select convert_tz(`date`, replace(substring_index(`date`, ' ', -1), '00', ':00'), '+00:00')
Edit:
select convert_tz(`date`, concat(left(substring_index(`date`, ' ', -1), 3), ':', right(substring_index(`date`, ' ', -1), 2)), '+00:00')

Well, as mysql ignores TZ qualifiers in date, I suggest you do a semimanual TZ conversion
select
date_add("2017-04-18 15:15:15 +1000", interval substring("2017-04-18 15:15:15 +1000", -5, 3) hour),
date_add("2017-04-18 15:15:15 +1000", interval substring("2017-04-18 15:15:15 -1000", -5, 3) hour)
;
the result will be in UTC timezone as you expect

Related

How to format timestamp from mysql select query to exclude the unnecessary zeros in HH:MM:SS?

I have a select query:
string checkprogress = "SELECT TIMEDIFF(CURRENT_TIMESTAMP,timestamp) FROM times";
Which in return gives me a format with unnecessary leading zeros...
00:01:39 //(hours:minutes:seconds)
How can I alter this query or alter my c# code so that my string excludes all the unnecessary zeros in the timestamp?
Keep in mind that if the time reaches over 24 hours, the string will become:
1.00:01:39 //(day.hours:minutes:seconds)
the format above is completely fine since a day value is incorporated, so the format change I'm looking for is strictly for when the DAY value is not in effect (A.K.A when time is under 23:59:59).
It isn_t pretty, but it works.
SET #endDate = '2020-06-27 10:02:00';
SET #startDate = '2020-06-26 12:59:01';
SELECT
CONCAT(
IF(FLOOR(TIMESTAMPDIFF(SECOND, #startDate, #endDate) / 86400) > 0,CONCAT(FLOOR(TIMESTAMPDIFF(SECOND, #startDate, #endDate) / 86400), '.'),''),
LPAD(FLOOR((TIMESTAMPDIFF(SECOND, #startDate, #endDate) % 86400)/3600),2,'0'), ':',
LPAD(FLOOR((TIMESTAMPDIFF(SECOND, #startDate, #endDate) % 3600)/60),2,'0'), ':',
LPAD((TIMESTAMPDIFF(SECOND, #startDate, #endDate) % 60),2,'0')
);
Result 21:02:59
SET #endDate = '2020-06-27 10:02:00';
SET #startDate = '2020-06-26 09:59:01';
SELECT
CONCAT(
IF(FLOOR(TIMESTAMPDIFF(SECOND, #startDate, #endDate) / 86400) > 0,CONCAT(FLOOR(TIMESTAMPDIFF(SECOND, #startDate, #endDate) / 86400), '.'),''),
LPAD(FLOOR((TIMESTAMPDIFF(SECOND, #startDate, #endDate) % 86400)/3600),2,'0'), ':',
LPAD(FLOOR((TIMESTAMPDIFF(SECOND, #startDate, #endDate) % 3600)/60),2,'0'), ':',
LPAD((TIMESTAMPDIFF(SECOND, #startDate, #endDate) % 60),2,'0')
)
Result 1.00:02:59
If you're using MySQL 8+, you can use REGEXP_REPLACE:
SELECT REGEXP_REPLACE('00:01:39', '^[0:]+(\\d)', '$1')
For earlier versions, you can call TRIM twice, the first to trim leading 00:s from the string, and then to trim any further leading 0s:
SELECT TRIM(LEADING '0' FROM TRIM(LEADING '00:' FROM '00:01:39'))
In both cases the output is
1:39
There is one caveat on the TRIM method, if the result can be 00:00:00 the output will be an empty string. That can either be dealt with in your application, or you can use an IF expression to check for it:
IF(TIMEDIFF(t1, t2) = '00:00:00', 0, TRIM(LEADING '0' FROM TRIM(LEADING '00:' FROM TIMEDIFF(t1, t2))))
Demo on dbfiddle

How to convert MSSQL to MySQL date in PHP

I am a PHP developer and I am working in converting MSSQL to MySQL file while converting data's
CAST(0x063A0B00 AS Date)
I cant convert this as a timestamp.
SELECT
CAST(
'1900-01-01 00:00:00' +
INTERVAL CAST(CONV(substr(HEX(BinaryData),1,8), 16, 10) AS SIGNED) DAY +
INTERVAL CAST(CONV(substr(HEX(BinaryData),9,8), 16, 10) AS SIGNED)* 10000/3 MICROSECOND
AS DATETIME) AS converted_datetime
FROM
(
SELECT 0x0000987C00000000 AS BinaryData
UNION ALL
SELECT 0x00009E85013711EE AS BinaryData
) d
From how to cast the hexadecimal to varchar(datetime)?

data between two specific times in mysql get error

I am trying to select all data from my table where condition is
performDate will be between today 2.00 AM to tomorrow 2.00 AM
My query gives this error
Incorrect parameter count in the call to native function 'DATEDIFF'
My query is
SELECT * FROM `admin_marker` WHERE
FROM_UNIXTIME(performDate)
BETWEEN DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0) + '02:00'
AND DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()+1), 0) + '02:00'
DATEDIFF expects only 2 parameters. You call it with 3 parameter.
Why dont you do it that way ?
WHERE performdate >= DATE_FORMAT(NOW(), '%Y-%m-%d 02:00:00') AND performdate <= DATE_FORMAT(CURRENTDATE + INTERVAL +1 DAY '%Y-%m-%d 02:00:00')

DATEDIFF() seconds with millisecond to float or decimal

How to convert the below code to float or decimal type?
SELECT DATEDIFF(ss, StartTime, GETDATE()) + '.' +
DATEDIFF(ms, StartTime, GETDATE())
This is what I needed. CONVERT(float, DATEDIFF(ms, StartTime, GETDATE()) / 1000.0)
The code i my question was completely wrong because DATEDIFF(ms, StartTime, GETDATE()) returns total number of milisecond between the two dates and not as i thought only difference in the milliseconds part.
The code im my question would work if I used the DATEPART instead DATEDIFF in both expressions:
DATEPART(ss, GETDATE()) - DATEPART(ss, #StartTime) + '.'
+ DATEPART(ms, GETDATE()) - DATEPART(ms, #StartTime)

Migration of mysql dates in integer format

I want to migrate dates from integer format to DATETIME.
My dates in my old database have the following format:
olddb.table.date = 20131114 (INT)
olddb.table.time = 900 (INT) (9 AM, 24h clock)
new database:
newdb.table.datetime = 2013-11-14 9:00:00 (DATETIME)
How would I migrate this with purely SQL?
SELECT CAST(CONCAT(DATE(olddb.table.date),' ',TIME(olddb.table.time*100)) AS DATETIME);
You can convert your date part with STR_TO_DATE (MySQL documentation):
STR_TO_DATE(olddb.table.date, "%Y%m%d")
And for the time part, you can use the same function :
STR_TO_DATE(olddb.table.time, "%h%i")
Then, you can concatenate date and time parts and apply function to concatenation result :
STR_TO_DATE(concatenated_value, "%Y%m%d%h%i")
Where concatenated_value is built with :
CONCAT(olddb.table.date, olddb.table.time)
Try this:
cast(
concat(
table.date div 10000, '-',
lpad((table.date mod 10000) div 100, 2, '0'), '-',
lpad(table.date mod 100, 2, '0'), ' ',
table.time div 100, ':',
lpad(table.time mod 100, 2, '0'))
as datetime)
Here's a working example:
http://www.sqlfiddle.com/#!2/1ff75/1