I want to choose the min value in two date ,such as
c := min(a,b);
It occupy compiler error :
Error(20,10): PLS-00103: Encountered
the symbol "," when expecting one of
the following:
. ( ) * # % & - + /
at mod remainder rem || multiset
I know we can use aggregate function Min in the SQL. I dan't whether there is the similar func i can use i pl/sql?
In PLSQL, the least function returns the smallest value in a list of expressions.
LEAST("ColumnName", _NumberOfRows)
Example: For the Minimum 5 Rows = LEAST(Price,5)
Related
Create a subprogram of type operator that receives two integers and sends them back
the negative sum of them. I.e. if the sum is positive it will be
the result is negative or if the sum is a negative result
positive. Ex. 6 and 4 give -10 as a result and 2 and -6 give 4.
For instance:
Type in two integers: **7 -10**
The negative sum of the two integers is 3.
Type in two integers: **-10 7**
The positive sum of the two integers is -3.
No entries or prints may be made in the subprogram.
So I attempted this task and actually solved it pretty easily using a function but when it came to converting it to an operator I stumbled into a problem.
This is my approach:
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
procedure Test is
function "+" (Left, Right : in Integer) return Integer is
Sum : Integer;
begin
Sum := -(Left + Right);
return Sum;
end "+";
Left, Right : Integer;
begin
Put("Type in two integers: ");
Get(Left);
Get(Right);
Put("The ");
if -(Left + Right) >= 0 then
Put("negative ");
else
Put("positive ");
end if;
Put("sum of the two integers is: ");
Put(-(Left + Right));
end Test;
My program compiles but when I run it and type two integers it says:
raised STORAGE_ERROR: infinite recursion
How do I solve this problem using an operator? I managed to tackle it easily with the procedure- and function subprogram but not the operator. Any help is appreciated!
You can use the type system to solve this without using a new operator symbol.
As a hint, operators can overload on argument and return types. And a close reading of the question shows the input type is specified, but not the output type. So, how about this?
type Not_Integer is new Integer;
function "+" (Left, Right : in Integer) return Not_Integer is
Sum : Integer;
begin
Sum := -(Left + Right);
return Not_Integer(Sum);
end "+";
As the two "+" operators have different return types, there is no ambiguity between them and no infinite recursion.
You will have to modify the main program to assign the result to a Not_Integer variable in order to use the new operator.
Hi I am working on one query, what I want to do is, whenever output comes in negative value,
I want to make it null, following is my query, can anyone help me with this ?
select (to_char((coalesce(14515200/3600000,0) - (coalesce(30512.65/3600,0) - (coalesce(1800/3600,0) + (coalesce(1800/3600,0))))),'FM99,999,999,999'))::character varying as test
If you want a result of NULL for negative values, use
nullif(greatest(/* your expression */, 0), 0)
The only drawback here is that a result of exactly 0 will also become NULL. If you want to avoid that, you could use a user defined function:
CREATE FUNCTION neg_to_null(double precision) RETURNS double precision
LANGUAGE plpgsql AS
'BEGIN; RETURN CASE WHEN $1 < 0.0 THEN NULL ELSE $1; END;';
I use PL/pgSQL to avoid function inlining.
Move your logic to the from clause and use case:
select (case when test_n >= 0 then test_n::character varying end)
from (select . . . as test_n -- your expression as number) x
If your expression actually uses values from a table, you can use a subquery, CTE, or lateral join to define test_n.
I have column TEXT_INT in my table and I would like convert to DECIMAL using TO_NUMBER Function. Unfortunately I am getting
INVALID NUMBER ORA-722 error.
I doubt it may have characters in it. So Issued below query but there is no alphabets in TEXT_INT.
SELECT *
FROM NANTHA_TABLE
WHERE UPPER(TEXT_INT) != LOWER(TEXT_INT);
Would you please provide an idea to resolve this issue or ways to finding wrong data?
try this:
create function like this:
CREATE OR REPLACE function f_invalid_number(number_field in varchar2) return number as
n_d number;
begin
n_d := TO_number(number_field);
return 0;
exception
when others then
return 1;
end;
/
then you can check invalid data like this:
SELECT *
FROM NANTHA_TABLE
WHERE f_invalid_number(TEXT_INT) =1
This query may help you.
select your_wrong_col ,
case
when trim(TRANSLATE(your_wrong_col ,'0123456789','')) is null
then 'numeric'
else 'not_numeric'
end as your_wrong_col
from YOUR_TABLE
where trim(TRANSLATE(your_wrong_col ,'0123456789','')) is not null;
Finds non-numeric rows.
Please use below query to find records where TEXT_INT column has characters other than number.
select * from NANTHA_TABLE where NOT REGEXP_LIKE(TEXT_INT,'^[0-9]+$')
In case you are expecting more string symbols in your column which should be considered as valid you can include them in the NOT REGEXP_LIKE condition so those columns also don't appear in the query.
eg. If expecting a '-' symbol in the start of the string for some values:
NOT REGEXP_LIKE(COLUMN_NAME, '^-?[0-9.]+$') "
where
'^' Begining
'?' Zero or One occurance
'+' One of More occurance
'$' End of String
Please note i know that the decimals in my above query will be accepted even if counted twice. For that I propose a different solution to count the number of decimals in the string and throw error. If any1 can integrate it with the REGEXP you are most welcome. How to count number of decimals for check.
LENGTH(COLUMN_NAME) - LENGTH(REPLACE(COLUMN_NAME,'.','')) > 1
To find more details about the REGEXP_LIKE please use the following link.
This question already has an answer here:
Having a Column name as Input Parameter of a PreparedStatement
(1 answer)
Closed 5 years ago.
I am trying to copy one table's content into another, in the following way:
sql ="INSERT INTO customer (nev, quantity, frequency) SELECT "
+ "?,COUNT(?),AVG(?)"
+ "FROM tempcustomer GROUP BY ? ORDER BY COUNT(?) DESC";
preparedStmt=conn.prepareStatement(sql);
preparedStmt.setString(1, "nev");
preparedStmt.setString(2, "nev");
preparedStmt.setString(3, "pont");
preparedStmt.setString(4, "nev");
preparedStmt.setString(5, "nev");
preparedStmt.executeUpdate();
I get the following error message:
com.mysql.jdbc.MysqlDataTruncation: Data truncation: Truncated incorrect DOUBLE value: 'pont'
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3971)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3909)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2527)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2680)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2501)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1858)
at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2079)
at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2013)
at com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5104)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1998)
at Sample.main(Sample.java:101)
I am new to JDBC, thanks in advance!
It depends on the arguments. As explained in the documentation:
The SUM() and AVG() functions return a DECIMAL value for exact-value
arguments (integer or DECIMAL), and a DOUBLE value for
approximate-value arguments (FLOAT or DOUBLE).
You can coerce a string argument to a number by treating it as a number:
AVG(? + 0)
However, a value such as 'pont' will be converted to 0 which will be included in the average calculation.
PostgreSQL has a useful function called GREATEST. It returns the largest value of those passed to it as documented here.
Is there any equivalent in SQLite?
As a note, I only need it to work with 2 arguments.
SELECT MAX(1,2,..)
ref: https://sqlite.org/lang_corefunc.html#maxoreunc
max(X,Y,...)
The multi-argument max() function returns the argument with the maximum value, or return NULL if any argument is NULL. The multi-argument max() function searches its arguments from left to right for an argument that defines a collating function and uses that collating function for all string comparisons. If none of the arguments to max() define a collating function, then the BINARY collating function is used. Note that max() is a simple function when it has 2 or more arguments but operates as an aggregate function if given only a single argument.
using a second value in MAX(value1, value2) would be the equivalent
Example:
UPDATE products SET Quantity = MAX(Quantity - #value, 0)...
if (Quantity - value) return a "Negative Number -0" then Max( , 0) will return 0 because 0 is bigger than -0 / -1 / -2 ... and so on
Max( , 1) will return 1 if the same condition (Quantity - value) return 0 or a Negative Number .. etc, you get the idea !
if we assume that both Quantity and #value could be NULL then use the combination: IFNULL(MAX(Quantity-#value,0),0)
IFNULL(..., 0) will return the second value of your choice IF the first one is NULL