How to keep a null date value from reverting back the default date after page refresh? (Oracle APEX) - oracle

I'm pretty new to APEX and programming as a whole, so bear with me. I have a table I created using dynamic PL/SQL and have a TO and FROM datepicker to set the range of rows to select. It is functioning great so far but I'm having a problem with the defaults for both datepickers. I've set the FROM datepicker to default to one year in the past and the TO datepicker is set to the current date. I've set them both using a SQL expression in the default field. Whenever you change the date and refresh, the table selects the correct rows. BUT if you want to remove the dates in both pickers so that you can see every row in the table, the null values are being reset to the default values after refreshing the page. I feel like there's probably a very simple way to fix this, but I'm at a loss. Any help with this would be greatly appreciated!

An item gets its value from session state; if it doesn't exist, then from source; if it doesn't exist (and the item is still NULL), then default value is used.
Therefore, what you did is just the opposite of what you want - if everything else is NULL, default value is used.
What to do? Don't set default value for the item - use it directly in report's WHERE clause. Something like this:
select whatever
from your_table
where some_conditions
and date_column between nvl(:P1_DATE_FROM, add_months(trunc(sysdate), -12))
and nvl(:P1_DATE_TO , trunc(sysdate))
So:
if P1_DATE_FROM and/or P1_DATE_TO are set, these values will be used
if they are NULL, they will be set to previous year and today
Of course, you'd remove both item's default values.

Related

Datedim function not returning yesterdays date webi

My Datedim function is not returning yesterdays date in webi, any ideas on how to show 13/04/2022, even if it has null values?
Thanks
If you have gaps in your date data the simplest way to fill them in is to create a variable with the TimeDim() function. However, that will not work for you since you do not have a true gap because your missing date is at the end.
You need a data source with all the dates you want to display regardless of if you have data for those dates or not and then merge on your date dimension. I answered a question very similar to this here. I am copying my answer from there below...
The TimeDim() function will fill in the empty periods in your time
data. The problem with that though is if it is the end of your date
range that is missing data those dates will not show up. Let me show
you what I mean. Here is my sample data from 12/01/2021 through
12/26/2021 (note missing dates) in the table on the left. The table on
the right is the my Var Data Date TimeDim variable defined as…
=TimeDim([Data Date]; DayPeriod)
So we have our missing dates in the middle, but not at the end
(12/25/2021 and 12/26/2021). To get those dates you need a query to
return all the dates in your specified range. If you have a universe
based on a
calendar
you could use that. Free-hand SQL based on a calendar table would
suffice as well.
If you have neither of those we can still get it to work using
free-hand SQL with a CTE. This is SQL Server syntax. You will have to
modify this SQL to work for whatever database platform you have if it
isn’t SQL Server.
Here is the SQL…
;with dates ([Date]) as (
Select convert(date,‘2021-12-01’) as [Date] – Put the start date here
union all
Select dateadd(day, 1, [Date])
from dates
where [Date] < ‘2021-12-26’ – Put the end date here
)
select [Date]
from dates
option (maxrecursion 32767) – Don’t forget to use the maxrecursion option!
Source: Generate a Date Table via Common Table Expression (CTE) |
Data and Analytics with Dustin
Ryan
Here is a
demo.
Now that you have a query returning all of the dates in your range
you can merge the date from that query to your Data Date.
You can then put the date object with all of dates or the Merged Date
in table with any measures from your pre-existing query and there you
have it.
If you need to add dimensions from you pre-existing query I think you
will need to create variables for them with Qualification set to
“Detail” and the Associated dimension set to “Merged Date” (or
whatever you called it). And if you do that I believe you will also
need to check “Avoid duplicate row aggregation” check box within the
Format Table properties.
Let us know how it goes.
Hopefully that will get you on the right track.

How to disable past dates in oracle apex with no validation

