Get information between two different times Oracle - sql

I am making a modification to an Oracle Query, where I need to get the information found from the query date and minute up to 30 minutes ago.
For example, I made the query at 16:35, so I need it to show me the information found from 16:05 to 16:35.
I did something like this, but I don't have the result I need.
Also, how can I make it find everything loaded with current date? This is what I have done with no result
AND FV.FEC_CAR = dateadd(minute,-30,getdate()) ORDER BY F.N_FILE DESC
Thank you very much in advance

dateadd and getdate aren't valid in Oracle's SQL dialect. That looks like SQL Server syntax but it probably works in some other database as well.
In Oracle, you'd do
fv.fec_car > sysdate - interval '30' minute
or
fv.fec_car > sysdate - 30/24/60
I find the interval syntax far clearer personally

As far as I can understand and interpret, you need to see the data at a point in the past before applying some modification to your table. This case,
SELECT *
FROM tab AS OF TIMESTAMP SYSTIMESTAMP - INTERVAL '30' MINUTE
might be used to see the values of half an hour before modification if undo_retention parameter's value of your database is big enough(without forgetting that it does not guarantee to return a result even if the value is big enough)

Related

How to add a certain number of days based on field value

I'm working on creating a view to convert some values that we get coming in for dates/times. (I've figured out the times already.) In this table, dates come in the format of "Days after 12-31-1840." I'm trying to create a view that shows the actual dates/times rather than in this format. This is what I have so far:
CASE WHEN UPPER(FLWSHT_MEAS_NM) LIKE '%DATE' THEN TO_CHAR(DATE '1840-12-31' + INTERVAL ACUTE_MEASURE_VALUE DAY)
I know that this is the correct syntax for adding dates, because I'm able to get the view working with this instead:
CASE WHEN UPPER(FLWSHT_MEAS_NM) LIKE '%DATE' THEN TO_CHAR(DATE '1840-12-31' + INTERVAL '30' DAY)
My question is, how do I add a specific number of days based on the ACUTE_MEASURE_VALUE field? I'm not able to run the code to get a runtime error as it's coming up as a syntax error.
From the Teradata documentation:
Teradata SQL extends the ANSI SQL:2011 standard to allow the operations of adding or subtracting a number of days from an ANSI DATE value. Teradata SQL treats the number as an INTERVAL DAY value.
I'm assuming your field ACUTE_MEASURE_VALUE is already in your table and is an integer. The words INTERVAL and DAY are part of the specification of interval constants - this is a variable and syntactically you don't use those keywords.
...TO_CHAR(DATE '1840-12-31' + ACUTE_MEASURE_VALUE)...
Just drop the INTERVAL keyword and the DAY keyword and it should work.
By the way, why are you using To_Char() in this? By transforming it into a character string it preempts anyone using this view from performing calculations on this date. If you leave the view in DATE format then any subsequent Select from this view has a lot more flexibility in manipulating this data field.

Substract the hour from the timestamp.( hour is part of the timestamp column)

Need some help to perform this in Hive .
I do have timestamp like "2019-03-11T18:23:49-04:00"
How to I subtract the hour and minutes from the above timestamp.( -04:00)
The hour component may vary based on the timezone.
Thanks in advance.
Looks like you can't do this as easily as you can in MSSQL with dateadd. Some very good suggestions are made here. I also tested this and it works as it should.
select from_unixtime(unix_timestamp('2015-12-12 16:15:17')+8500)
I have modified this to work with your exact timestamp format
select from_unixtime(UNIX_TIMESTAMP(substring(translate("2019-03-11T18:23:49-04:00",'T',' '),1,19))+8500)

Find entries in sql that were done milliseconds against each other

I need to debug an issue I found with someone else's code, where it reads both the id and the timestamp for a key. But, I noticed that it is not reading the millisecond information. While this is a potential cause, this not confirmed, and I need to find out if this is the cause.
The problem could occur if two entries in the table happened within the same second, 10:20:05.0500 pm and 10:20:05.5000 pm, but not 10:20:05.5000 and 10:20:06.0500.
How do I write such a query to look for it?
I am using Oracle pl/sql.
In Oracle to use fractional seconds a column must be of data type "TIMESTAMP".
Maybe someone else's code is using a variable of type DATE, which is year-month-day hour-minute-second without fractional seconds.
Database SQL Language Reference: Data Types
Can you give the table description and someone else's code?
To find records with the same date/time up to second precision but with different milliseconds, you can compare different records by joining the table to itself
SELECT A.ts, B.ts
FROM
Test A
INNER JOIN Test B
ON TO_CHAR(A.ts, 'YYYY-MM-DD HH24:MI:SS') = TO_CHAR(B.ts, 'YYYY-MM-DD HH24:MI:SS')
WHERE
A.ts < B.ts
ORDER BY
A.ts, B.ts;
TO_CHAR truncates the milliseconds. This is important, because functions that round could yield different seconds. E.g., CAST(ts as timestamp(0)) rounds, which is not what wee need.
The example from the link below has a record with 999 milliseconds to test this.
See example on SQL Fiddle.

Cannot pull correct time range

After executing this query
AND TRUNC(ITD.TRAN_DATE)>= '02-APR-18'
AND TO_CHAR(ITD.TRAN_DATE,'HH24MI')>='0600'
AND TRUNC(ITD.TRAN_DATE)<= '03-APR-18'
AND TO_CHAR(TRUNC(ITD.TRAN_DATE+1),'HH24MI')<='0030'
Everything after 6AM for April 2nd will show, but also times after 12:30AM the next day shows. What am I doing wrong here?
When we truncate a date we remove the time element. Consequently, a mask of 'HH24:MI' will return '00:00' which means that this will always be true regardless of the actual time component of ITD.TRAN_DATE:
AND TO_CHAR(TRUNC(ITD.TRAN_DATE+1),'HH24MI')<='0030'
Probably what you should do is something more straightforward, such as
where itd.tran_date >= date '2018-04-02' + (6/24)
and itd.tran_date <= date '2018-04-03' + (1/48)
Here's a SQL Fiddle demo comparing your WHERE clause with my suggestion.
Not sure what you want. Maybe this:
and to_char(itd.tran_date, 'YYYYMMDD-HH24MISS')
between '20180402-0600'
and '20180403-0030'
If so however, and if the table have many rows and tran_date is an indexed column, this might be a lot faster:
and itd.tran_date
between to_date('20180402-0600','YYYYMMDD-HH24MI')
and to_date('20180403-0030','YYYYMMDD-HH24MI')
In your question you assume the date_format to be DD-MON-YY. This might be the case now, for the environment you have today, but might not be so always. So it's good practice to use to_char or to_date on DATE values or strings.

Oracle SQL: Why does last_day(SYSDATE) need to_date()?

Yesterday, I changed one of our queries to pivot on sysdate.
In my where's and joins I used last_day(SYSDATE) at first; however, this returned no data (This was filtering on a time_key column with date values).
When I changed the query to to_date(last_day(SYSDATE)) the query returned data as it should.
I have heard that SYSDATE contains time as well as a date (Although, I don't see this in
a SELECT SYSDATE FROM DUAL;).
Can anyone elaborate on why I need to call to_date() on my last_day(SYSDATE)? Shouldn't I have already had a date value?
This is a big long for a comment.
The expression last_day(sysdate) works fine. Here is a SQL Fiddle.
My guess is that the problem you are experiencing is that the time component is passed through, and you don't want the time. In that case, just use trunc(). The following are equivalent:
trunc(last_day(sysdate))
last_day(trunc(sysdate))