On Oracle SQL Developer, I'm looking to write a where function that gives me the last 7 days of data. I can write that part myself fine, but the extra part that I need to add on to the end is that I only want results that are for the past 7 days before my current time.
For example, if I query at 14:00 today, I would want it to return results for the past 7 days with data only up until 14:00, as opposed to the full day.
Is this possible?
SYSDATE is the Oracle built in date function that supplies the current date / time accurate to 1 second. All you have to do is:
select *
from whatever
where whatever.datecol between sysdate - 7 and sysdate;
Related
I was recently working on some SQL queries where I had to separate the values into various months (e.g. December, January, Feb...) and make some comparisons. However I was at a loss for wondering about what to use for the ending day of each month. So I was wondering what happens when you define a date that does not technically exist. For example.
WHERE myDate BETWEEN '2016-01-01' AND '2016-02-31' //note Feb 31 does not exist.
My assumption (based on my current query seeming to return the proper results) is that it simply ignores the extra dates that do not exist (e.g. when counting the dates, it simply has no dates for the range outside of the regular dates).
Is this undefined behavior that I may run into trouble with in the future? Or is there a better way to do this to cover all basis?
Why don't you want to use LAST_DAY() function:
SELECT SYSDATE, trunc(LAST_DAY(SYSDATE)) last,
LAST_DAY(SYSDATE) - SYSDATE days_left FROM DUAL;
Output:
SYSDATE LAST DAYS_LEFT
----------------- ----------------- ----------
03.02.16 18:38:26 29.02.16 00:00:00 26
1 row selected.
I am using Oracle SQL developer. I am using the following query to get the current time stamp.
select to_char(CURRENT_TIMESTAMP,'DDMMYYYY/HHMMSS') from dual;
In this, minutes is constantly set to 10. But when we don't use to_char, it is working fine. How to find what went wrong? Is there any method to correct this?
You should use MI in HHMMSS instead of MM. MI stands for minutes, MM is for months, and currently it is October, hence the 10.
You can find the available formatting options at Oracle's site.
select to_char(CURRENT_TIMESTAMP,'DDMMYYYY/HHMMSS') from dual;
The issue is with the HHMMSS - MM is used to represent the month number. MI is what is used to represent minutes.
So what you're really after is:
select to_char(CURRENT_TIMESTAMP,'DDMMYYYY/HHMISS') from dual;
Ok, so i have database on PL/SQL.
Here is my sql question:
SELECT t.AC_NUMBER, t.DATE, a.comment_1, a.comment_2, a.comment_3, a.comment_4
FROM proddba.cust_info t
left join proddba.cust_descr a on a.ac_number=t.ac_number
where a.open_date=(select min(b.open_date)
from proddba.cust_descr b
where b.ac_number=t.ac_number
and b.open_date>=t.date
and b.open_dane<=t.date+7days)
How to dynamically add +7 days to date?
And second, how to get only one date min(b.open_date) if there is two this same datas? Should I use distinct?
(select distinct min(b.open_date)
from proddba.cust_descr b
where b.ac_number=t.ac_number
and b.open_date>=t.date
and b.open_dane<=t.date+7days)
If have to get about 15000 records from database, is this should work?
Best Regards
You can simply add a number of days to a DATE
t.date + 7
will add exactly 7 days to the DATE in t.date (so the time component will be preserved).
MIN will already cause the subquery to return a single data value-- there is no need to add a DISTINCT since it won't ever change the output. I'm not sure what problem you are trying to describe that results from getting multiple rows-- are you possibly concerned that the outer query returns two rows that have the same a.open_date value and you are trying to ensure that you get only one row?
When I find months between 28-FEB-11 and 29-FEB-12, months_between function in oracle returns 12. Actually it should be 12.096. This function is not calculating for the leap year proper.
For between 28-FEB-11 and 29-FEB-12, it is 1 year(12 months) and 1 day.
select months_between('28-FEB-12', '28-FEB-11') from dual; -- 12
**select months_between('29-FEB-12', '28-FEB-11') from dual; -- 12**
select months_between('28-FEB-12', '27-FEB-11') from dual; -- 12.0322
select months_between('27-FEB-12', '28-FEB-11') from dual; -- 11.9677
is this an Oracle bug??..
-Vishwa
From the Oracle documentation:
MONTHS_BETWEEN returns number of months between dates date1 and date2. If date1 is later than date2, then the result is positive. If date1 is earlier than date2, then the result is negative. If date1 and date2 are either the same days of the month or both last days of months, then the result is always an integer. Otherwise Oracle Database calculates the fractional portion of the result based on a 31-day month and considers the difference in time components date1 and date2.
So it's following the documented behavior. It's just not what you expected.
It's not a bug because ORACLE says so, it's a logical error driven by a human (and documenting as something rigth) which is worse.
How come if difference between the last days of January and February months is exactly 1 month (29 exact days). Please see below:
MONTHS_BETWEEN('29-FEB-12','31-JAN-12')
1
With an extra day (30 days) the difference in months is less than 1. Please see below:
MONTHS_BETWEEN('29-FEB-12','30-JAN-12')
.967741935
WRONG. That's not rigth at all!
SQL Server in another hand handle this correctly:
select DATEDIFF(MM,'29-FEB-12','30-JAN-12')
select DATEDIFF(MM,'29-FEB-12','31-JAN-12')
both are 1
I have a query to run on oracle that gives output as sid,serial#,transaction start time,sql_id,transaction id.
What I need to do is, whenever a transaction's start time is more than 1 hour behind the system time, I need to run another query with that sql_id and send it as an email.
How do I compare this time output from ORACLE sql and compare it with the system time?
I need to automate this process and add it to the cron on UNIX.
Please help!
The function SYSDATE returns the current system date. When you subtract two dates, you get a difference measured in days. Multiply by 24 and you get a difference in terms of hours.
SELECT *
FROM v$transaction
WHERE (sysdate - start_date)*24 > 1
will give you the transactions that started more than 1 hour ago. You can also use interval arithmetic if you find that clearer.
SELECT *
FROM v$transaction
WHERE sysdate - interval '1' hour > start_date