oracle datetime field indexing - oracle

How do we perform indexing on a datetime field in oracle. We should be able to search for a specific year
Thanks

To create an index in Oracle, use:
CREATE INDEX your_index_name ON your_table_name(your_column_name)
For more info about Oracle index creation, read this link.
Correction & Clarification
If you use a function to isolate a component of a date (IE: EXTRACT, or TRUNC), an index on the column will not help. But an index will help if you provide a date range:
WHERE your_date_column BETWEEN TO_DATE('2010-01-01', 'YYYY-MM-DD')
AND TO_DATE('2010-12-31', 'YYYY-MM-DD')
You can however create function based indexes in Oracle:
CREATE INDEX your_index_name
ON your_table_name(EXTRACT(YEAR FROM your_column_name))
...which DBAs loath with a passion.

You can index a DATE column (which stores date and time in Oracle) directly:
CREATE INDEX ix ON table (column)
Oracle will be able to use this index directly if you build your query so as to perform a RANGE SCAN. For example, if you want to retrieve rows from 2010:
SELECT ...
FROM table
WHERE column >= DATE '2010-01-01'
AND column < DATE '2011-01-01'
This index can also be used to answer queries for a specific month, day or any other range.

Add an index which is not bound to a column, but an expression that extract the year from that column:
create index sample_index on YourTable (extract(year from YourDateColumn)) tablesapce YourIndexSpace;
When you query the table using that expression, Oracle will use the index.

Just create index like shown above. DO NOT USE TRUNC FUNCTION, because it ignores any indexes. For instance, my datecreate field has next format 03.12.2009 16:55:52 So I used to use
trunc(datecreate, 'dd')=to_date(to_char(sysdate,'dd.mm.yyyy'),'dd.mm.yyyy')
and it worked very slowly(about 5 sec)!!! Now I use next expression:
datecreate>=to_date(to_char(sysdate,'dd.mm.yyyy'),'dd.mm.yyyy') and sw.datecreate<to_date(to_char(sysdate+1,'dd.mm.yyyy'),'dd.mm.yyyy')
and my query executes in 0,01 sec

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.

Query not filtering with date in Oracle

There are records in table for particular date. But when I query with that value, I am unable to filter the records.
select * from TBL_IPCOLO_BILLING_MST
where LAST_UPDATED_DATE = '03-09-21';
The dates are in dd-mm-yy format.
To the answer by Valeriia Sharak, I would just add a few things since your question is tagged Oracle. I was going to add this as a comment to her answer, but it's too long.
First, it is bad practice to compare dates to strings. Your query, for example, would not even execute for me -- it would end with ORA-01843: not a valid month. That is because Oracle must do an implicit type conversion to convert your string "03-09-21" to a date and it uses the current NLS_DATE_FORMAT setting to do that (which in my system happens to be DD-MON-YYYY).
Second, as was pointed out, your comparison is probably not matching rows due LAST_UPDATED_DATE having hours, minutes, and seconds. But a more performant solution for that might be:
...
WHERE last_update_date >= TO_DATE('03-09-21','DD-MM-YY')
AND last_update_date < TO_DATE('04-09-21','DD-MM-YY')
This makes the comparison without wrapping last_update_date in a TRUNC() function. This could perform better in either of the following circumstances:
If there is an index on last_update_date that would be useful in your query
If the table with last_update_date is large and is being joined to other tables (because it makes it easier for Oracle to estimate the number of rows from your table that are inputs to the join).
Your column might contain hours and seconds, but they can be hidden.
So when you filter on the date, oracle implicitly adds time to the date. So basically you are filtering on '03-09-21 00:00:00'
Try to trunc your column:
select * from TBL_IPCOLO_BILLING_MST
where trunc(LAST_UPDATED_DATE) = '03-09-21';
Hope, I understood your question correctly.
Oracle docs

How to create index when there is a arithmetic operation done on that column

I have a query that selects records from a table that are older than 72 days.
SELECT id FROM TABLE_NAME WHERE TIMESTAMP <= SYSDATE - INTERVAL '72' HOUR;
The performance of this query is horrible, so I have added an index to the TIMESTAMP column.
This works fine with thousands of records, but when the record count is 10 million (even more, sometimes), I hardly see any performance improvement with the index.
My guess is that the arithmetic operation is killing the performance of the query.
Please tell me if there are any other approaches to speeding up this query.
Assuming that the timestamp column is of the type TIMESTAMP, the problem is that the implicit conversion from DATE (which is returned by SYSDATE) to TIMESTAMP kills the index.
You could add a function-based index or you could change the use of SYSDATE to SYSTIMESTAMP.

How is the best way to generate a calendar table in PowerCenter?

I must generate a table of calendar dates from dateIni to dateEnd in Powercenter Designer.
dateIni is is fixed, for example '2013-01-01'
dateEnd is sysdate + 'n' months
I'm trying to generate from a java tranformation, that can generate several dynamic rows but needs an input row and I do not have any input... it there any other better approach using seq generator???
As an example table content result must be
date
=======
'2013-01-01'
'2013-01-02'
'2013-01-03'
...
...
'2016-03-10'
You can pass a single input row from any source into the Java transformation and then generate rows with consecutive dates in a loop.
You can create a simple table with two columns - dateIni and dateEnd. It will contain a single row that will both kickstart the Java code and provide configuration for the mapping.
When working with an Oracle database you can also use the following query in your source qualifier:
SELECT level
FROM dual
CONNECT BY
level <= 1000 --(or any other number)
This will generate 1000 rows.
With an Expression-transformation you can change this into dates:
ADD_TO_DATE(to_date('20190101','yyyymmdd'), 'DAY',Level)

Max value of VARCHAR field oracle

I have a field that I would like to get the max value of but it is a varchar2(6) field, all numerals. I am unable to change the type of field
The field is a date listed like this 201307 for July 2013. Just using MAX does not work.
Using oracle.
SELECT MAX(date) "MostRecent" FROM tablename;
Should work. YYYYMM is going to sort as newest first in a MAX aggregate or analytic function in Oracle.

Resources