SQLite3 on windows: Convert epoch to normal time - sqlite

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

Related

How do i subtract time from datetime in snowflake?

pdt.startTime is datetime
s_first.FromTimeOfDay is a time
I want to subtract the time drom the datetime. When i run the code below, Snowflake gives me this error invalid type [CAST(S_FIRST.FROMTIMEOFDAY AS TIMESTAMP_NTZ(9))] for parameter 'TO_TIMESTAMP_NTZ'
select (pdt.StartTime - (SELECT s_first.FromTimeOfDay::datetime FROM Shift s_first))
from RAW_CPMS_AAR.POWERBI_DowntimeTable AS PDT
When i try this:
select (pdt.StartTime::TIMESTAMP_NTZ(9) - (SELECT s_first.FromTimeOfDay::TIMESTAMP_NTZ(9) FROM Shift s_first))
from RAW_CPMS_AAR.POWERBI_DowntimeTable AS PDT
I get more or less the same error: invalid type [CAST(S_FIRST.FROMTIMEOFDAY AS TIMESTAMP_NTZ(9))] for parameter 'TO_TIMESTAMP_NTZ'
How do I convert the time into a datetime format so that I can subtract the two. It doesnt seem to me that there is a clear way to convert time into datetime in snowflake.
Is this what you're after?
select current_timestamp() as sample_timestamp
, time(sample_timestamp) as sample_time
, date(sample_timestamp) as sample_date;
A user pointed me in the right direction. i didnt realize i could use "dateadd" to also subtract time.
dateadd(HOUR, - (HOUR(current_timestamp())), temp.DateTime)

Sqllite query convert timestamp to date

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 ...

Checking if a Date falls within a Range of Dates in SQLITE

I have an sqlite table with with 2 date columns (beginning & end). I am looking for a query that returns the date range a date falls in. e.g Checking range where 10-02-2016 falls given 2 records as follows
(01-02-2016 - 12-02-2016) & (13-02-2016 - 20-02-2016)
Clearly it falls in the first range but how do I do it in SQLITE. Please help?
I think the problem is next SQLite requires dates to be in format YYYY-MM-DD. With that format you could write a simple query:
select * from table where yourdate between begin and end
If you can't change your format you should look at next sqlite functions https://sqlite.org/lang_datefunc.html
Also you can try substr function to cotact date in needed format.

How to convert minutes into HH:MM:SS formate in sql server

I am having a column in my sql server database table with datatype bigint.
Now i need to convert this column into HH:MM:SS formate.
For this i use this command
SELECT cast(CONVERT(VARCHAR,DATEADD(MS,SUM(mFld),0),8) as Time) FROM tblm
But this command doesn't show right time duration.
For right time duration i use this command
SELECT rtrim(LTRIM(cast((mFld/(60*60)) as char)))+':' +rtrim(LTRIM(cast((mFld%(60*60))/(60)as char)))+':'+rtrim(LTRIM(cast(((mFld%(60*60))%(60)) as char))) FROM tblm
This command shows right result.But resulted data type is varchar.How can i convert this column in to time column with same result.
Example
DECLARE #minites bigint;
SET #minites = 5200020;
SELECT rtrim(LTRIM(cast((#minites/(60*60)) as char)))+':' +rtrim(LTRIM(cast((#minites%(60*60))/(60)as char)))+':'+rtrim(LTRIM(cast(((#minites%(60*60))%(60)) as char)))
SELECT cast(CONVERT(VARCHAR,DATEADD(MS,SUM(#minites),0),8) as Time)
How can i convert this column in to time column with same result.
You can't. Time has the range 00:00:00.0000000 to 23:59:59.9999999.
time (Transact-SQL)

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.

Resources