PreparedStatement.setDate(int, Date) problem in WHERE clause
Hello all,
First and foremost, thanks for the help. It is really appreciated.
I am having the toughest time tracking down a problem with how I am handling date setting with my PreparedStatement. The kicker is that I am only having problems with the where clause date sets.
The date is pulled out of the database, put into a string, modified, put back into a date, and then used to query via SQL.
Data selected this way:
"SELECT * FROM table_name"
Data updated this way:
"UPDATE table_name SET name = ? WHERE pk_id = ? AND pk_date = ?"
The java.sql.Date object I use to PreparedStatement.setDate(int, Date) is exact to seconds. It works great if it is not in the where clause (eg Inserts work, Updates work without where clause) which leads me to believe there is truncation of data somewhere between setDate(int, Date) and the native SQL in Oracle 9i.
Is that a correct assumption? How do I solve this?
Thanks,
John
My assumption was correct. If a java.sql.DATE is pulled from a Oracle 9i database it may have more data in it than is allowed for in java.sql.Date. java.sql.Timestamp, on the other hand, does retain all data in the field.
The correct code looks something like this
prepStmt.setTimestamp(Timestamp.valueOf(timeString));
cheers,
John
Similar Messages
-
Performance with dates in the where clause
Performance with dates in the where clause
CREATE TABLE TEST_DATA
FNUMBER NUMBER,
FSTRING VARCHAR2(4000 BYTE),
FDATE DATE
create index t_indx on test_data(fdata);
query 1: select count(*) from TEST_DATA where trunc(fdate) = trunc(sysdate);
query 2: select count(*) from TEST_DATA where fdate between trunc(sysdate) and trunc(SYSDATE) + .99999;
query 3: select count(*) from TEST_DATA where fdate between to_date('21-APR-10', 'dd-MON-yy') and to_date('21-APR-10 23:59:59', 'DD-MON-YY hh24:mi:ss');
My questions:
1) Why isn't the index t_indx used in Execution plan 1?
2) From the execution plan, I see that query 2 & 3 is better than query 1. I do not see any difference between execution plan 2 & 3. Which one is better?
3) I read somewhere - "Always check the Access Predicates and Filter Predicates of Explain Plan carefully to determine which columns are contributing to a Range Scan and which columns are merely filtering the returned rows. Be sceptical if the same clause is shown in both."
Is that true for Execution plan 2 & 3?
3) Could some one explain what the filter & access predicate mean here?
Thanks in advance.
Execution Plan 1:
SQL> select count(*) from TEST_DATA where trunc(fdate) = trunc(sysdate);
COUNT(*)
283
Execution Plan
Plan hash value: 1486387033
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 1 | 9 | 517 (20)| 00:00:07 |
| 1 | SORT AGGREGATE | | 1 | 9 | | |
|* 2 | TABLE ACCESS FULL| TEST_DATA | 341 | 3069 | 517 (20)| 00:00:07 |
Predicate Information (identified by operation id):
2 - filter(TRUNC(INTERNAL_FUNCTION("FDATE"))=TRUNC(SYSDATE@!))
Note
- dynamic sampling used for this statement
Statistics
4 recursive calls
0 db block gets
1610 consistent gets
0 physical reads
0 redo size
412 bytes sent via SQL*Net to client
380 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
Execution Plan 2:
SQL> select count(*) from TEST_DATA where fdate between trunc(sysdate) and trunc(SYSDATE) + .99999;
COUNT(*)
283
Execution Plan
Plan hash value: 1687886199
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 1 | 9 | 3 (0)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | 9 | | |
|* 2 | FILTER | | | | | |
|* 3 | INDEX RANGE SCAN| T_INDX | 283 | 2547 | 3 (0)| 00:00:01 |
Predicate Information (identified by operation id):
2 - filter(TRUNC(SYSDATE@!)<=TRUNC(SYSDATE@!)+.9999884259259259259259
259259259259259259)
3 - access("FDATE">=TRUNC(SYSDATE@!) AND
"FDATE"<=TRUNC(SYSDATE@!)+.999988425925925925925925925925925925925
9)
Note
- dynamic sampling used for this statement
Statistics
7 recursive calls
0 db block gets
76 consistent gets
0 physical reads
0 redo size
412 bytes sent via SQL*Net to client
380 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows
Execution Plan 3:
SQL> select count(*) from TEST_DATA where fdate between to_date('21-APR-10', 'dd-MON-yy') and to_dat
e('21-APR-10 23:59:59', 'DD-MON-YY hh24:mi:ss');
COUNT(*)
283
Execution Plan
Plan hash value: 1687886199
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 1 | 9 | 3 (0)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | 9 | | |
|* 2 | FILTER | | | | | |
|* 3 | INDEX RANGE SCAN| T_INDX | 283 | 2547 | 3 (0)| 00:00:01 |
Predicate Information (identified by operation id):
2 - filter(TO_DATE('21-APR-10','dd-MON-yy')<=TO_DATE('21-APR-10
23:59:59','DD-MON-YY hh24:mi:ss'))
3 - access("FDATE">=TO_DATE('21-APR-10','dd-MON-yy') AND
"FDATE"<=TO_DATE('21-APR-10 23:59:59','DD-MON-YY hh24:mi:ss'))
Note
- dynamic sampling used for this statement
Statistics
7 recursive calls
0 db block gets
76 consistent gets
0 physical reads
0 redo size
412 bytes sent via SQL*Net to client
380 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processedHi,
user10541890 wrote:
Performance with dates in the where clause
CREATE TABLE TEST_DATA
FNUMBER NUMBER,
FSTRING VARCHAR2(4000 BYTE),
FDATE DATE
create index t_indx on test_data(fdata);Did you mean fdat<b>e</b> (ending in e)?
Be careful; post the code you're actually running.
query 1: select count(*) from TEST_DATA where trunc(fdate) = trunc(sysdate);
query 2: select count(*) from TEST_DATA where fdate between trunc(sysdate) and trunc(SYSDATE) + .99999;
query 3: select count(*) from TEST_DATA where fdate between to_date('21-APR-10', 'dd-MON-yy') and to_date('21-APR-10 23:59:59', 'DD-MON-YY hh24:mi:ss');
My questions:
1) Why isn't the index t_indx used in Execution plan 1?To use an index, the indexed column must stand alone as one of the operands. If you had a function-based index on TRUNC (fdate), then it might be used in Query 1, because the left operand of = is TRUNC (fdate).
2) From the execution plan, I see that query 2 & 3 is better than query 1. I do not see any difference between execution plan 2 & 3. Which one is better?That depends on what you mean by "better".
If "better" means faster, you've already shown that one is about as good as the other.
Queries 2 and 3 are doing different things. Assuming the table stays the same, Query 2 may give different results every day, but the results of Query 3 will never change.
For clarity, I prefer:
WHERE fdate >= TRUNC (SYSDATE)
AND fdate < TRUNC (SYSDATE) + 1(or replace SYSDATE with a TO_DATE expression, depending on the requirements).
3) I read somewhere - "Always check the Access Predicates and Filter Predicates of Explain Plan carefully to determine which columns are contributing to a Range Scan and which columns are merely filtering the returned rows. Be sceptical if the same clause is shown in both."
Is that true for Execution plan 2 & 3?
3) Could some one explain what the filter & access predicate mean here?Sorry, I can't. -
Problem with date fields in where clause after changing driver
Hi,
We have changed the oracle driver we use to version 10
and now we have some trouble with date-fields.
When we run this SQL query from our Java program:
select *
from LA_TRANS
where LA_TRANS.FROM_DATE>='20040101' AND LA_TRANS.FROM_DATE<='20041231'
We get this error code:
ORA-01861: literal does not match format string
The query worked fine whit the previous driver.
Is there some way I can run a SQL query with date fields
as strings in the where clause?
Thanks!Keeping the argument of standard SQL or not aside, comparing DATE columns to a constant string (which looks like a date in one of the thousands of the formats available, but not in others) is NOT one of the best programming practices and leads to heartburn and runtime errors (as you have seen yourself).
I would rather compare a DATE to a DATE.
Also, constants like that would be another issue to fix in your code:
http://asktom.oracle.com/pls/ask/f?p=4950:8:6899751034602146050::NO::F4950_P8_DISPLAYID,F4950_P8_CRITERIA:528893984337, -
PL/SQL Evaluation problem of where clause in case of NUMBER column type
I found the following problem in Oracle® Database 2 Day Developer's Guide 11g Release 1 (11.1) B28843-04:
The sole parameter of function eval_frequency is employee_id IN employees.employee_id%TYPE.
An ORA-01422 exception occurs when the execution reaches the following select command
SELECT e.hire_date
INTO hire_date
FROM employees e
WHERE employee_id= e.employee_id;
A possible cause of the error is that the type of employee_id is NUMBER while the employees.employee_id is NUMBER(6,0) . The result of the selection is the same as there were no WHERE clause at all.
Everything worked fine, when I declared a temporary variable of NUMBER(6,0) for storing the actual parameter of function and used this variable in the where clause, but I consider this "solution" as being no solution.
It is pointless to use %TYPE parameter of a function for flexibility if I must degrade this flexibility by a fixed declaration of a temporary variable of the same type as the column in question.
What is wrong?
The Developer'Guide I used, the Oracle Sql Developer I used or the PL/SQL version ?Hi,
Welcome to the forum!
user8949829 wrote:
A possible cause of the error is that the type of employee_id is NUMBER while the employees.employee_id is NUMBER(6,0) . The result of the selection is the same as there I don't think so. The variable employee_id is defined as having the exact same type as the eponymous column. Even if it didn't, I believe Oracle will always implicity convert between datatypes when possible, rounding if necessary. That may cause errors, but it isn't causing this error.
No, the error has nothing to do with the data type. It has to do with the ambiguity of employee_id: is it a column, or is it a variable?
The default is that it means the column name, so
WHERE employee_id = e.employee_idis equivalent to saying
WHERE e.employee_id = e.employee_idwhich isn't quite the same thing as not having a WHERE clause; rows with NULL employee_id would still be excluded, if there were any.
I think it's best not to use variable names that are the same as column names. You could call the variable v_employee_id, or, since it's an IN-argument, in_employee_id.
If you must use a variable that can be mistaken for a column, then qulaify it with the name of the procedure, like this:
WHERE eval_frequency.employee_id = e.employee_id
Everything worked fine, when I declared a temporary variable of NUMBER(6,0) for storing the actual parameter of function That makes sens. You probably gave that variable a name that couldn't be mistaken for a column in the table.
Edited by: Frank Kulash on Jan 12, 2011 8:27 PM -
How to use the MAX DATE condition in WHERE CLAUSE FILEDS
Hi,
I am trying to fetch the result for getting maximun date but when i try to execute the query i am getting the error as follows.
CONDITION : trunc(max(RD.DATERECEIVED)) BETWEEN TO_DATE('01/08/2011','DD/MM/YYYY') AND TO_DATE('01/08/2011','DD/MM/YYYY')
ERROR: Group function is not allowed here.
CHEERS,
PRABU AMMAIAPPANI see a couple of problems here.
First, what you posted below is not a syntactically valid query. It seems to be part of a larger query, specifically, this looks to be only the GROUP BY clause of a query.
Prabu ammaiappan wrote:
Hi,
I Have a group function in the Query. Below is the Query i have used it,
GROUP BY S.FREIGHTCLASS,
R.CONTAINERKEY,
S.SKU,
S.DESCR ||S.DESCRIPTION2,
S.PVTYPE,
RD.LOTTABLE06,
R.WAREHOUSEREFERENCE,
RD.TOLOC,
R.ADDWHO,
R.TYPE,
S.CWFLAG,
S.STDNETWGT,
S.ORDERUOM,
R.ADDDATE,
C.DESCRIPTION,
(CASE WHEN P.POKEY LIKE '%PUR%' THEN 'NULL' ELSE to_char(P.PODATE,'dd/mm/yyyy') END),
NVL((CASE WHEN R.ADDWHO='BOOMI' THEN RDD.SUPPLIERNAME END),SS.COMPANY),
RDD.BRAND,
S.NAPA,
RD.RECEIPTKEY,
R.SUSR4,
P.POKEY,
RDD.SUSR1,
r.STATUS, DECODE(RDD.SUSR2,' ',0,'',0,RDD.SUSR2),
rd.SUSR3Second, the answer to your primary question, "How do I add a predicate with with a MAX() function to my where clause?" is that you don't. As you discovered, if you attempt to do so, you'll find it doesn't work. If you stop and think about how SQL is processed, it should make sense to you why the SQL is not valid.
If you want to apply a filter condition such as:
trunc(max(RD.DATERECEIVED)) BETWEEN TO_DATE('01/08/2011','DD/MM/YYYY') AND TO_DATE('01/08/2011','DD/MM/YYYY')you should do it in a HAVING clause, not a where clause:
select ....
from ....
where ....
group by ....
having max(some_date) between this_date and that_date;Hope that helps,
-Mark -
Problems with WHERE CLAUSE in selects executed by BAPIs
Dear Experts,
I'm trying to make a SAP Java Connector. My problem is that, when they are called by Java, BAPI functions don't run in the same manner as in SAP testing mode .
I tested two BAPI functions called through my connector and I saw with the debbuger that BAPI functions don't execute the select and loop at with a WHERE clause when they are called by Java, therefore they throw errors. But in the SAP testing mode functions work just fine.
I'm new to SAP and I don't know why this happens. Would you like to explain to me how to fix this problem?
Thank you very much.
Kind regards,
Maricica979380 wrote:
Yeah,i m sorry.I m a forum newbie.All right,we ve been given an oracle account for the purpose of this project,an we have connect to the oracle database either via linux terminal with sqlplus,or via oracle client and plsql.So i have to create some tables,then fill these tables with tuples(we ve been given fixed .sql files to do that),and we re suggested to do that from the linux terminal.I did that successfully.Then we have to run some queries either in terminal or plsql.I chose plsql.I connect,i can see the tables that i have created but when i run(in both sql and terminal window) for example the query "select * from table_1",i take 0 rows as a result.I tested to run the query from terminal(with sqlplus) to see if the tables are really empty,but i recieve the results as i should.
I am sorry for my chaotic writing but i cant even explain the problem to myself better and i m not a native.
(im using oracle version 11.2.0.1, this is the first time i m using oracle databases and i m a rookie in databases in general)
Edited by: 979380 on 1 Ιαν 2013 10:33 πμ
[oracle@localhost ~]$ sqlplus user1/user1
SQL*Plus: Release 11.2.0.2.0 Production on Tue Jan 1 11:19:29 2013
Copyright (c) 1982, 2010, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> create table table_1 (id number);
Table created.
SQL> select * from table_1;
no rows selected
SQL> insert into table_1 values(1);
1 row created.
SQL> select * from table_1;
ID
1
SQL> in order to SELECT any rows from any table, first you must INSERT data into the table!
is COPY & PASTE broken for you? -
Urgent: Performance problem with where clause using IN and an OR condition
Select statement is:
select fl.feed_line_id
from ap_expense_feed_lines_all fl
where ((:1 is not null and
fl.feed_line_id in (select distinct r2.object_id
from xxdl_pcard_wf_routing_lists r2,
per_people_f hr2
where upper(hr2.full_name) like upper(:1||'%')
and hr2.person_id = r2.person_id
and r2.fyi_list is null
and r2.sequence_number <> 0))
or
(:1 is null))
If I modify the statement to remove the "or (:1 is null))" part at the bottom of the where clause, it returns in .16 seconds. If I modify the statement to only contain the "(:1 is null))" part of the where clause, it returns in .02 seconds. With the whole statement above, it returns in 477 seconds. Anyone have any suggestions?
Explain plan for the whole statement is:
(1) SELECT STATEMENT CHOOSE
Est. Rows: 10,960 Cost: 212
FILTER
(2) TABLE ACCESS FULL AP.AP_EXPENSE_FEED_LINES_ALL [Analyzed]
(2) Blocks: 8,610 Est. Rows: 10,960 of 209,260 Cost: 212
Tablespace: APD
(6) TABLE ACCESS BY INDEX ROWID HR.PER_ALL_PEOPLE_F [Analyzed]
(6) Blocks: 4,580 Est. Rows: 1 of 85,500 Cost: 2
Tablespace: HRD
(5) NESTED LOOPS
Est. Rows: 1 Cost: 4
(3) TABLE ACCESS FULL XXDL.XXDL_PCARD_WF_ROUTING_LISTS [Analyzed]
(3) Blocks: 19 Est. Rows: 1 of 1,303 Cost: 2
Tablespace: XXDLD
(4) UNIQUE INDEX RANGE SCAN HR.PER_PEOPLE_F_PK [Analyzed]
Est. Rows: 1 Cost: 1
Thanks in advance,
PeterThanks for the reply, but I have already checked what you are suggesting and I am pretty sure those are not causing the problem. The hr2.full_name column has an upper index and the (4) line of the explain plan shows that index being used. In addition, that part of the query executes on its own quickly.
Because the sql is not displayed in an indented format on this page it is a little hard to understand the structure so I am going to restate what is happening.
My sql is:
select a_column
from a_table
where ((:1 is not null) and a_column in (sub-select statement)
or
(:1 is null))
The :1 bind variable is set to a varchar2 entered on the screen of an application.
If I execute either part of the sql without the OR condition, performance is good.
If the :1 bind variable is null with the whole sql statement (so all rows or a_table are returned), performance is still good.
If the :1 bind variable is a not-null value with the whole sql statement, performance stinks.
As an example:
where (('wa' is not null) and a_column in (sub-select statement)) -- fast
where (('wa' is null)) -- fast
where (('' is not null) and a_column in (sub-select statement) -- fast
or
('' is null))
where (('wa' is not null) and a_column in (sub-select statement) -- slow
or
('wa' is null)) -
Date functions in WHERE clause? HELP
The following two queries are identical except for how I supply the date values in the where clause, yet the first query using the a custom my_date function runs 30x slower than the one using the TO_DATE() function. Both return DATE types...any reason for the difference?
SELECT * from outcomes
WHERE start_time >=fn_my_date('START_MONTH')
AND start_time < fn_my_date('END_MONTH')+1
-- This runs 30x faster--
SELECT * from outcomes
WHERE start_time >=TO_DATE('08/01/2001','MM/DD/YYYY')
AND start_time < TO_DATE('08/31/2001','MM/DD/YYYY')+1
On the flip side, I've also experienced queries running slower using the TO_DATE() function vs the LAST_DAY(sysdate) for equivalenet dates.
nullI haven't seen the message coming up when using LENGTH or SUBSTR, but every time I connect to a database or attempt to change my preferences (including the "NLS Parameters: Comp" preference), this appears.
You are attempting to set the preference you should be to switch this from ANSI to something else, but SQL Developer is ignoring the preference setting (which I think is a bug).
A way to set the NLS_COMP to something else is to use something like "alter session set nls_comp = BINARY;". Note that changing any preference after that appears to overwrite this and you need to do this for each new connection you start. -
I have ssas DB with two cubes, they share the date dimension. My query shows users on the dimension and visit as a measure. In the where clause I have range of dates. I have this strange behavior that after a FULL cube process I don't see
any data when applying the range. I'm sure that I have data for the range because when I filter each day desperately I can see data. Another weird scenario is that if the date is on the dimension I can see data. The thing that solve it is when process DATA
the cubes. This my query:
select non empty [Users].[User].[User] columns,
{[Measures].[Visits]} on rows
from [System Visit] where
([Dim_Date].[Georgian Calendar].[Date ID].&[20150125]:[Dim_Date].[Georgian Calendar].[Date ID].&[20150202])
Any thoughts what can be the issue? ThanksHi liranbn,
According to your description, you want to return the Visits within a date range. Right?
In this scenario, please modify your MDX query like below:
select nonempty([Users].[User].[User].members,[Measures].[Visits]) on columns,
[Measures].[Visits] on rows
from [System Visit]
where
({[Dim_Date].[Georgian Calendar].[Date ID].&[20150125]:[Dim_Date].[Georgian Calendar].[Date ID].&[20150202]})
Please see MDX with AdventureWorks sample:
If you have any question, please feel free to ask.
Simon Hou
TechNet Community Support -
Query Builder won't apply chosen date to the where clause
Does anybody know why when I chose a date field, in the where clause, when using Query Builder, it won't actually write the chosen date to the actual query?
If I select the "View Query" tab, no date shows up. Also if I press the "Run Report" button in the "View Result" tab, I get this error:
"An error was encountered performing requested operation: ORA-00936: missing expression"
This is because actual date data is missing.
After I hit the "Apply" button, I have to manually type the date data.
I thought this was a bug of the previous version, but I just installed version 1.5.4 and I have no different result.
Thanks.I just wanted to add my name to the list of people having this issue.
Oracle Techies, please help. -
PreparedStatement.setDate(int parameterIndex, Date x)
Anyone knows how to set an instance of Date(long date)?
What do I put in the parameter?
Date d = new Date(????);
Anyone can advice?1. If you want to create the object with the current date, simply use the parameterless constructor, assuming your creating an object of java.util.Date
2. If your creating an object of java.sql.Date and you want the current date in it, then you can do
java.sql.Date today = new java.sql.Date(System.currentTimeMillis());3. If your creating either of the two types of date with a specefic date set in it you could use
java.util.Calendar cal = java.util.Calendar.getInstance();
cal.set(year, month, date);
Date = new Date(cal.getTime().getTime());hope I didnt leave out a possibility.... ;-)
Regards
Omer -
Cast a timestamp to date in a "where" clause
Hi everybody :)
I've got a little problem that seems very simple, but I can't solved it myself :(
I've got a column with a "timestamp" format, and I'd like to select all the records that are from a precise date, let's say "12/14/2009".
I have the following SQL statement :
SELECT * FROM myTable WHERE (CAST (myTimestampField AS DATE))= TO_DATE('12/14/2009', 'mm/dd/yyyy')
But that returns me nothing... However, I've got records with timestamps that where created the 14th of december.
Can you help me with that, I'm really out of ideas... And I'm not a SQL expert :p
Thanks a lot!what is the bbcode for the code ? simple code ?write before and after your code snippet.
Max
[My Italian Oracle blog|http://oracleitalia.wordpress.com/2010/01/02/query-gerarchiche/] -
How to use DATE in the where clause
I need to select list of records from a table where available date is greater than or equal to current date. Below is the table structure and
select query used to get the list of records
CREATE TABLE TEMP (ITEM_ID NUMBER(20),ITEM_NAME VARCHAR2(100),CREATION_DATE DATE,AVAILABLE_DATE DATE);
INSERT INTO TEMP (ITEM_ID,ITEM_NAME,CREATION_DATE,AVAILABLE_DATE) VALUES(1,'ITEM1',SYSDATE,SYSDATE);
INSERT INTO TEMP (ITEM_ID,ITEM_NAME,CREATION_DATE,AVAILABLE_DATE) VALUES(2,'ITEM2',SYSDATE,SYSDATE+10);
INSERT INTO TEMP (ITEM_ID,ITEM_NAME,CREATION_DATE,AVAILABLE_DATE) VALUES(3,'ITEM3',SYSDATE,SYSDATE-10);
INSERT INTO TEMP (ITEM_ID,ITEM_NAME,CREATION_DATE,AVAILABLE_DATE) VALUES(4,'ITEM4',SYSDATE,SYSDATE);
INSERT INTO TEMP (ITEM_ID,ITEM_NAME,CREATION_DATE,AVAILABLE_DATE) VALUES(5,'ITEM5',SYSDATE,SYSDATE+5);
SELECT ITEM_NAME, available_date FROM TEMP WHERE available_date > SYSDATE OR available_date LIKE SYSDATE;I am getting the expected records but i am not able to find a condition where i can use *>=* for date data type, a query like the below one is
not returning expected records
SELECT ITEM_NAME, available_date FROM TEMP WHERE available_date >= SYSDATE ;Edited by: Balaji on Mar 19, 2012 9:13 PMHi,
Balaji wrote:
I need to select list of records from a table where available date is greater than or equal to current date. Below is the table structure and
select query used to get the list of records
CREATE TABLE TEMP (ITEM_ID NUMBER(20),ITEM_NAME VARCHAR2(100),CREATION_DATE DATE,AVAILABLE_DATE DATE);
INSERT INTO TEMP (ITEM_ID,ITEM_NAME,CREATION_DATE,AVAILABLE_DATE) VALUES(1,'ITEM1',SYSDATE,SYSDATE);
INSERT INTO TEMP (ITEM_ID,ITEM_NAME,CREATION_DATE,AVAILABLE_DATE) VALUES(2,'ITEM2',SYSDATE,SYSDATE+10);
INSERT INTO TEMP (ITEM_ID,ITEM_NAME,CREATION_DATE,AVAILABLE_DATE) VALUES(3,'ITEM3',SYSDATE,SYSDATE-10);
INSERT INTO TEMP (ITEM_ID,ITEM_NAME,CREATION_DATE,AVAILABLE_DATE) VALUES(4,'ITEM4',SYSDATE,SYSDATE);
INSERT INTO TEMP (ITEM_ID,ITEM_NAME,CREATION_DATE,AVAILABLE_DATE) VALUES(5,'ITEM5',SYSDATE,SYSDATE+5);
SELECT ITEM_NAME, available_date FROM TEMP WHERE available_date > SYSDATE OR available_date LIKE SYSDATE;
Both operands to LIKE are supposed to be strings. Don't try to use a DATE, such as SYSDATE, with LIKE.
I am getting the expected records but i am not able to find a condition where i can use *>=* for date data type, a query like the below one is
not returning expected records
SELECT ITEM_NAME, available_date FROM TEMP WHERE available_date >= SYSDATE ;
It's returning the results I expect.
If you'd say what results you were expecting, someone could help you wite a query to get them.
Assuming 1 second elapses between the time 'ITEM4' is inserted and the time the 2nd query is run, then, at run-time, available_date will be 1 second less than SYSDATE, so it should not be included.
If you want to find rows that are on the same calendar day as SYSDATE, or later, then use
WHERE available_date >= TRUNC (SYSDATE) -
Problem in where clause in Union
Hi,
This is my query
select activity_source,
cust_acct_no,
trx_no,
order_no,
dollar_amount,
deduction_number,
dispute_status,
dispute_dt,
case when count(*) over (partition by cust_acct_no,trx_no order by cust_acct_no,trx_no)=1 then activity_source else trx_no end as order2,
case when count(*) over (partition by cust_acct_no,trx_no,order_no order by cust_acct_no,trx_no,order_no)=1 then activity_source else order_no end as order3
From
(select
'QFAL' activity_source
, ded.customer_account cust_acct_no
, ded.invoice_nbr trx_no
, ded.sales_order_prime order_no
, ded.deduction_amount dollar_amount
, ded.deduction_nbr deduction_number
, NULL dispute_status
, NULL dispute_dt
from
DW_AP_FAL_DEDUCTION ded
Where/*_*/
ded.follow_date = '9-SEP-9999'
UNION
select
'PS' activity_source
, item.CUST_ID cust_acct_no
, item.ITEM trx_no
, item.ORDER_NO order_no
, item.BAL_AMT dollar_amount
, NULL deduction_number
, item.DISPUTE_STATUS dispute_status
, item.DISPUTE_DT dispute_date
from
otr_item_ps item
Where/*_*/
item.DISPUTE_STATUS IN ('MUL','RDC','PA','CCR')
and item.BUSINESS_UNIT IN ('ARNAM','ARCAN','ARUSC')
and item.ITEM_STATUS = 'O')
order by
order2 desc,
order3 desc,
cust_acct_no
I wanted to implement a criteria where ded.deduction_amount dollar_amount in the first part of the query should not be equal to item.BAL_AMT
i.e; ded.deduction_amount<>item.BAL_AMT
how do i do this in the query?Select name, lodging, age
From longtime
Union
Select name address, 0 -- when you do not have the same column in the table.
From prospect
/* Order by age; -- you cannot use column name in the order by clause. */
order by 3 ; you should use the column number instead. -
Date comparison in WHERE clause
I have this simple SQL sequence:
CREATE TABLE t1 (d1 DATE);
INSERT INTO t1 VALUES(CURRENT_DATE);
SELECT * FROM t1 WHERE d1 = CURRENT_DATE;
It is obvious what it does, yet on Oracle 10g R1 the SELECT query does not return anything. Any ideas on why this happens?
Thanks,
RobertWith me, everything works:
SQL> CREATE TABLE t11 (d1 DATE);
Tabel is aangemaakt.
SQL> INSERT INTO t11 VALUES(CURRENT_DATE);
1 rij is aangemaakt.
SQL> SELECT * FROM t11 WHERE d1 = CURRENT_DATE;
D1
24-10-2006 11:17:11
1 rij is geselecteerd.But this is because it is executed in a script.
It doesn't work with you because you are obviously unable to type the select statement within a second :-)
You probably forgot that the current_date has a time component with seconds too.
Regards,
Rob.
Maybe you are looking for
-
Query Takes Longer time as the Data Increases.
Hi , We have one of the below Query which takes around 4 to 5 minutes to retrieve the data and this appears to be very slow as the data grows. DB Version=10.2.0.4 OS=Solaris 10 tst_trd_owner@MIFEX3> explain plan for select * from TIBEX_OrderBook as o
-
NOW, I can't open any pdf files HELP!
-
place a marker
-
I'm receiving the follow message when loading firefox 5.0 The NTVDM CPU has encountered an illegal instruction
-
Replaced harddrive after it crashed-I need a new serial number. have redemption code
I replaced my harddrive on my laptop using windows 7. I lost abobe photo lightroom 5. I have my disk, redemption codes, id, and pass word. I need a new serial number to reinstall.