If statements in WHERE clause - oracle

Is it possible in Oracle to put conditional IF statements in the WHERE clause?
I want to filter all rows with an end date before today. And if the end date is empty, it should not filter on it. I've tried this:
SELECT discount_amount
FROM vw_ph_discount_data
WHERE sysdate > start_date
AND
IF
end_date IS NOT EMPTY
THEN
sysdate < end_date
But I get "invalid relational operator".

You can try:
SELECT discount_amount
FROM vw_ph_discount_data
WHERE sysdate > start_date
AND sysdate < nvl(end_date,sysdate+1)

I don't think that if-else statements can be used in pure Sql code. You need to use stored procedure to achieve your aim. I suppose in your case you can use the code below:
DECLARE
DATE end_date
BEGIN
IF end_date IS NOT NULL THEN
SELECT discount_amount
FROM vw_ph_discount_data
WHERE sysdate > start_date AND sysdate < end_date;
END IF;
END;

Even if it's possible, it's not a good idea. Per-row functions will destroy performance.
In this case, the best way is to probably just union two exclusive queries:
SELECT discount_amount
FROM vw_ph_discount_data
WHERE sysdate > start_date
AND end_date IS NULL
UNION ALL
SELECT discount_amount
FROM vw_ph_discount_data
WHERE sysdate > start_date
AND end_date IS NOT NULL
AND sysdate < end_date
(changed to NULL from EMPTY since that seems to be what you were after).
Assuming end_date is indexed, this should scream along even though it's two queries. Having to do some extra processing on each and every row returned is rarely a good idea.
Whatever methods you choose to investigate, benchmark them with real world data. The prime directive of optimisation is measure, don't guess.

Couldn't you do this:
SELECT discount_amount
FROM vw_ph_discount_data
WHERE sysdate > start_date
AND (end_date IS EMPTY OR sysdate < end_date)

You could use the IF statement to create multiple queries or try WHERE (end_date IS NULL OR end_date > SYSDATE).
Not sure if you should use IS [NOT] EMPTY on "end_date". See IS EMPTY.

Related

Performance of using cursor PLSQL

I have to get a count using a cursor.
the basic cursor is
get *
from table
In here there are three types of data I want to get the count of.
sysdate < start_date
sysdate > startdate and sysdate < enddate
sysdate > enddate
I have 2 ways of doing this.
Loop the above cursor and use a if condition to get the counts.
Create 3 separate cursors with the conditions and directly get the count
As I have a lot of data what way would be good in perspective of performance ?
Don't use a cursor at all.
Use conditional aggregation. That's almost surely faster than any cursor based approach.
SELECT count(CASE
WHEN sysdate < start_date THEN
1
END) count1,
count(CASE
WHEN sysdate > startdate
AND sysdate < enddate THEN
1
END) count2,
count(CASE
WHEN sysdate > enddate THEN
1
END) count3
FROM elbat;

Loop Insert Date a daily commit day to day timestamp ORACLE

