I need to write a select query that gets data in between EPOCH(datetime)-3600 AND EPOCH(datetime).
select all incidents modified in specific date range between EPOCH(datetime)-3600 AND EPOCH(datetime)
So my query is:
SELECT *
FROM TABLENAME
WHERE COLUMN_NAME BETWEEN COLUMNNAME-3600 AND COLUMNNAME
Will this query get the data from 1 hour ago to the current time, in Unix TimeStamp format?
Given a column (COLUMN_NAME) where data is stored as a unix timestamp (eg number of seconds elapsed since January 1st, 1970), you are looking to filter records based on a DATE passed as parameter.
My understanding is that you need a way to convert a unix timestamp to a date. There is no such built-in function in Oracle, however you are allowed to add a number to a date, where the number represents the number of days to add.
Try :
SELECT *
FROM TABLENAME
WHERE
TO_DATE('01/01/1970', 'dd/mm/yyyy') + COLUMN_NAME/60/60/24 -- convert unix timestamp to date
BETWEEN :mydate - 3600 AND :mydate
;
Replace both :mydate with the actual DATE for which you want the search to be performed.
Related
Date data saved from stripe start_date as string timestamp like "1652789095".
Now I want to filter with this timestamp string form last 12 months.
what should I do ?
how can I filter with this timestamp string?
These are some examples - I'm sure there are plenty of options that would work.
convert to date
select *
from Table
where
to_timestamp(cast(start_date as int)::date > date_add(now(), interval -1 year);
work with unix timestamps
-- approx 1 year ago, by way of example
select *
from Table
where
start_date > '1621253095';
-- exactly one year ago, calculated dynamically
select *
from Table
where
start_date >
cast(unix_timestamp(date_add(now(), interval -1 year)) as varchar);
I'm not a MySQL guy really so forgive any syntax errors and fix up the sql as needed to work in MySQL.
Resources:
PostgreSQL: how to convert from Unix epoch to date?
https://www.postgresonline.com/article_pfriendly/3.html
I have a table which is fed two different date formats:
d/m/Y & m/d/Y. The date format wanted is d/m/Y
I am able to select the date column and do a check and format if the date is in the wrong format.
This is my current SQL query:
SELECT COALESCE(TRY(date_format(date_parse(tbl.date, %d/%m/%Y), %d/%m/%Y)),
TRY(date_format(date_parse(tbl.date, %m/%d/%Y), %d/%m/%Y))) as date
FROM xxx
That fixes the mismatched dates...however I also need to query a date range e.g. the last 7 days.
If I add a WHERE statement it does not execute as I have already queried the date earlier.
How can I format my dates AND filter based on a given range (last 7 days)?
In ANSI SQL -- implemented by Presto, which Athena is based on -- the WHERE clause cannot reference the SELECT projections, so you need a aubquery:
SELECT *
FROM (
SELECT COALESCE(TRY(date_parse ....... AS date
FROM xxx
)
WHERE date > current_date - INTERVAL '7' DAY
I need to substract 2 timestamps in the given format:
16/01/17 07:01:06,165000000
16/01/17 07:01:06,244000000
I want to express the result with 2 decimal values but somewhere in the CAST process I am loosing precision. My atempt by now goes this way:
select
id,
trunc((CAST(MAX(T.TIMESTAMP) AS DATE) - CAST(MIN(T.TIMESTAMP) AS DATE))*24*60*60,2) as result
from table T
group by id;
But I get id_1 '0' as a result for the two timestamps above even after I set the truncate decimals at 2.
Is there a way that I can obtain the 0.XX aa a result of the substraction?
It's because you are casting the timestamp to date.
Use to_timestamp to convert your string into timestamp.
Try this:
with your_table(tstamp) as (
select '16/01/17 07:01:06,165000000' from dual union all
select '16/01/17 07:01:06,244000000' from dual
),
your_table_casted as (
select to_timestamp(tstamp,'dd/mm/yy hh24:mi:ss,ff') tstamp from your_table
)
select trunc(sysdate + (max(tstamp) - min(tstamp)) * 86400 - sysdate, 2) diff
from your_table_casted;
The difference between two timestamps is INTERVAL DAY TO SECOND.
To convert it into seconds, use the above trick.
DATE—This datatype stores a date and a time, resolved to the second. It does not include the time zone. DATE is the oldest and most commonly used datatype for working with dates in Oracle applications.
TIMESTAMP—Time stamps are similar to dates, but with these two key distinctions: you can store and manipulate times resolved to the nearest billionth of a second (9 decimal places of precision), and you can associate a time zone with a time stamp, and Oracle Database will take that time zone into account when manipulating the time stamp.
The result of a substraction of two timestamps is an INTERVAL:
INTERVAL—Whereas DATE and TIMESTAMP record a specific point in time, INTERVAL records and computes a time duration. You can specify an interval in terms of years and months, or days and seconds.
You can find more information here
I want to display in my gridview all data whose timestamp is not older than 1 years from current date.
My timestamp is formatted like so: 20110125-071830 or yyyymmdd-hhmmss
I have tried to reference :
Retrieve rows less than a day old:
Select * from table_name WHERE DATE(timestampVal) > DATE(NOW() - INTERVAL 1 YEAR);
but got missing expression flag
I also tried various others; however, my timestampVal is different simply by how it is formatted.
Like for example: https://community.oracle.com/thread/2207956
With coding: Select * from table_name WHERE timestampVal < sysdate - interval '1' year ;
but get error: literal does not match format string which means sysdate can't read how mine is formatted.
How do I query my timestamp to pull all that are a year or less old?
FYI: timestampVal is string type [varchar]
It looks like the following should work:
Select *
from table_name
WHERE TO_DATE(timestampVal, 'YYYYMMDD-HH24MISS') > sysdate - interval '1' year;
Also note that I reversed the comparison: you indicated that you want rows where timestampVal is not older than 1 year ago - so timestampVal should be greater (newer) than current time minus one year.
Give that a try.
Best of luck.
just like sysdate inserts the current date,
Is there any way to insert only the current time using a sql query.
for example *insert into table_name values(sysdate); *
There is no Oracle data type that represents just a hours/minutes/seconds. There is DATE and the various TIMESTAMP types that represents a point in time, including date and time. You don't say why you're trying to store this, but you could store the time as an offset in a NUMBER column, as fractions of a day.
So, if you wanted to find store the time of day that a store opens on Monday, you'd do:
TRUNC( SYSDATE ) + offset