Rewrite condition WHERE TRUNC (data_operac) = to_date(:1, 'YYYY-MM-DD')
Hi,
I've got query with condition
WHERE TRUNC (data_operac) >= to_date(:1, 'YYYY-MM-DD') there is index b-tree on data_operac but cant be used (because of trunc) .
Is there any way I can rewrite that condition to make index usage possible ?
Regards.
Greg
Like this?
WHERE data_operac >= to_date(:1, 'YYYY-MM-DD')because following is always true
data_operac >= TRUNC (data_operac)
Similar Messages
-
Help w/ "where TRUNC(date_field)"
I'm a "newbie" to pl/sql - Oracle and I'm hoping someone can help me.
I need to extract records based on a given date range and I've put together the following query:
SELECT *
FROM abc.table
WHERE TRUNC(ld_dt) = TO_DATE('11-SEP-10','DD-MON-YY')
When I run this thru SQLplus or ODI any date fields output as 04/11/2010 12:00:00 (or 00:00:00) - I'm loosing the time portion of the field... I've narrowed it down to the WHERE TRUNC(). This query runs correctly in SQLplus & ODI if I remove the WHERE clause and format any date columns as TO_CHAR(ld_dt,'MM-DD-RR HH:MI:SS')... Its been a bit mind-boggling because it works in TOAD but I need it to work with SQLplus or ODI.
NLS_DATE_FORMAT: DD-MON-RR
NLS_DATE_LANGUAGE: AMERICAN
We’re running ODI 10.1.3.6.2 – 10g R2 Database
Thanks !Hi,
I am not sure what you are doing. I cannot reproduce your problem.
Please post some create table and insert statement, test cases of your queries that are reproducible.
Try it in sqlplus and copy paste your output here.
SQL> alter session set nls_date_format='MM/DD/YYYY HH24:MI:SS';
Session altered.
SQL>
SQL> select * from test;
DT LD_DT
04/04/2011 16:54:45 04/04/2011 16:53:45
04/04/2011 16:54:15 04/04/2011 16:53:45
04/04/2011 16:54:05 04/04/2011 16:53:45
04/04/2011 16:54:00 04/04/2011 16:53:45
SQL> select * from test where trunc(ld_dt)=trunc(sysdate);
DT LD_DT
04/04/2011 16:54:45 04/04/2011 16:53:45
04/04/2011 16:54:15 04/04/2011 16:53:45
04/04/2011 16:54:05 04/04/2011 16:53:45
04/04/2011 16:54:00 04/04/2011 16:53:45
SQL>
SQL> SELECT * FROM test WHERE TRUNC(ld_dt)=TO_DATE('04-APR-11','DD-MON-YY');
DT LD_DT
04/04/2011 16:54:45 04/04/2011 16:53:45
04/04/2011 16:54:15 04/04/2011 16:53:45
04/04/2011 16:54:05 04/04/2011 16:53:45
04/04/2011 16:54:00 04/04/2011 16:53:45
SQL> -
TO_DATE with YYYY format mask behavior
I was selecting records from a table where a date in the table is prior to a given year:
SELECT * FROM the_table WHERE date_col <TO_DATE('2006','YYYY');
On January 31st, this worked fine.
On February 1st, I started seeing records from January 2006.
When I run the following in SQL*Plus, the result is 2/1/2006:
SELECT TO_DATE('2006','YYYY') FROM DUAL;
I have used this format before (but perhaps not in the exact situation) and I've never seen this issue before.
SQL*Plus is version 8.0.6.0.0. The result occurs on database versions 10.2.0.3.0 and 10.2.0.4.0.
I'm not understanding why the current date would have any effect on the SELECTed date.
Any insights would be appreciated.
Thanks!Hi,
When you don't spedcify the month in TO_DATE, it defaults to the current month.
SELECT SYSDATE
, TO_DATE ('2006', 'YYYY')
FROM dual;just produced
02-Feb-2009 01-Feb-2006You have to give the month:
TO_DATE ('200601', 'YYYYMM')Edited by: Frank Kulash on Feb 2, 2009 3:21 PM
Or use TRUNC (..., 'YYYY'), as Walter showed you. -
Conversion bug of to_date(x,yyyy/mm/dd)
Dear Madam or Sir,
my name is Christian Butzke and I am working for Perpetuum Co. Ltd. in Japan.
I am using Oracle Database 9. I encountered maybe a bug of the to_date sql function.
I am not sure where to post bug reports. I search one hour on the Oracle homepage for an email address, but there was none, so I tried this forum. If this forum does not handle bugs, I am sorry.
The following script should show after each select command one row with 2005-01-01 00:00:00.0.
CREATE TABLE test ("df" DATE);
INSERT INTO test VALUES ('2005-01-01');
SELECT TO_DATE("df",'yyyy/mm/dd'), "df" FROM test;
SELECT * FROM test WHERE "df">='2005-01-01';
SELECT * FROM test WHERE TO_DATE("df",'yyyy/mm/dd')>='2005-01-01';
DROP TABLE test;
Instead the first select returns:
TO_DATE(DF,YYYY/MM/DD) DF
0005-01-01 00:00:00.0 2005-01-01 00:00:00.0
The second select returns correctly the row
DF
2005-01-01 00:00:00.0
However, the last select returns nothing.
This should be a bug, shouldn't it be?
With regards,
Christian-Manuel Butzke
Perpetuum Co. Ltd.I am using Oracle Database 9. I encountered maybe a
bug of the to_date sql function.the bug is in your code, not in oracle!
CREATE TABLE test ("df" DATE);
INSERT INTO test VALUES ('2005-01-01');this is poor coding, you should use
insert into test values (to_date('2005-01-01', 'YYYY-MM-DD'));
SELECT TO_DATE("df",'yyyy/mm/dd'), "df" FROM test;this is also buggy code, you should use
select "dt" from test
to_date(date) is a source of errors
SELECT * FROM test WHERE "df">='2005-01-01';again, this is poor coding, you compare string with date. Prefer where "df" >= to_date('2005-01-01', 'YYYY-MM-DD')
SELECT * FROM test WHERE
TO_DATE("df",'yyyy/mm/dd')>='2005-01-01';same as above -
Conditional where clauses in database adapter
I am trying to write a query in a database adapter that allows for conditional where clauses. Specifically, I want to include or not include a where clause (and clause) if an input variable is empty or not. Can a prepared statement with lexical parameters be used in a database adapter? Or is there another way to do this?
Thanks in advance.I am afraid that you can only bind parameters to the SQL, you can't bind the SQL itself to the SQL. We do have QueryByExample support, where based on what fields are set in an example object, it will construct a different SQL select.
If you pass in
<Address>
<street/>
<city>
<state>CA</state>
</Address>
it will return all addresses in California. If you pass in:
<Address>
<street/>
<city>Los Angeles</city>
<state/>
</Address>
it will return all addresses in Los Angeles.
Thanks
Steve -
To_date('2008','yyyy') = April 1, 2008
I just found this out.
I have been using it thinking it would be January 1st for quite a while. I can see it being useful, but I can't see where it is documented - is it an NLS_ setting?
JonI personally wouldn't rely on any assumptions when you're using to_date on a string, other than if you don't supply hours, minutes or seconds, they'll be set to 0.
If you're after just the year, try trunc(<your date>, 'yyyy') - that will set it to 1st Jan at midnight of the year in the date passed in. -
Integer parameters not acceptible in WHERE clause with TO_DATE function.
Please tell me why the following query is not acceptible in a BI Publisher (11.1.1.5) data set.
SELECT * FROM all_tables
WHERE
LAST_ANALYZED >= TO_DATE(:yyyy || '-' || :startmo || '-' || :startday, 'YYYY-MM-DD')
AND LAST_ANALYZED < TO_DATE(:yyyy || '-' || :endmo || '-' || :endday, 'YYYY-MM-DD')
Attempting to save the query results in:
"ORA-01858: a non-numeric character was found where a numeric was expected"
It doesn't work even if you surround the parameters with TO_NUMBER().
The query clearly works in SQL Developer.Also, neither of the following work, even though they both work in SQL Developer:
SELECT table_name, last_analyzed
FROM all_tables
WHERE
LAST_ANALYZED >= (TO_DATE(:start_date, 'MM-DD-YYYY') + 1)
SELECT table_name, last_analyzed
FROM all_tables
WHERE
LAST_ANALYZED >= (TO_DATE(:start_date, 'MM-DD-YYYY') + INTERVAL '1' DAY)
These don't work in BIP even when the start_date parameter has a format set to the default MM-dd-yyyy. So to sum up, I can't use date parameters, nor can I use integers passed as date parts. What the fuck am I supposed to do?
After four years, your fucking date parameters still don't work! Fuck off Oracle. I'm going home, frustrated with your garbage as usual. -
Plain SQL Conditional WHERE Clause with two sub-selects
Hi SQL Experts,
I need a bit quick of help with a query.
This is a "select" statement for an OBI repository phyiscal table source object.
So I cant put PL/SQL or Stored Procedures in it. I just need
to have two different conditions/selects depending on the user role (this information comes
from the session).
The above, does not work:
--------------ORA00905. 00000 - "missing keyword"
SELECT OFFICENO FROM orgunit
WHERE
CASE WHEN 'SESSION_VARIABLE' LIKE '%globalmanager%' THEN
ASSIGNEDOFFICES =
SELECT DISTINCT ASSIGNEDOFFICES
FROM USERSTABLE INNER JOIN orgunit ON
orgunit.OFFICEKEY = USERSTABLE.OFFICEKEY
WHERE USERSTABLE.USERNAME ='VALUEOF(NQ_SESSION.USER)'
ELSE
OFFICENO =
SELECT DISTINCT OFFICENO FROM USERSTABLE
INNER JOIN orgunit ON
orgunit.OFFICEKEY = USERSTABLE.OFFICEKEY
WHERE USERSTABLE.USERNAME ='VALUEOF(NQ_SESSION.USER)'
END;
Can anyone help?Hi,
I can't tell what you want to do just by looking at code that doesn't do it. Maybe:
SELECT officeno
FROM orgunit
WHERE ( session_variable LIKE '%globalmanager%'
AND assignedofficies IN
SELECT assignedoffices
FROM usertable u1
JOIN orgunit o1 ON o1.officekey = u1.officekey
WHERE u1.username = 'VALUEOF(NQ_SESSION.USER)'
OR ( NVL ( session_variable
) NOT LIKE '%globalmanager%'
AND officeno IN
SELECT officeno
FROM usertable u2
JOIN orgunit o2 ON o2.officekey = u2.officekey
WHERE u2.username = 'VALUEOF(NQ_SESSION.USER)'
There might be some simpler and more efficient way, depending on your tables and your reuirements.
The condition u2.username = 'VALUEOF(NQ_SESSION.USER)' is almost certainly not what you really want.
Whenever you have a question, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables involved, and also post the results you want from that data.
Explain, using specific examples, how you get those results from that data.
Always say which version of Oracle you're using (e.g., 11.2.0.2.0).
See the forum FAQ: https://forums.oracle.com/message/9362002#9362002 -
Conditional where, or PL/SQL block as source for an interactive report?
I have this situation: The main screen of my application shows a list of documents. For now, there has to be a quick "security" built in, so administrators can see all documents, but other users can only see the documents they created (login of creator is stored in the documents table). In the future we'll probably use VPD's to enforce some rules, but for now we need a quick solution.
So i stored the administrators in a seperate table. Now i want the interactive report on the main page act as follows:
- If an admin is logged in, it has to have this query: select field 1, field 2, field 3 from documents
- For all other users: select field 1, field 2, field 3 from documents where creator = :app_user
Is this possible in one interactive report? I could make two reports with the different queries and make them conditional according to who is logged in, but this gives me two interactive reports to maintain. Is it possible to use a PL/SQL block as source for the report? I tried, but I get syntax errors. Is it possible to make the where clause conditional on some way?Hi,
Install Demonstration application to your workspace:
Home>Create Application and select Demonstration Application.
Check page 1 interactive report. That should give idea how you do it.
Br, Jari -
Why does to_date gives yyyy as 0006
Hi ,
this is from my nls_session paramete
PARAMETER VALUE
10 NLS_TIME_FORMAT HH.MI.SSXFF AM
11 NLS_TIMESTAMP_FORMAT DD-MON-RR HH.MI.SSXFF AMIf i execute the followings :
select TO_DATE(SYSDATE, 'DD-MM-YYYY') from dual
results --> 11/20/0006 the year is STRANGE
select TO_DATE(SYSDATE, 'DD-MM-YY') from dual
results --> 11/20/2006 th year is OKappreciate ur advise
tks & rdgsWhy are you using to_date on a date?
to_date accepts a character input value, when you pass a date it gets implicitly converted to a string using the nls_date_format value for the session and then back to a date again.
There is an example Re: TO_DAYS equivalant in Oracle -
Hi,
I have a below query.In last condition (ie. AND sb.timezone in )i have to pass 0 when sa.timezone_subject is null
elae i have to pass (1,2).
for ex if sa.timezone_subject is null then
the last condition should be
AND sb.timezone in(0)
else sb.timezone in(1,2)
query
select sa.timezone_subject,sb.*
from subject_availability sa,SUBJECT_BOUNDARIES sb
where sa.year= sb.year
and sa.month = sb.month
and sa.subject =sb.subject
and sa.subject_option=sb.subject_option
and sa.lvl = sb.lvl
and sa.YEAR = 2011
AND sa.MONTH = 'MAY'
AND sa.SUBJECT = 'BIOLOGY'
AND sa.SUBJECT_OPTION = 'BIOLOGY'
AND sa.LVL = 'HL'
AND sa.language = 'ENGLISH'
AND sb.timezone in()and i have tried using case but it does't worked out please adviceHI,
select sa.timezone_subject,sb.*
from subject_availability sa,SUBJECT_BOUNDARIES sb
where sa.year= sb.year
and sa.month = sb.month
and sa.subject =sb.subject
and sa.subject_option=sb.subject_option
and sa.lvl = sb.lvl
and sa.YEAR = 2011
AND sa.MONTH = 'MAY'
AND sa.SUBJECT = 'BIOLOGY'
AND sa.SUBJECT_OPTION = 'BIOLOGY'
AND sa.LVL = 'HL'
AND sa.language = 'ENGLISH'
AND ( sa.timezone_subject is null AND sb.timezone in(1)
OR sb.timezone in(2,3)
{code}
Regards.
Al -
How create a conditional "where"?
Hi,
I have a form where I put some values and after they are used to do a query when the user click on button "submit". Maybe the user write just one or two fields from many and these fields I want to use on a where clause. So, in the 'where' clause should have just the items who was completed by user and will be used the condition 'AND' not 'OR'. Can someone help me?
Thanks, sorry my English, but I from Brazil and I speak a lot of this.Felipe,
Read this thread:
How to retrieve 2 values from a table in a LOV
and have a look in my Demo Application:
http://htmldb.oracle.com/pls/otn/f?p=31517:1
Denes Kubicek
http://deneskubicek.blogspot.com/
http://www.opal-consulting.de/training
http://htmldb.oracle.com/pls/otn/f?p=31517:1
------------------------------------------------------------------- -
Good afternoon,
I have the following issue,
I have that do a LOOP to a table Internal, ie.
Loop at it_tabla where lgart = '0100' or lgart = '0200' or lgart = '0300' or lgart = '0400' or lgart = '0500' or lgart = '0600' or lgart = '0700' or lgart = '0800' .
Endloop.
The question is, Is there any way , in the which not have that do many or in the sentence LOOP?
Kind regards.I think Rob means that it will go through the table once.
It's true for Standard tables. But as it says in the help on the WHERE clause.
While with standard tables all rows of the internal table are checked for the logical expression of the WHERE- addition, with sorted tables and hash tables (as of Release 7.0) you can achieve optimized access by checking that at least the beginning part of the table key in sorted tables and the entire table key in hash tables is equal in the logical expression through queries linked with AND. Optimization also takes effect if the logical expression contains other queries linked with AND with arbitrary operators
In this case though, the queries are linked with OR - so you still get all rows checked!
Now, if it_tabla is a sorted table with key lgart, then you could do the following:
PERFORM read_the_table USING: '0100', '0200',...
FORM read_the_table USING i_lgart TYPE ...
data: l_tabix TYPE sytabix.
READ TABLE it_table WITH TABLE KEY lgart =i_lgart TRANSPORTING NO FIELDS.
l_tabix = sy-tabix.
LOOP AT it_table FROM l_tabix INTO ls_wa.
IF ls_wa-lgart NE i_lgart.
EXIT.
ENDIF.
" Do stuff
ENDLOOP.
ENDFORM.
matt -
Modify Function and Conditional Where
I am a newbie to the Oracle world. I am working on an Oracle 8.1 system. I have experience with Microsoft SQL Server.
I have two issues that are related to Oracle, many that are not.
- first how do I go about altering a function that is currently in the database, I would like to pull the contents into an editor so that I can see how it is currently working
- second, i have a parameter that is being passed into the function above, it usually holds a value but now the developers would like to be able to pass a % meaning don't use the criteria. Is it possible to modify the function to use the parameter in the where clause if it doesn't equal %, but don't include it if it does
Thank you for assisting me while I travel in the darkThis is one way:
AND ( SEWN.STYLE.BUSINESS_UNIT_ID=SEWN.STYL_COLO_IDENTIFIER.BUSINESS_UNIT_ID
and SEWN.STYLE.STYLE_ID=SEWN.STYL_COLO_IDENTIFIER.STYLE_ID )
AND ( SEWN.LABEL.BUSINESS_UNIT_ID = BusinessUnitId
AND SEWN.STYLE.STYLE_ID >= DECODE(FromStyleID, '%', SEWN.STYLE.STYLE_ID, FromStyleID)
AND SEWN.STYLE.STYLE_ID <= DECODE(ToStyleID, '%', SEWN.STYLE.STYLE_ID, FromStyleID));[pr]
John -
Conditional WHERE clause for APEX
Dear SQL gurus,
I know that there's a APEX forum, but I think that there maybe an sql based solution for this...
What I have is a query like this:
SELECT COMMENT_ORIG, COMMENT_CHARGING
FROM CL_INVENTORY_OPEN_OUT
WHERE INCIDENTAL_FK=:P13_INCIDENTAL_FK where :P13_INCIDENTAL_FK is a field in the Web Based GUI...
What I need is to change the WHERE clause to something like this
WHERE
DECODE(:P13_INCIDENTAL_FK, NULL, 1=1, INCIDENTAL_FK=:P13_INCIDENTAL_FK)so to say:
if the : P13_INCIDENTAL_FK is null, then let the clause be 1=1, otherwise let it be NCIDENTAL_FK=:P13_INCIDENTAL_FK.
Is there a way to do this inside a sql statement?
Best Regards and THX
JohannSELECT COMMENT_ORIG, COMMENT_CHARGING
FROM CL_INVENTORY_OPEN_OUT
WHERE INCIDENTAL_FK=:P13_INCIDENTAL_FKDo not know how you are using the query, but you might try something like this
BEGIN
CASE :P13_INCIDENTAL_FK
WHEN NULL THEN
SELECT COMMENT_ORIG, COMMENT_CHARGING
FROM CL_INVENTORY_OPEN_OUT
ELSE
SELECT COMMENT_ORIG, COMMENT_CHARGING
FROM CL_INVENTORY_OPEN_OUT
WHERE INCIDENTAL_FK=:P13_INCIDENTAL_FK
END CASE;
END;Edited by: ajallen on May 7, 2010 6:44 AM
Maybe you are looking for
-
Problem with a resultset before a delete in a stored procedure
We have been using the Microsoft provider in the past. However, we now need to use CLOBs, so we would like to move to the Oracle provider. However, we have several stored procedures that select some data out of a table, compile a result set, and then
-
Alternating Background color for Detail Records
Post Author: jbecker CA Forum: Formula I have found other posts concerning thing similar to this but not quite the same. I have an 8.5 report that needs to have everyother record in the detail section to be a different color. The problem is that it
-
Hat der iMac 27 Zoll einen Mini USB Anschluss ?
Hey , ich hab mich gefragt ob der iMac mit 27 eigentlich einen Mini USB Anschluss hat ? Danke im Vorraus
-
Search help exit while user double click
Hi, As it was done earlier but while test facing some issue,Double clicked value from the output is not returned to input screen whatever thevalues only last value is returning to the screen please ooverview some of my code, IF callcontrol-step = 'DI
-
The setting "use touch ID for iTunes & App Store" isn't turning on..help?
please help i'm new to ios7 and im very confused