Sqllite query convert timestamp to date - sqlite

I'm working with DBeaver on Ubuntu Linux, and querying a sqlite database via a org.sqlite.JDBC driver. In a table, a column called "event_date" is apparently of type
ABCevent_date(TIMESTAMP(10))
so when I query it I get a column of big long numbers like :
|event_date |
|-------------|
|1430434800000|
|1430434800000|
|1430434800000|
|1430434800000|
|1430434800000|
|1433286000000|
|1433286000000|
I've tried lots of things using DATE, and DATETIME and STRFTIME but cannot get these to appear in the query results as a normal date like "2014-05-10", usually just get NULL. How can I convert these numbers to dates? Thx. J

Your dates are unix epoch times with milliseconds.
You must divide event_date by 1000 to strip off the milliseconds and then use the function date() or datetime():
SELECT date(event_date / 1000, 'unixepoch')
FROM ...

Related

How can I convert timestamps to dates in SQLite?

How can I convert timestamps to dates in SQLite?
The following code only produces a Timestamp column and a Date columns with NULL values. The the SQL code needs to convert from a "08/28/2020 8:00" format.
SQL CODE:
'''Select Timestamp, strftime('%m-%d-%Y', Timestamp) as Date
FROM Room_Data'''
The SQLite documentation is pretty clear, but I can't seem to get the desired result.
The strftime is meant to format a date, rather than perform conversion.
In the meantime you could try something like that to gather the pieces:
SELECT Timestamp,
SUBSTR(c,7,4) || '-' || SUBSTR(Timestamp,1,2) || '-' || SUBSTR(Timestamp,4,2) as Date
FROM Room_Data
Since SQlite doesn't really have the concept of a date, unlike other DBMS, the best choice would be to convert your dates to integer, either as Unix timestamps or in string form (YYYY-MM-DD) but storing dates as integer like 20201010 would be acceptable too.
NB: be careful with names like Timestamp or Date, they are reserved keywords in many programming languages and DBMSes.
The original code won't work in Windows 10 for some reason. Trying this from a Linux distro (Kubuntu, in this case) seems to resolve the issue. In Windows, the date needs to be converted to a '2020-01-01' format to actually work.
SQLite is amazing, but not sure why functionality changes for Windows 10. Feel free to comment if you know more about the differences.

Calculate a date in behind 24 hours Hive

My demand is really so silly, so basically I need to go back in time 24 hours in a timestamp column in Hive.
So far, I have tried two different ways but it's not going thru:
select
recordDate, --original date
cast(date_sub(cast(recorddate as timestamp),1) as timestamp), -- going one day behind without hour
cast((cast(cast(recorddate as timestamp) AS bigint)-1*3600) as timestamp) -- crazy year
from mtmbuckets.servpro_agents_events limit 10;
My output looks:
I appreciate the support you can give me.
thanks
There is not straight forward function in hive .
1 Create UDF to do so .
or
Convert date in no of second and do you calculation( -24 *60*60) sec then change back int to data.
use from_unixtime and unix_timestamp to achieve below code.
select from_unixtime(unix_timestamp(recorddate) - 86400)
from mtmbuckets.servpro_agen ts_events limit 10;;
From_unixtime
Convert time string with given pattern to Unix time stamp (in seconds) The result of this function is in seconds.
Unix_timestamp
Converts time string in format yyyy-MM-dd HH:mm:ss to Unix timestamp (in seconds), using the default timezone and the default locale, return 0 if fail: unix_timestamp('2009-03-20 11:30:01') = 1237573801

Storing my dates as long (milliseconds) in SQLite: Can I use strftime(...)?

I'm storing my dates in SQLite in a column of data type INTEGER. I'm storing the milliseconds since 1970.
Eg:
date (long) other columns ...
-----------------------------------------
1407297600000 ...
1407211200000 ...
1407124800000 ...
My question is: how can I use strftime() under this circumstances?
If not, I should use TEXT as the column type??
Running this:
select strftime('%Y-%m', date) from my_table;
Is throwing nonesense stuff:
strftime('%Y-%m', date)
-----------------------------------------
1968-19
1968-19
1968-19
Unless you tell it otherwise, strftime() thinks those numbers are Julian day values - very different from Unix epoch milliseconds.
You'll want to convert to seconds, and tell strftime() these are Unix epoch numbers:
select strftime('%Y-%m', date / 1000, 'unixepoch');
See the Modifiers section in the SQLite Date and Time Functions docs.

SELECT clause with a DATETIME column in Sybase 15

I'm trying to do a query like this on a table with a DATETIME column.
SELECT * FROM table WHERE the_date =
2011-03-06T15:53:34.890-05:00
I have the following as an string input from an external source:
2011-03-06T15:53:34.890-05:00
I need to perform a query on my database table and extract the row which contains this same date. In my database it gets stored as a DATETIME and looks like the following:
2011-03-06 15:53:34.89
I can probably manipulate the outside input slightly ( like strip off the -5:00 ). But I can't figure out how to do a simple select with the datetime column.
I found the convert function, and style 123 seems to match my needs but I can't get it to work. Here is the link to reference about style 123
http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.help.ase_15.0.blocks/html/blocks/blocks125.htm
I think that convert's slightly wrongly documented in that version of the docs.
Because this format always has century I think you only need use 23. Normally the 100 range for convert adds the century to the year format.
That format only goes down to seconds what's more.
If you want more you'll need to past together 2 x converts. That is, past a ymd part onto a convert(varchar, datetime-column, 14) and compare with your trimmed string. milliseconds comparison is likely to be a problem depending on where you got your big time string though because the Sybase binary stored form has a granularity of 300ms I think, so if your source string is from somewhere else it's not likely to compare. In other words - strip the milliseconds and compare as strings.
So maybe:
SELECT * FROM table WHERE convert(varchar,the_date,23) =
'2011-03-06T15:53:34'
But the convert on the column would prevent the use of an index, if that's a problem.
If you compare as datetimes then the convert is on the rhs - but you have to know what your milliseconds are in the_date. Then an index can be used.

SQLite3 on windows: Convert epoch to normal time

I am trying to convert the following timestamp(in milliseconds since epoch) to normal date-time. Am using sqlite3 on windows xp.
I am using this query:
select datetime((timestamp/86400000)+25569) from table;
(timestamp is the column name which contains the values like 1289325613669,1289325823860,
1289327180545).
I dont seem to be getting the right values. Am i doing something wrong?
Do this:
select datetime('1289325613', 'unixepoch');
The unixepoch modifier expects a value in seconds.
Currently, what you provide to datetime is interpreted as a Julian Day number.
The reference for date and time functions is here

Resources