im executing query in oracle. i need insert data everyday commit in looping like this :
DECLARE
start_date NUMBER;
end_date NUMBER;
business_date VARCHAR2 (8);
BEGIN
start_date := TO_NUMBER (TO_CHAR (TO_DATE ('2017-01-01', 'yyyy-MM-dd')));
end_date := TO_NUMBER (TO_CHAR (TO_DATE ('2018-01-01', 'yyyy-MM-dd')));
FOR cur_r IN start_date .. end_date
LOOP
INSERT INTO file_backup
SELECT *
FROM file_core
WHERE TO_NUMBER (TO_CHAR (TO_DATE (datecreated, 'yyyy-MM-dd')))>=start_date+cur_r
AND TO_NUMBER (TO_CHAR (TO_DATE (datecreated, 'yyyy-MM-dd')))<=end_date;
COMMIT;
END LOOP;
END;
I dunt know error this script .. please help me .. btw i newbie in oracle sorry ..
As #boneist pointed out, your manipulation using numbers isn't going to work. You should keep the data type as it is and compare with values of the same data type.
Assuming you have a legitimate need to do this in a loop you could do something like this:
BEGIN
FOR r IN (
select date '2017-01-01' + level -1 as this_date
from dual
connect by level <= date '2018-01-01' - date '2017-01-01'
)
LOOP
INSERT INTO file_backup
SELECT *
FROM file_core
WHERE datecreated >= r.this_date
AND datecreated < r.this_date + 1;
COMMIT;
END LOOP;
END;
/
Or if the data type is actually a timestamp rather than a date as suggested in a comment, something like:
BEGIN
FOR r IN (
select timestamp '2017-01-01 00:00:00'
+ (level -1) * interval '1' day as this_timestamp
from dual
connect by level <= extract(day from timestamp '2018-01-01 00:00:00'
- timestamp '2017-01-01 00:00:00')
)
LOOP
INSERT INTO file_backup
SELECT *
FROM file_core
WHERE datecreated >= r.this_timestamp
AND datecreated < r.this_timestamp + interval '1' day;
COMMIT;
END LOOP;
END;
/
... though you might want to work on the condition for the connect-by query, e.g.
FOR r IN (
select timestamp '2017-01-01 00:00:00'
+ numtodsinterval(level -1, 'DAY') as this_timestamp
from dual
connect by timestamp '2017-01-01 00:00:00'
+ numtodsinterval(level -1, 'DAY') < timestamp '2018-01-01 00:00:00'
)
LOOP
...
or as #boneist suggested in a comment, with a simpler loop:
BEGIN
FOR num_days in 0..(date '2018-01-01' - date '2017-01-01' - 1)
LOOP
INSERT INTO file_backup
SELECT *
FROM file_core
WHERE datecreated >= timestamp '2017-01-01 00:00:00'
+ numtodsinterval(num_days, 'DAY')
AND datecreated < timestamp '2017-01-01 00:00:00'
+ numtodsinterval(num_days + 1, 'DAY');
COMMIT;
END LOOP;
END;
/
The main problem with this approach is restartability. If there is an error part way through the loop you can't just re-run it, as you'd be inserting duplicates.
Multiple inserts and commits are also less efficient that a single insert, or even multiple inserts and a single commit. If you don't have enough undo space to allow a single transaction to do all the work you need you should be fixing the database configuration to allow that, rather than working around it and potentially compromising data integrity.
i need backup this table . and insert only 2 month in new table
That sounds like you need to partition the table by month and use partition swaps to shift old months from the live to the backup table, perhaps. Partitioning costs more but if you have those data volumes it may be justified.
Failing that you could consider renaming your current table to backup, recreating your original table, and just copying the two months' worth of data you want to keep back to that. But that's a one-off thing, you still have the ongoing problem of ageing records out of the main table and into backup. And it has its own issues with dependencies, constraints, etc.
You don't need any loop and you should skip all these TO_CHAR, TO_NUMBER, TO_DATE conversions. Try this:
INSERT INTO file_backup
SELECT *
FROM file_core
WHERE datecreated BETWEEN DATE '2017-01-01' AND DATE '2018-01-01';
Perhaps datecreated has time values different to 00:00:00, in this case you should run
INSERT INTO file_backup
SELECT *
FROM file_core
WHERE TRUNC(datecreated) BETWEEN DATE '2017-01-01' AND DATE '2018-01-01';
or in case datecreated is a VARCHAR2 data type rather than DATE run
INSERT INTO file_backup
SELECT *
FROM file_core
WHERE TO_DATE(datecreated, 'YYYY-MM-DD') BETWEEN DATE '2017-01-01' AND DATE '2018-01-01';
Assuming your datecreated column is of data type DATE and you only want one copy of the rows of the table then you do not need PL/SQL:
INSERT INTO file_backup
SELECT *
FROM file_core
WHERE datecreated BETWEEN DATE '2017-01-01' AND DATE '2018-01-01';
COMMIT;
You could use a DATE literal to set the start_date and end_date and use a loop like this.
DECLARE
start_date NUMBER;
end_date NUMBER;
BEGIN
start_date := DATE '2017-01-01';
end_date := DATE '2018-01-01';
FOR cur_r IN 0 .. (end_date - start_date)
LOOP
INSERT INTO file_backup
SELECT *
FROM file_core
WHERE TRUNC (datecreated) = start_date + cur_r;
COMMIT;
END LOOP;
END;
Why wouldn't you do it using a simple INSERT, such as
INSERT INTO file_backup
SELECT *
FROM file_core
WHERE datecreated BETWEEN DATE '2017-01-01' AND DATE '2018-01-01';
If you're just practicing PL/SQL and loops, well, remove COMMIT out of the LOOP. Both START and END_DATE should be "converted" to a number using a proper format mask (i.e. yyyymmdd). FOR loop's index should go from 1 to a difference between END and START_DATE.
[EDITED, after reading MT0's comment]
[EDITED #2, after reading some more comments]
Bah, my code is rubbish, should've thought about what I'm doing. Basically, if I meant to write it properly, it would have looked like #Kaushik Nayak's, and there's really no point in doing it twice.

Oracle query not giving result for current_date

What is the query in Oracle to fetch the data for current_date
the column end_date is like the following
end_date
27-10-16 03:35:00.000000000 PM
23-11-16 11:15:00.000000000 AM
02-11-16 03:00:00.000000000 PM
08-11-16 09:00:00.000000000 AM
Like I am running the following query as
Select * from table1
where end_date < TO_DATE('2017-04-11 00:00:00', 'YYYY-MM-DD HH24:MI:SS')
it is running successfully, but when i replace the query with the current date ... it is not giving the results
Select * from table1
where end_date < TO_DATE(current_date, 'YYYY-MM-DD HH24:MI:SS')
could someone tell me what is the cause the second query is not giving results.
CURRENT_DATE returns date. There is no need to use TO_DATE. The below query should be enough.
Select * from table1
where end_date < current_date;
If you run the below query you'll understand what went wrong for you. Year becomes 0011.
SELECT TO_DATE(current_date, 'YYYY-MM-DD HH24:MI:SS') FROM DUAL;
Please note that CURRENT_DATE returns the current date in the session time zone. SYSDATE returns the current date and time set for the operating system on which the database resides. This means that CURRENT_DATE and SYSDATE can return different results. You can have a look at this
The query worked like this :
Select * from table1
where trunc(end_date) < trunc(sysdate)
Trunc is used to compare the both dates and it fetch the results.
CURRENT_DATE is already a DATE value. You can format the output using to_char if you want.
end_date < CURRENT_DATE should do the job. Or you can set the nls parameter accordingly for a better readability.
If you are comparing only date, without timestamp, you can go with trunc()

Oracle Query is returning value when setting value to it, but not returning value when passing date value

My below query is not returning any values when i pass the START_DATE and END_DATE as parameter to my oracle procedure. But when i set actual date in the query i am getting results. Kindly help me what is the mistake i have done here. Thanks in advance.
SELECT INCIDENT_ID
FROM INC_SM1 I
WHERE
I.CLOSE_TIME >= TO_DATE(START_DATE,'DD-MON-YYYY HH24:MI:SS')
AND I.CLOSE_TIME < TO_DATE(END_DATE,'DD-MON-YYYY HH24:MI:SS')
The below query is returning values, when I run my procedure
SELECT INCIDENT_ID
FROM INC_SM1 I
WHERE
I.CLOSE_TIME >= TO_DATE('01-JUL-2013 00:00','DD-MON-YYYY HH24:MI')
AND I.CLOSE_TIME < TO_DATE('01-AUG-2013 00:00','DD-MON-YYYY HH24:MI')
Note: My input to procedure is TO_DATE('01-JUL-2013 00:00','DD-MON-YYYY HH24:MI')
Since you're already forcing the date format, you can use the following query instead:
SELECT INCIDENT_ID
FROM INC_SM1 I
WHERE
I.CLOSE_TIME >= START_DATE
AND I.CLOSE_TIME < END_DATE

Date function on oracle

I've got a question about date function on oracle.
I have the following table
statistic_table(
pages AS varchar(10),
date_created AS date
);
I have the following sql
SELECT COUNT(*) FROM statistic_table WHERE date_created BETWEEN sysdate-5 AND sysdate-1
and
SELECT COUNT(*) FROM statistic_table WHERE date_created BETWEEN to_date('12-AUG-2011') AND to_date('16-AUG-2011');
the question is, why is it return different numbers. assuming sysdate-5 returns 12-aug-2011 and sysdate-1 returns 16-aug-2011
Any help would be much appreciated!
Cheers,
sysdate - 5 will give you a date with the current time. So if I ran it at 1pm precisely, the query would be equivalent to:
select (*)
FROM statistic_table
WHERE date_created BETWEEN to_date('12-Aug-2011 13:00:00')
AND to_date('16-Aug-2011 13:00:00')
whereas the second query is:
select (*)
FROM statistic_table
WHERE date_created BETWEEN to_date('12-Aug-2011 00:00:00')
AND to_date('16-Aug-2011 00:00:00')
you should probably try this instead:
select (*)
FROM statistic_table
WHERE date_created BETWEEN trunc(sysdate) -5
AND trunc(sysdate) -1
A date in Oracle is a point in time with a precision of a second.
SYSDATE returns the current date and time and is therefore not the same as to_date('17-AUG-2011'):
SQL> SELECT to_char(sysdate, 'dd-mon-yyyy hh24:mi:ss') FROM DUAL;
TO_CHAR(SYSDATE,'DD-MON-YYYYHH
------------------------------
17-aug-2011 15:52:13
Use the TRUNC function if you only want the date component:
SQL> SELECT to_char(trunc(sysdate), 'dd-mon-yyyy hh24:mi:ss') FROM DUAL;
TO_CHAR(TRUNC(SYSDATE),'DD-MON
------------------------------
17-aou-2011 00:00:00
Because SYSDATE includes a time component, so if the current time is 11:22:33, then
SELECT COUNT(*) FROM statistic_table
WHERE date_created BETWEEN sysdate-5 AND sysdate-1
is actually equivalent to
SELECT COUNT(*) FROM statistic_table
WHERE date_created BETWEEN to_date('12-AUG-2011 11:22:33','DD-MON-YYYY HH24:MI:SS')
AND to_date('16-AUG-2011 11:22:33','DD-MON-YYYY HH24:MI:SS')
To avoid the time component do this:
SELECT COUNT(*) FROM statistic_table
WHERE date_created BETWEEN TRUNC(sysdate)-5 AND TRUNC(sysdate)-1
An Oracle DATE always has a day and a time component.
sysdate-5 returns a date exactly 5 days ago. If today is August 17 at 10 AM, for example, sysdate-5 returns August 12 at 10 AM.
to_date('12-AUG-2011', 'DD-MON-YYYY'), on the other hand, returns August 12 at midnight. So it returns a date that is 10 hours earlier than sysdate-5.
sysdate auto returns with a time component as mentioned by the previous answers.
When using to_date it is converting a string to a date. With this being said you can pass in parameters to make it return the same thing.
Have a look at this link that explains it.
to_date parameters

Resources