mysql convert bigint to datetime - mysql

The CreateDateTime column in a table is in the format bigint(20). For example it looks like: 131037078373067074. I didn't create this table, it is passed on to me.
Using the example here Convert BigInt timestamp to a real Date with row aggregation and operations in mySQL, I have tried dividing CreateDateTime by POW(10,8) or POW(10,9) .
SELECT FROM_UNIXTIME(CreateDateTime/POW(10,8)) AS due_date
FROM images;
results look like '2011-07-11 02:53:03.730671'.
SELECT FROM_UNIXTIME(CreateDateTime/POW(10,9)) AS due_date
FROM images;
results look like '1974-02-25 09:11:18.373067'.
I am expecting a date in 2016. However dividing CreateDateTime by POW(10,7) gives null.
What should I do?

I am expecting a date in 2016.
Dividing by 89000000 gives a date in 2016
(To discover that I converted a date in the middle of 2016 to seconds since unix epoch and divided your number by that).
What should I do?
If you have some way to get the real datetime (like from an existing and known working user interface) you can probably reverse engineer the formula. Take several samples and keep track of the number in CreateDateTime and the correct date time. The number may represent microseconds or nanoseconds since a different epoch. Unix uses 1970-01-01 UTC, Excel uses Jan 0, 1900 (no, not Jan 1 but Jan 1 - 24 hours), NTP uses Jan 1 1900, IBM BIOS (and ZIP) uses Jan 1, 1980.
The number of "fractional seconds" in one second may be a multiple of 60 instead of 10.
Use the data you gather from the user interface to solve for
(CreateDateTime + a) / b = UnixSecondsSince1970_01_01
OR
(CreateDateTime / b) + a = UnixSecondsSince1970_01_01
where "a" is the offset needed to adjust for non-unix epoch and "b" is the number of units needed to add up to one second in the "CreateDateTime" representation.
Others epoch choices referenced here:
https://en.wikipedia.org/wiki/Epoch_(computing)#Notable_epoch_dates_in_computing
( 131037078373067074 / 10000000000 + ( 24 * 60 * 60 * 17000 ) )
Also gives a date in 2016

Related

Calculate the difference in hours between two String dates in MySQL?

I have two String columns in MySQL database. Those two columns were populated from a Java program in following way:
System.currentTimeMillis(); //first column
System.currentTimeMillis(); + someStringHours //second column; the String, someStringDays reprensents some number of days, let's say 5 hours in millis...
Which function in MySQL can be used to calculated the difference to get number of hours between these two columns?
You call them string dates but they are actually UNIX timestamps in milliseconds (also called Javascript timestamps). That's what System.currentTimeMillis() generates. It's a Java long data item, and a MySQL BIGINT data item. You can store it in a string. (You can store it that way if you must, but searching and sorting numbers stored as strings is an unreliable mess; beware!)
A typical Javascript timestamp (or UNIX timestamp in milliseconds) is a big integer like 1600858176374456. 1 600 858 176 374 456.
You can convert such a timestamp to a MySQL TIMESTAMP value with FROM_UNIXTIME() like this
FROM_UNIXTIME(column * 0.001)
Why multiply the column value by 0.001 (that is, divide it by 1000)? Because FROM_UNIXTIME() takes the timestamp in seconds, whereas System.currentTmeMillis() generates it in milliseconds.
Then you can use DATEDIFF() like this
DATEDIFF(FROM_UNIXTIME(laterTs*0.001),FROM_UNIXTIME(earlierTs*0.001))
This gives an integer number of days.
If you need the time difference in some other unit, such as hours, minutes, or calendar quarters, you can use TIMESTAMPDIFF(). This gives you your difference in hours.
TIMESTAMPDIFF(HOUR,
FROM_UNIXTIME(laterTs*0.001),
FROM_UNIXTIME(earlierTs*0.001));
You can use SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, or YEAR as the time unit in this function.
Pro tip: Use your DBMS's date arithmetic functions if you possibly can. They've worked out all sorts of edge cases for you already.
And, by the way, if you declare your columns like this (Timestamp with a millisecond precision: How to save them in MySQL):
laterTs TIMESTAMP(3),
earlierTs TIMESTAMP(3),
You'll have an easier time indexing on and searching by these times.
SELECT (1600858176374-1600944576374)/(24*60*60*1000) as Days
Where (1600858176374-1600944576374) are timestamps and (246060*1000) is a mills in day

How can I convert this to DATETIME SQL?

