Timestamp update DB2 SQL - sql

I would like to update query for records between 24 hours since the specific date and time. The current query works fine, except I need to update two timestamps manually. I am looking to reduce timestamps number to one or replace it with dynamic expression, so it will minimize human error if possible.
Current query looks like this:
SELECT timestamp
FROM table
WHERE timestamp BETWEEN '2023-01-18-06.00.00.000000' AND '2023-01-19-06.00.00.000000'
I have been trying multiple recommended options but it does not work yet:
WHERE timestamp > '2023-01-19-06.00.00.000000' - 24 HOURS
WHERE timestamp > '2023-01-19-06.00.00.000000' – ‘24 HOURS’
WHERE timestamp ('2023-01-19-06.00.00.000000' - 24 HOURS)
WHERE timestamp > '2023-01-19-06.00.00.000000' - '24.00.00.000000'
WHERE timestamp BETWEEN '2023-01-04-06.00.00.000000' AND INTERVAL - 24 HOURS
WHERE timestamp > CURRENT DATE - 24 HOURS
WHERE timestamp ('2023-01-19' - 1 DAY, ('06.00.00.000000' - 24 HOURS))
Could anyone let me know what I am doing incorrectly?

'2023-01-19-06.00.00.000000' - 24 HOURS is near, but incorrect because DB2 doesn't see the first value as a timestamp but as a string even if it makes the automatic cast in the working query. so what you have to do is to tell it is a timestamp, because you add a duration
with the timestamp keyword
WHERE yourtimestamp > timestamp '2023-01-19-06.00.00.000000' - 24 HOURS
or the timestamp function
WHERE yourtimestamp > timestamp('2023-01-19-06.00.00.000000') - 24 HOURS
or this notation
WHERE yourtimestamp > '2023-01-19-06.00.00.000000'::timestamp - 24 HOURS
if you're not using DB2LUW or an old version, one or more option may not be available
i suggest you try something like this
SELECT timestamp
FROM table cross join (values timestamp '2023-01-19-06.00.00.000000') as ref (stamp)
WHERE timestamp between ref.stamp - 24 hours and ref.stamp

For the past 24hrs, as implied by your example
WHERE timestamp > CURRENT DATE - 24 HOURS
You'd want to use CURRENT TIMESTAMP not CURRENT DATE
For a specific period, you'll always need two dates specified in the WHERE clause like so
WHERE timestamp BETWEEN startTs AND endTs
For a specific 24hr period from a given starting timestamp, you can do something like so:
WHERE timestamp BETWEEN startTs AND startTs + 24 hours
You can define startTs as a global variable, and use it in your select
create variable startTs timestamp default('2023-01-18 06:00:00.000');
SELECT timestamp
FROM table
WHERE timestamp BETWEEN startTs AND startTs + 24 hours;
Or you could use a table value constructor to store it for use...
WITH tmp (startTime) AS (
VALUES (timestamp('2023-01-19 06:00:00.000'))
)
select timestamp from table
where timestamp between (select startTime from tmp limit 1)
and (select startTime + 2 hours from tmp limit 1);
Depending on your use case, it might be worthwhile to encapsulate the statement as a stored procedure or a user defined table function (UDTF)...

Related

timestamp string to timestamp in sql

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

Oracle 11g and SQL Server EPOCH-3600 and EPOCH (Unix_timestamp)

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.

Hive SELECT records from 1 hour ago

I have a hive table that contains a column called timestamp. The timestamp is a bigint field generated from java System.currenttimemillis(). I suppose it should be in UTC. Right now I am trying to select records from 1 hour ago. I know in MySQL you can do something like:
SELECT * FROM table WHERE datetimefield >= DATE_SUB(NOW(), INTERVAL 1 HOUR)
In hive, it seems like NOW() is missing. I did some searching and find unix_timestamp(). I should be able to get the current UTC time in milliseconds by doing a unix_timestamp()*1000.
So if i want to get records from 1 hour ago I am thinking about doing something like:
SELECT * FROM hivetable WHERE datetimefield >= (unix_timestamp()*1000-3600000);
Can someone suggest if it's the right way to approach this problem? Also what if I want to select like 1 day ago? Seems inconvenient to convert that to milliseconds. Any help or suggested readings will be highly appreciated. Thanks in advance for your help.
Yes unix_timestamp() gets you the seconds elapsed since Unix epoch. You can subtract 60*60*1000 milliseconds and compare your field to get the desired records.
For Hive 1.2.0 and higher you can use current_timestamp
select *
from hivetable
where
datetimefield >= ((unix_timestamp()*1000) - 3600000);
For 1 day,convert the milliseconds to date format and use date_sub
select *
from hivetable
where
from_unixtime(unix_timestamp(datetimefield,'MM-dd-yyyy HH:mm:ss')) >=
date_sub(from_unixtime(unix_timestamp()),1);

Substraction with decimal in ORACLE SQL

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

SQL . Call midnight timestamp unix without having to enter it manually

I have a table with 4 columns value , animal_id, food_id and timestamp. Time stamp records the data in unix time. Currently my script is working fine and produces a table like this. This is all great and all but this forces me to go edit the file and change the timestamp each day for today's midnight timestamp and this is quite time consuming. Is there a way to produce this line of code for every day without having to update it ? Thank you in advance. I use oracle 10g.
The line in the script I want to change
WHERE timestamp BETWEEN 1406073600 AND (1406073600 + 86400)
The Script
SELECT MAX(value),animal_id, food_id
FROM farm1
WHERE timestamp BETWEEN 1406073600 AND (1406073600 + 86400)
group by animal_id, food_id ;
The Table Results
MAX(VALUE) animal_id food_id
---------- ---------- ----------
9302 8 9081
8015 10 9081
You can convert a date to a Unix-style epoch timestamp number with:
(your_date - date '1970-01-01') * 60 * 60 * 24
... since the timestamp is the number of seconds since 1970-01-01.
So to run your query for all records yesterday (which your sample seems to be doing):
SELECT MAX(value),animal_id, food_id
FROM farm1
WHERE timestamp >= (trunc(sysdate - 1) - date '1970-01-01') * 60 * 60 * 24
AND timestamp < (trunc(sysdate) - date '1970-01-01') * 60 * 60 * 24
GROUP by animal_id, food_id ;
BETWEEN is inclusive so will catch both midnights, which could potentially lead to a value being included in two days' runs, or skewing the results for one of them. I've changed from that to explicit >= and < to make it inclusive. Or you could just subtract 1 from the later time of course, to make it 23:59:59.
SQL Fiddle demo (including a value being picked up from the wrong with your original query).
A way to solve it would be to convert each timestamp to a conceptual date and use the virtual column systimestamp:
SELECT MAX(value),animal_id, food_id
FROM farm1
WHERE TO_CHAR(timestamp, 'YYYYMMDD') = TO_CHAR(systimestamp, 'YYYYMMDD')
group by animal_id, food_id ;