I am disabling past dates in Date Picker in Apex v4.1 by entering in Setting as Minimum Date +0d
But when I am trying to edit and save data next day that field is showing error. Can somebody please help.
I can't view images, but - the way you described it - I'd suggest you to remove +0d in Date Picker item and create your own validation whose type is PL/SQL Function returning error text; it will check whether item value is equal or larger than TRUNC(SYSDATE), e.g.
if :P1_DATE_ITEM < trunc(sysdate) then
return ('Error - date has to be larger or equal to today''s date');
end if;
Then set validation's server side condition to e.g. ITEM IS NULL, while the "item" is table's primary key item (or - if you're working with the ROWID - use it).
Doing so, you'd tell Apex to perform control only for newly created rows (because their primary key column value isn't set yet, nor it has a ROWID as row isn't saved into the database). "Old" rows have it and validation won't fire.

How can Time and Date be automatically updated when a data is inserted or updated in oracle?

I want that every time a data is input in oracle table the date and time must automatically be updated in one of the column named 'CREATION_DATE'.
Setting default value of SYSDATE is more efficient than a trigger. As helpc mentioned, a default value can be overridden if NULL is explicitly provided in the INSERT. If you don't intend to pass date time thru application at all, you can define the column as NOT NULL with a default as sysdate .
A trigger will do what you want. I think something like this is what you are looking for:
CREATE OR REPLACE TRIGGER date_trigger
AFTER INSERT ON your_table
FOR EACH ROW
WHEN (new.your_table> 0)
BEGIN
:NEW.CREATION_DATE:= SYSDATE;
END;
/
Depending on your needs I usually like to add both a create_date and an update_date column to pick up timestamps for changes that may occur later.

Oracle - retrieve date having timestamp values

We have a situation, where we have a table (say Forms) in Oracle DB which has a column (say edition_date) of type date. It was strictly meant to hold the date information in YYYY-MM-DD (ex: 2012-11-23)format with no timestamp value.
Unfortunately, due to a code problem, lot of rows got created with timestamp values. There are tons of records in this table, and I want to update only those rows which had this bad data. I can do that using the query
UPDATE forms SET edition_date = TRUNC( edition_date )
I want to add a where clause to it, so it updates only the bad data. The problem, I am not sure how to retrieve those rows that has timestamp added to it. Below is a snapshot of the data I have:
FORM_ID EDITION_DATE
5 2012-11-23
6 2012-11-23 11:00:15
..
11 2010-07-11 15:23:22
..
13 2011-12-31
I want to retrieve only the row with form ids 6 and 11. I trioed using the length functions but I think that is good for Strings only. Is there any way to do this. Thanks anyone who can help me.
A date has no format; you're only seeing how it's displayed. However, the answer to your question is, effectively, what you've said:
I want to add a where clause to it, so it updates only the bad data.
So, where the date is not equal to the date without time:
update forms
set edition_date = trunc(edition_date)
where edition_date <> trunc(edition_date)
To ensure that this doesn't happen again you can add a check constraint to your table:
alter table forms
add constraint chk_forms_edition_date
check ( edition_date = trunc(edition_date) );
I would advise against all of the above. Don't destroy potentially useful data. You should simply select trunc(edition_date) where you do not want time. You may want to use the time in the future.
You're correct, do not use LENGTH() etc for dates, it depends on your NLS_DATE_FORMAT settings and so could be different on a session-by-session basis. Always use date functions when dealing with dates.

How to set a conditional DEFAULT value to a column in oracle without using triggers?

How to set a conditional DEFAULT value to a column in oracle without using triggers?
I want to achieve following needs:
If "flag1"=1 then Default value to column Newfield must be "4".
If "flag1"=2 then Default value to column Newfield must be "5".
That's what triggers are for. Use them.
A column DEFAULT is not allowed to reference another column, so this is not possible.
You could perhaps just let the default remain as NULL, and then have a view to adjust it like:
create view mytable_view as
select flag1, nvl(newfield, case flag1 when 1 then 4
when 2 then 5
end) as newfield
from mytable;
If column Newfield does not need to be updateable, then you could simply implement it in a view, as Tony showed, or in 11g you could make it a virtual column.
If 'flag' is something session (or statement) level then SYS_CONTEXT may be a way through. Possibly a BEFORE STATEMENT trigger if the issue is avoiding a context switch for every row inserted

Resources