Getting max value from a Date column in Oracle using max() - oracle

I am trying to query the latest date from table with a column DATE.
My expression is as follow:
SELECT MAX(DATE) FROM table_name;
I received back error:
ORA-00936: missing expression
00936. 00000 - "missing expression"
*Cause:
*Action:
Any idea what is the issue? please help.

DATE is a reserved word and cannot be used as an unquoted identifier as the SQL engine expects it to be either a data-type or as part of a date literal such as DATE '1970-01-01'. In your case, the SQL engine expects DATE to be followed by the literal of the date-literal (e.g. '1970-01-01') but that does not happen which is why it complains about a missing expression.
If you have a column named DATE then you should:
Change it to a different name; or
If that is not possible, always use a quoted identifier whenever you refer to the column (which requires double quotes around the identifier and the correct case to be used).
SELECT MAX("DATE") FROM table_name;

Related

Missing expression in convert function

I am trying to use convert function to convert date field to varchar but it keep giving missing expression error.
My Query:
select INVENTORY_ITEM_ID,
convert(varchar(20), IMPLEMENTATION_DATE,110)
from apps.mtl_item_revisions
where INVENTORY_ITEM_ID=21928;
gives error:
ORA-00936: missing expression
00936. 00000 - "missing expression"
*Cause:
*Action: Error at Line: 97 Column: 35
The following line is Microsoft SQL Server syntax, not Oracle syntax:
convert(varchar(20), IMPLEMENTATION_DATE,110)
It doesn't matter what kind of database driver you use, or who wrote it. The query is being sent to Oracle with SQLServer syntax and Oracle is giving the error. (Also, while it might be required in a query tool, be wary of terminating oracle SQL statements with semicolon; in some languages (e.g. C#, or dynamic SQL within PL/SQL) you may get an unexpected character error.)
Change your SQL to this:
select INVENTORY_ITEM_ID,
to_char(IMPLEMENTATION_DATE, 'mm-dd-yyyy') as IMPLEMENTATION_DATE
from apps.mtl_item_revisions
where INVENTORY_ITEM_ID=21928
That's the ORacle equivalent of SQLServer 110 date format

Restrict thousand separator in TO_NUMBER (Oracle)

I'm trying to parse numbers using the following code
TO_NUMBER('1,234.56', '9999999D99')
For some reason comma is ignored and the value is parsed correctly, despite the format doesn't have it. Is there any way to restrict usage of thousand group separator?
So far I only came up with setting a bogus symbol as a separator with the hope that user will not use it
TO_NUMBER('1,234.56', '9999999D99', 'NLS_NUMERIC_CHARACTERS=''.ã''');
This is a very strange request - if Oracle is correctly converting the string to a number then it seems to be doing it's job correctly. However, if you really need to do this for whatever reason then simply remove the format mask.
SQL> select to_number('1,234.56') from dual;
select to_number('1,234.56') from dual
*
ERROR at line 1:
ORA-01722: invalid number
SQL> select to_number('1234.56') from dual;
TO_NUMBER('1234.56')
--------------------
1234.56
SQL>
Though the SQL Language Reference doesn't mention any default values I believe that the default value for the format mask described in the OLAP DML Reference for TO_NUMBER() applies:
The default number format identifies a period (.) as the decimal marker and does not recognize any other symbol.
This, in turn, means that a comma is an invalid value for the conversion and thus the conversion will fail.

How to pass a date parameter from report builder query designer to oracle database