Trying to convert this value to DATETIME and unsuccessful so far. Thoughts and ideas are welcome.
03MAR2020:02:45:58.977000
You can use the function STR_TO_DATE() with the proper format:
select str_to_date('03MAR2020:02:45:58.977000', '%d%b%Y:%k:%i:%s.%f')
See the demo.
Result:
2020-03-03 02:45:58.977000
in SQL Server
03MAR2020:02:45:58.977000 is failing to convert tot datetime because, first there is an extra : sign after the year and the fractional seconds is 6 digits instead of max 3.
If the string is always the same length and the same format, then you can use the following to solve this problem:
DECLARE #example varchar(25) = '03MAR2020:02:45:58.977000'
SET #example = LEFT(#example, 22) --- remove last 3 zeros
SET #example = STUFF(#example, CHARINDEX(':', #example), LEN(':'), ' ') ---repalce the first ':'
SELECT convert(datetime, #example) --- convert to datetime
More details: Here are the parts of datetime data type in SQL Server.
YYYY is four digits from 1753 through 9999 that represent a year.
MM is two digits, ranging from 01 to 12, that represent a month in the specified year.
DD is two digits, ranging from 01 to 31 depending on the month, that represent a day of the specified month.
hh is two digits, ranging from 00 to 23, that represent the hour.
mm is two digits, ranging from 00 to 59, that represent the minute.
ss is two digits, ranging from 00 to 59, that represent the second.
n* is zero to three digits, ranging from 0 to 999, that represent the fractional seconds.
Taken from the SQL server documentation
https://learn.microsoft.com/en-us/sql/t-sql/data-types/datetime-transact-sql?view=sql-server-ver15

Convert Bigint data to human readable date-time in Sql

I have created a MySql table and feed data therein with below code
CREATE TABLE IF NOT EXISTS DB (
INN VARCHAR(200) NOT NULL,
Time BIGINT not NULL
);
Now I want to get a table which will report the Maximum and Minimum values of Time when grouped by INN. Below is my code -
SELECT INN, from_unixtime(MIN(Time)), from_unixtime(MAX(Time)) FROM DB GROUP BY INN
I want to get the Time values reported as normal date-time instead of Epoch time. But with above code, I am getting <NA> values.
A typical Time value is like 1546380001264082944
Can someone please help me to get the correct code to achieve the same.
The problem here is to do with the precision of the unix timestamp you are using.
Consider this:
SELECT FROM_UNIXTIME(1546380001), FROM_UNIXTIME(1546380001264082944)
The output is:
2019-01-01T22:00:01Z (null)
The timestampt value you have, 1546380001264082944, contains a level of precision beyond that accepted by MySQL.
The definition of FROM_UNIXTIME is:
FROM_UNIXTIME(unix_timestamp[,format])
The doc states:
unix_timestamp is an internal timestamp value representing seconds
since '1970-01-01 00:00:00' UTC
The precision of your timestamp is significantly greater than seconds since the Unix Epoch.
The docs are available here.
The value 1546380001264082944 is too big to be epoch seconds or even milliseconds. This is easily verified by putting this value on https://currentmillis.com/.
You have stored precision upto a nanosecond. So, divide the column value by 1e9 before passing them to from_unixtime.
SELECT INN, from_unixtime(MIN(Time) / 1000000000), from_unixtime(MAX(Time) / 1000000000)
FROM DB
GROUP BY INN

SQL division by a time frame

Have an RDB with a quantity x and the date that quantity started being tracked, date_1 and the date it was finished being tracked date_2. If tracking is still on going that second date is NULL obviously.
What I would like to do is take the number X and get its average over either date_1 and date_2. And if date_2 is NULL then go by current time. Any help?
[EDIT] to clarify in RDB format, one row with data column (x), data column (date_1) and data column (data_2) along with other fields of importance.
[EDIT] so imagine X as some integer like 100,000 and dates being March 30, 2016 12:29:45 and April 3, 2016 03:42:29. Not sure how to breakdown the date/times yet so open to suggestions. The end goal is calculate how much of x can be allocated in one month vs how much in the other month. Depending on how fine grain you breakdown the time frame (days vs seconds) will ultimately change those numbers.

Access SQL: How to convert a year stored in an int to a date - dates are in 16th century

There are some similar questions on Stack Overflow (like questions 1923876, or question 266924 ) concerning SQL Server or TSQL. However I have received an ACCESS 2010 database and I'm trying to use the given solution with CAST etc. in ACCESS SQL, but with no success so far.
The dates are in 16 th century and there are two columns:
exact_date (datetime) and
estimated_year (int).
These columns should be used to sort using ORDER BY (assuming month and day as 1st January if only the year is known or estimated) the result.
Since CAST is not supported in Access SQL, consider DateSerial(year, month, day) to convert an integer year to a Date/Time value for Jan 1 of that year.
Here is an example copied from the Access Immediate window:
estimated_year = 1505
? DateSerial(estimated_year, 1, 1)
1/1/1505
? TypeName(DateSerial(estimated_year, 1, 1))
Date