I'm trying to convert this string "0x00009F0900000000" into a date either through MySql or Rails as I'm working on a migration.
Can't find what format it is. Doesn't look like there's a way to convert hexadecimal value into a date through rails or mysql.
Solution found here: how to cast the hexadecimal to varchar(datetime)?
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 0x00009F0900000000 AS BinaryData
) d
In ruby (and ignoring time)
require 'date'
str = "0x00009F0900000000"
p Date.new(1900,1,1) + str[0..9].hex
#<Date: 2011-06-21 ((2455734j,0s,0n),+0s,2299161j)>
This is the closest I found to your question:
> "0x00009F0900000000".to_i(16)
=> 174861003522048
> time = "0x00009F0900000000".to_i(16)
=> 174861003522048
> Time.at(time/1000)
=> 7511-02-16 05:58:42 +0100
> Time.at(time/1000000)
=> 1975-07-17 21:30:03 +0100
Try several divisors till you get what should be accurate
Related
If I use the Following code in PHPMyAdmin (SQL) statement, it works and I get a list of all Birthdays of the following 31 days. Here is my code:
SELECT
*
FROM
membres
WHERE
DATE_ADD(
date_de_naissance,
INTERVAL
YEAR(CURDATE()) -
YEAR(date_de_naissance) +
IF(DAYOFYEAR(CURDATE()) > DAYOFYEAR(date_de_naissance), 1, 0)
YEAR
)
BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 31 DAY)
AND
(
MONTH(date_de_naissance) <> MONTH(CURDATE())
OR
DAY(date_de_naissance) <> DAY(CURDATE())
)
How would it be in Cakephp? Please help
I generally find it's much easier to do the date math in CakePHP than in MySQLs, so I'd do something like this:
$birthday_members = $this->Membres->find()
->where([
'date_de_naissance >' => Cake\I18n\FrozenDate::now(),
'date_de_naissance <=' => Cake\I18n\FrozenDate::now()->addDays(31),
]);
Is there any effecient way to php date adding without using date_add()
using php < ver 5.3
Can i use strtotime integer number to add days . here is my program:
$tg12 = DATE ( 'Y-m-d', strtotime ( $tg1 ) + 86400 ) ; // ADD 1 DAY // REVERSE back to format Human / reverse Strtotime() ;
$tg13 = DATE ( 'Y-m-d', strtotime ( $tg1 ) + 86400 * 2 ) ; // ADD 1 DAY // REVERSE back to format Human / reverse Strtotime() ;
$tg14 = DATE ( 'Y-m-d', strtotime ( $tg1 ) + 86400 * 3 ) ; // ADD 1 DAY // REVERSE back to format Human / reverse Strtotime() ;
Is the format correct? any suggesttion/IDEAs?
Thanks in advance
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)?
I working on a financial project where i need to calculate the difference of arrear days. If I use the method datediff() of mysql then it returns the result based on 365 days. I need the result based on 360 days. if use the following sql query
select datediff('20140908','20130908') from dual;
mysql returns the date difference 365. This is the actual date difference but in accounting/Financial calculation the difference is exactly one year (360 days). This is what I want. The result should be 360 instead 365.
Currently I want to use US standard.
to get the same result like Excel, I found the following code to use in MySQL:
select case
when (day(Startdate)>=30 or Startdate=last_day(Startdate) then
case
when(day(Enddate)>=30) then
30*(12*(year(Enddate)-year(Startdate))+month(Enddate)-month(Startdate))
else
30*(12*(year(Enddate)-year(Startdate))+month(Enddate)-month(Startdate))+days(Enddate)-30
end
else
30*(12*(year(Enddate)-year(Startdate))+month(Enddate)-month(Startdate))+days(Enddate)-days(Startdate)
end
Use
TO_DAYS(date2) - To_DAYS(date1)
It returns the number of days between date1 and date2. Then you can do with the result what you need.
sdate = from date
edate = to date
this calculate the days between the 2 date, taking into account that a month is 30 days, 1 year is 360 days, and checking if the date are at the end of the month, so e.g. from 1.1.2019 to 28.2.2019 = 60 days etc.
to be 1 month or 1 year, the edate should be a day before, so 1.1 - 31.1 = 30 days, 1.1 - 1.2 = 31 days
SELECT GREATEST(
0,
(YEAR(DATE_ADD(edate, INTERVAL 1 DAY)) - YEAR(sdate)) * 360 +
(MONTH(DATE_ADD(edate, INTERVAL 1 DAY)) - MONTH(sdate)) * 30 +
(
IF(DAYOFMONTH(DATE_ADD(edate, INTERVAL 1 DAY)) = DAYOFMONTH(LAST_DAY(DATE_ADD(edate, INTERVAL 1 DAY))), 30, DAYOFMONTH(DATE_ADD(edate, INTERVAL 1 DAY))) -
IF(DAYOFMONTH(sdate) = DAYOFMONTH(LAST_DAY(sdate)), 30, DAYOFMONTH(sdate))
)
Pretty late here, but posting my solution for future reference
CREATE FUNCTION `DAYS360_UDF`(sdate DATE, edate DATE)
RETURNS INTEGER
DETERMINISTIC
CONTAINS SQL
BEGIN
DECLARE sdate_360 INTEGER;
DECLARE edate_360 INTEGER;
SET sdate_360 = ( YEAR(sdate) * 360 ) + ( (MONTH(sdate)-1)*30 ) + DAY(sdate);
SET edate_360 = ( YEAR(edate) * 360 ) + ( (MONTH(edate)-1)*30 ) + DAY(edate);
RETURN edate_360 - sdate_360;
END
Usage -
select DAYS360_UDF('20130908', '20140908')
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