i'm using report builder 3.0 connected to oracle database.
i'm trying to pass a date parameter in the query with no success, i don't know the exact syntax.
I've tried :
SELECT *
FROM igeneral.GCL_CLAIMS
WHERE CREATED_BY IN (:CREATED_BY) AND CLAIM_YEAR IN(:UW_YEAR)
AND (Trunc(LOSS_DATE,'mm/dd/yyyy') BETWEEN to_char(':From', 'mm/dd/yyyy')
AND to_char('To', 'mm/dd/yyyy'))
i got this error: ORA-01036: illegal variable name/number
also i've tried this:
SELECT *
FROM igeneral.GCL_CLAIMS
WHERE CREATED_BY IN (:CREATED_BY) AND CLAIM_YEAR IN(:UW_YEAR)
AND (LOSS_DATE BETWEEN ':From' AND ':To')
i got this error: ORA-01036: illegal variable name/number
thanks
The solution depends on the data type of the from and to parameter.
If your parameter is DATE datatype, use to_char to conver date into a literal of the required format you want to display. Else, if the parameter is VARCHAR2 datatype, use to_date to convert the date literal to date for comparision.
In your case, it is probable that LOSS_DATE column is a DATE, and your parameters are literals, so use `TO_DATE on the parameters using proper format mask.
LOSS_DATE BETWEEN to_date(':From', 'mm/dd/yyyy') AND to_date('To', 'mm/dd/yyyy')
The error, ORA-01036: illegal variable name/numberbeing, typically occurs in an Oracle Report when the parameter identified in the object navigator does not correspond with a bind variable referenced in a query.
Does your :From and :To bind variables appear in "User Parameters" in your Object Navigator? I did notice that your code cited does not have the colon preceding it for the :To bind variable.
Correct Example
Here is a screenshot of a seeded Oracle Report's "User Parameters"....and specifically the :p_as_of_date bind variable. Notice with the pictured Property Inspector, one identifies a datatype and an Input Mask. Did you set parameters, :From and :To, in this manner? Specifically, if your parameter is being used as a date, I would make sure that Datatype is set to Date in the Property Inspector.
Here is an snippet of the :p_as_of_date bind variable in a "seeded" Oracle Report and here it is being referenced in a query:
where TRUNC(ps.gl_date) <= :p_as_of_date
Notice that no casting is needed.
With your query, I would take this approach (again, no casting):
AND TRUNC(LOSS_DATE,'mm/dd/yyyy') BETWEEN :From AND :To
Consequently, I would just make sure the bind variable cited in "User Parameters" correspond with the bind variables in your query.

Comparing Date column to sysdate yields: a non-numeric character was found where a numeric was expected

I've been having a strange issue where the comparison of a date column to SYSDATE yields the following error:
01858. 00000 - "a non-numeric character was found where a numeric was expected"
*Cause: The input data to be converted using a date format model was
incorrect. The input data did not contain a number where a number was
required by the format model.
*Action: Fix the input data or the date format model to make sure the
elements match in number and type. Then retry the operation.
I'm re-creating a MATERIALIZED VIEW; which included some minor changes, and whenever the process aborts it always points to the '>=' in the following derived table query:
SELECT id,
desc,
start_date,
end_date
FROM T_LIPR_POLICY_ROLE TLPR
WHERE end_date >= SYSDATE
Now end_date is a type DATE, and I can actually execute this query by itself, but whenever I try to run it in the materialized view it always aborts with the error above. Although last week I was able to create it with the same query.
Any ideas?
Thank you,
Hi I'm terribly sorry for the long delay. I just couldn't post the whole statement for security reasons.
Now the issue has been resolved. The problem was that our materialized view script was aggregating data from 17 different places vía a UNIONs. Now for some reason the error was pointing to wrong line of code (see below).
SELECT id,
desc,
start_date,
end_date
FROM T_LIPR_POLICY_ROLE TLPR
WHERE end_date >= SYSDATE <-- ORACLE POINTS TO THIS LINE
Now this was like the tenth statement in the script, but the error really was in the sixth statement in the script; which was obviously misleading. In this statement a particular record (out of millions) was attempting the following operation:
to_date(' / 0/ ') <-- This was the cause of the problem.
Note that this text wasn't like this in the actual script it literally said to_date(<column name of type varchar>), but 2 records out of 15 million had the text specified above.
Now what I don't quite get is why Oracle points to the wrong line of code.
¿Is it an Oracle issue?
¿Is it a problem with the SQL Developer?
¿Could it be a conflict with a hint? We use several like this: /*+ PARALLEL (init 4) */
Thank you for all your help.
Is desc a column name? If yes then you are using a oracle reserved keyword desc as a column name.
SELECT id,
desc,---- here
start_date,
end_date
FROM T_LIPR_POLICY_ROLE TLPR
WHERE end_date >= SYSDATE
We cannot use oracle reserved keywords in column names.
Please change the column name.

How to fix 'ORA-29909: label for the ancillary operator is not a literal number' with bound variable?

The following SQL running of an Oracle Text CONTEXT indexed column:
variable label number;
exec :label := 1;
SELECT * FROM TEST_TABLE
WHERE CONTAINS(INDEXEDCOL, 'token', :label) > 0;
Results in:
Error report:
SQL Error: ORA-29909: label for the ancillary operator is not a literal number
29909. 00000 - "label for the ancillary operator is not a literal number"
*Cause: The label used for invoking the ancillary operator is not
a literal number.
*Action: Use a literal number as the label.
I found one mention of a similar problem here:
https://forums.oracle.com/forums/thread.jspa?threadID=91417
but altering the cursor with the following command does not help in my case:
ALTER SESSION SET cursor_sharing='EXACT';
There's also this stackoverflow question:
ORA-29908: missing primary invocation for ancillary operator
but the answer is not applicable.
I'm using Oracle 11.2.0.3.0

Resources