Built in SQL Function for Sum of Geometric Progression
Hi all,
Please let me know if there is any availble built in SQL function for
SUm of Geometric Progression.
Thanks in Advance,
Kaushik B.
and also relatively simple to write:
s = a*(1-power(r,n+1))/(1-r)
assuming you have a row with values of a,r and n (and that's the sum you need)
Jon
Similar Messages
-
What are the built-in SQL functions in Oracle? I already know: COUNT, SUM, AVG, MAX, and MIN. Are there any more?
Thanxhttp://download-west.oracle.com/docs/cd/B10501_01/server.920/a96540/functions2a.htm
-
Equivalent Sql function for Forms NAME_IN() built in
Please let me know, is there any equivalent Sql function equivalent to Forms Name_in() built in. Thanks.
What would such a function do in pure SQL? Or are you asking if you can refer to Forms variables in SQL that you issue from Forms?
The answer to the first question I will delay until you define what it is that you want to do. The answer to the second question is that there is no way to do that. The database has no way of knowing how to resolve references to variables defined in client side PLSQL. -
PL/SQL-Function for validation
I wrote a PL/SQL function in order to validate that the SUM of input-values doesn't exceed 100.
I used validation rule from APEX for PL/SQL-expression (same like SQL-expression):
f_page_16_validation(:P16_PSP_PSP)<=100 (Condition: CREATE, SAVE)
source code for function:
CREATE OR REPLACE FUNCTION f_page_16_validation
(pi_p16_psp_psp VARCHAR2)
RETURN NUMBER
IS
vl_sum NUMBER(5) := 0;
BEGIN
SELECT NVL(SUM(psp_anteil_projekt),0) INTO vl_sum
FROM st_psp_projekt_psp
WHERE UPPER(psp_psp) = pi_p16_psp_psp;
RETURN vl_sum;
END f_page_16_validation;
When I check in SQL*Plus against database: SELECT f_page_16_validation('A_TEST_UHL_PSP%') FROM dual;
I get the correct value. When I input into APEX-Application the validation rule doesn't fire (CREATE). If my SUM is over 100 and try to change
one value (in order to come under 100) (SAVE) - the validation rule fires but quite independent of the SUM - it accepts NO VALUE!!
Apex-Version: 3.1.1.00.09
Can somebody tell me what's wrong? Is there any other way - SQL,PL/SQL-expression, PL/SQL-function body (bool, error text) didn't work.1. If your PL/SQL expression is written as you stated:
f_page_16_validation(:P16_PSP_PSP)<=100then you will receive an error message if the total is <= 100 and not if it exceeds 100.
2. Where in your code do you consider the input values? I see that you are selecting from a table, where your already inserted values are but I see no input values. Are you using a form or a tabular form? If I look at the names of your buttons then I wouls say you are using a form.
3. You are talking about condition. What type of condition you use? I would use a PL/SQL Expression like this:
:REQUEST IN ('CREATE', 'SAVE')4. I would suggest to use a validation of type PL/SQL Function Returning Error Text since you can put all the code in one.
Denes Kubicek
http://deneskubicek.blogspot.com/
http://www.opal-consulting.de/training
http://apex.oracle.com/pls/otn/f?p=31517:1
------------------------------------------------------------------- -
SQL Function for calculating IP Address
Hi Experts ,
I have an IP Range i need to find out the First Host detail for the IP using a SQL scalar function .
Example : 11.30.10.40
Subnet Mask is 26
Output required
First host: 11.30.10.1
am confused how i can derive the first host values from a sql function kindly help .
Thanks
PriyaDECLARE @Mask TINYINT = 26;
DECLARE @AddressIP VARCHAR(15)= '11.30.10.40';
DECLARE @AddressValue BIGINT = PARSENAME(@AddressIP,4)*CAST(256*256*256 AS BIGINT)+PARSENAME(@AddressIP,3)*256*256+PARSENAME(@AddressIP,2)*256+PARSENAME(@AddressIP,1);
WITH
n0(n) AS (SELECT 0 UNION ALL SELECT 0)
,n1(n) AS (SELECT 0 FROM n0 a CROSS JOIN n0)
,n2(n) AS (SELECT 0 FROM n1 a CROSS JOIN n1)
,n3(n) AS (SELECT 0 FROM n2 a UNION ALL SELECT 0 g FROM n2)
,t (n) AS (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM n3)
,bits(b,v) AS (SELECT n,POWER(CAST(2 AS BIGINT),32-n) FROM t)
SELECT @AddressIP [Address]
,CAST((SUM(v) / 256 / 256 / 256) % 256 AS VARCHAR(3)) + '.' + CAST((SUM(v) / 256 / 256) % 256 AS VARCHAR(3)) + '.' + CAST((SUM(v) / 256) % 256 AS VARCHAR(3)) + '.' + CAST(SUM(v) % 256 AS VARCHAR(3)) [Netmask]
,CAST(((SUM(v) ^ 0xFFFFFFFF) / 256 / 256 / 256) % 256 AS VARCHAR(3)) + '.' + CAST(((SUM(v) ^ 0xFFFFFFFF) / 256 / 256) % 256 AS VARCHAR(3)) + '.' + CAST(((SUM(v) ^ 0xFFFFFFFF) / 256) % 256 AS VARCHAR(3)) + '.' + CAST(((SUM(v) ^ 0xFFFFFFFF)) % 256 AS VARCHAR(3)) [Wildcard]
,CAST(((@AddressValue & SUM(v)) / 256 / 256 / 256) % 256 AS VARCHAR(3)) + '.' + CAST(((@AddressValue & SUM(v)) / 256 / 256) % 256 AS VARCHAR(3)) + '.' + CAST(((@AddressValue & SUM(v)) / 256) % 256 AS VARCHAR(3)) + '.' + CAST(((@AddressValue & SUM(v))) % 256 AS VARCHAR(3)) + '/' + CAST(MAX(b) AS VARCHAR(2)) [NetworkAddress]
,CAST(((((SUM(v) ^ 0xFFFFFFFF)) | @AddressValue) / 256 / 256 / 256) % 256 AS VARCHAR(3)) + '.' + CAST(((((SUM(v) ^ 0xFFFFFFFF)) | @AddressValue) / 256 / 256) % 256 AS VARCHAR(3)) + '.' + CAST(((((SUM(v) ^ 0xFFFFFFFF)) | @AddressValue) / 256) % 256 AS VARCHAR(3)) + '.' + CAST(((((SUM(v) ^ 0xFFFFFFFF)) | @AddressValue)) % 256 AS VARCHAR(3)) [BroadcastAddress]
,CAST(((@AddressValue & SUM(v)) / 256 / 256 / 256) % 256 AS VARCHAR(3)) + '.' + CAST(((@AddressValue & SUM(v)) / 256 / 256) % 256 AS VARCHAR(3)) + '.' + CAST(((@AddressValue & SUM(v)) / 256) % 256 AS VARCHAR(3)) + '.' + CAST(((@AddressValue & SUM(v))) % 256 + 1 AS VARCHAR(3)) [FirstHost]
,CAST(((((SUM(v) ^ 0xFFFFFFFF)) | @AddressValue) / 256 / 256 / 256) % 256 AS VARCHAR(3)) + '.' + CAST(((((SUM(v) ^ 0xFFFFFFFF)) | @AddressValue) / 256 / 256) % 256 AS VARCHAR(3)) + '.' + CAST(((((SUM(v) ^ 0xFFFFFFFF)) | @AddressValue) / 256) % 256 AS VARCHAR(3)) + '.' + CAST(((((SUM(v) ^ 0xFFFFFFFF)) | @AddressValue)) % 256 - 1 AS VARCHAR(3)) [LastHost]
,(((SUM(v) ^ 0xFFFFFFFF)) | @AddressValue) - (@AddressValue & SUM(v)) - 1 [TotalHostCount]
FROM bits WHERE @Mask BETWEEN 1 AND 32 AND b <= @Mask
Jon -
How to distinguish built-in SQL functions of PL/SQL?
I m having a hard time to figure out which functions are used ONLY in SQL statements and which are used in regular expr(ie, variable assignment,). Can anyone show me a list of each or perhaps a URL to look for?
I have searched through either the developer's guide and reference but couldn't find any appropriate indication in one place that make it clear.
For instance, I thought I can use CAST function in a variable assginment like the following:
declare
cursor myCur is SELECT Value_varchar2(1) FROM table WHERE id = 1;
myRec myCur%ROWTYPE;
var_a NUMBER(1);
begin
OPEN myCur;
FETCH myCur INTO myRec;
CLOSE myCur;
var_a := CAST(myCur.Value_varchar2(1) AS NUMBER(1));
DBMS_OUTPUT.PUT_LINE('var_a = ' || TO_CHAR(var_a));
end;
. It seems like CAST function can ONLY be used in SQL statement, but no doc so far states that?!
Edited by: HappyJay on 2010/05/12 12:05Sorry to bother you, Bob!
I think I might already found the list. Is it the following list?
---------------------- QUOTED FROM Oracle® Database PL/SQL Language Reference 11g Release 2 (11.2)Part Number E10472-06
SQL Functions in PL/SQL Expressions
In PL/SQL expressions, you can use all SQL functions except:
Aggregate functions (such as AVG and COUNT)
Analytic functions (such as LAG and RATIO_TO_REPORT)
Data mining functions (such as CLUSTER_ID and FEATURE_VALUE)
Encoding and decoding functions (such as DECODE and DUMP)
Model functions (such as ITERATION_NUMBER and PREVIOUS)
Object reference functions (such as REF and VALUE)
XML functions (such as APPENDCHILDXML and EXISTSNODE)
These conversion functions:
BIN_TO_NUM
These miscellaneous functions:
CUBE_TABLE
DATAOBJ_TO_PARTITION
LNNVL
NVL2
SYS_CONNECT_BY_PATH
SYS_TYPEID
WIDTH_BUCKET
PL/SQL supports an overload of BITAND for which the arguments and result are BINARY_INTEGER.
When used in a PL/SQL expression, the RAWTOHEX function accepts an argument of data type RAW and returns a VARCHAR2 value with the hexadecimal representation of bytes that comprise the value of the argument. Arguments of types other than RAW can be specified only if they can be implicitly converted to RAW. This conversion is possible for CHAR, VARCHAR2, and LONG values that are valid arguments of the HEXTORAW function, and for LONG RAW and BLOB values of up to 16380 bytes.
---------------------- -
XMLType schema detail lost in pl/sql function for a jdeveloper webservice
If I write a very simple pl/sql procedure to wrap as a web service to return the XML from an XMLtype table column, the jdeveloper (11.1.1.3) web service generation process seems to ignore the detail
of the registered XMLTypes' schema, and converts the 'output' to an xsd:string in the generated WSDL file, which has knock on effect that generated data controls are 'dumb' in only seeing output as a string, and providing no real data functionality at XML schema level. Since I know web service data controls work fine when provided with the correct level of detail in the WSDL, what am I missing in terms of pursuading jdeveloper to generate wsdl's with full xsd schema info for the return XMLType?
This is my table:
ID NOT NULL NUMBER(12)
SPEC SYS.XMLTYPE(XMLSchema "http:
//localhost:8080/public/demo
/xsd/sample2.xsd" Element "b
ooks") STORAGE BINARY
PRD_ID NOT NULL NUMBER(10)
ISSUE NOT NULL NUMBER(6)
EDIT_NO NOT NULL NUMBER(6)
PUBLISHED_BY_PEO_ID NUMBER(10)
PUBLISHED_ON_DT DATE
EFFECTIVE_FROM_DT NOT NULL DATE
EFFECTIVE_TO_DT DATE
=======================================================
This is the XML Schema:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="urn:books"
xmlns:bks="urn:books">
<xsd:element name="books" type="bks:BooksForm"/>
<xsd:complexType name="BooksForm">
<xsd:sequence>
<xsd:element name="book"
type="bks:BookForm"
minOccurs="0"
maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="BookForm">
<xsd:sequence>
<xsd:element name="author" type="xsd:string"/>
<xsd:element name="title" type="xsd:string"/>
<xsd:element name="genre" type="xsd:string"/>
<xsd:element name="price" type="xsd:float" />
<xsd:element name="pub_date" type="xsd:date" />
<xsd:element name="review" type="xsd:string"/>
</xsd:sequence>
<xsd:attribute name="id" type="xsd:string"/>
</xsd:complexType>
</xsd:schema>
=================================================
This is the pl/sql:
function GetSpecificationById (p_spec_id in NUMBER) return XMLType
IS
l_specification sys.XMLType;
BEGIN
select specification
into l_specification
from specifications
where id = p_spec_id;
return l_specification;
EXCEPTION
WHEN OTHERS THEN -- handles all other errors
ROLLBACK;
END GetSpecificationById;
==============================================
And this is the generated WSDL:
<?xml version="1.0" encoding="UTF-8" ?>
<wsdl:definitions
name="MyWebService1"
targetNamespace="http://devsafetrytrailcom/MyWebService1.wsdl"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:tns="http://devsafetrytrailcom/MyWebService1.wsdl"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/"
>
<wsdl:types>
</wsdl:types>
<wsdl:message name="MyWebService1_getspecificationbyid">
<wsdl:part name="pSpecId" type="xsd:decimal"/>
</wsdl:message>
<wsdl:message name="MyWebService1_getspecificationbyidResponse">
*<wsdl:part name="result" type="xsd:string"/>*
</wsdl:message>
<wsdl:portType name="MyWebService1">
<wsdl:operation name="getspecificationbyid" parameterOrder="pSpecId">
<wsdl:input message="tns:MyWebService1_getspecificationbyid"/>
<wsdl:output message="tns:MyWebService1_getspecificationbyidResponse"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="MyWebService1" type="tns:MyWebService1">
<soap:binding style="rpc"
transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="getspecificationbyid">
<soap:operation soapAction="http://devsafetrytrailcom/MyWebService1.wsdl/getspecificationbyid"/>
<wsdl:input>
<soap:body use="literal"
namespace="http://devsafetrytrailcom/MyWebService1.wsdl"
parts="pSpecId"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"
namespace="http://devsafetrytrailcom/MyWebService1.wsdl"
parts="result"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="MyWebService1">
<wsdl:port name="MyWebService1Port" binding="tns:MyWebService1">
<soap:address location="http://localhost:7101/SafetyTrail-Model-context-root/MyWebService1Port"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
Edited by: user592403 on Jul 26, 2010 9:57 AMAnyone seen this problem before??
-
Using pl/sql functions for transformation
How can I use pl/sql functions in the transformation mapping field for my interface?
I have a name field where firstname and lastname are concatenated via a space-character and I would like to extract this 'name' field to 2 separate database attributes.
This means that I need to use a function that uses SUBSTRING and INSTR to be able to get the firstname and lastname separatly out of the name-field.
The INSTR-function isn't known inside the expression editor in ODI so I'm wondering how I can use my own function?Hi Romanna,
Are you sure? Where did you do your transformation?
-Source, Staging area or Target
I try this on Oracle target...
FIRST_NAME = substr(MYTABLE.FIRST_LASTNAME, 1, instr(MYTABLE.FIRST_LASTNAME, ' ')-1)
LAST_NAME = substr(MYTABLE.FIRST_LASTNAME,instr(MYTABLE.FIRST_LASTNAME, ' ')+1) -
Sql function for fetching only initail char of string
Hi All,
I would like to know is there any sql builtin function which returns initial char(abbrevation) of any string pass to it
for eg. DEPARTMENT SUB GROUP should return DSG..
Pls do share with me if any 1 is aware of this or who has already self defined any function for this purpose.
Thanks in AdvanceAnd if you have 10g available, here's the regex version:
with t as (select 'DEPARTMENT SUB GROUP' col1
from dual)
select t.col1, regexp_replace(t.col1, '(\w)\w*\s*', '\1')
from t;Small correction, just in case we only have 1 letter words.
C. -
SQL function for PKIDs in 6.1.1
Hi,
I would like to ask if there is a new SQL function to generate the shorter PKIDs in 6.1.1?
Thanks in advance.We do not yet have database functions to generate the new shortened PKIDs. If you want this, you can log an Enhancement Request with support..
Thanks,
Ron -
How can i make a pl/sql function for BLOB datatype
hi..anyone here who is very familiar about BLOB datatype. i have a COLUMN_ID, COLUMN_A in a certain TABLE_1. COLUMN_A is blob datatype that contains almost 250,000rows
SQL>select column_A from table_1 where column_id=1234567
column_A
00000001000000010000000606D4E833074B69EC06D4E91F074CO18406D50C58074C031E
how can i make a user-defined function to compute and convert this blob datatype into decimal length value. this hex value are points in the map.. the function must contain
1.get the length of a blob then
2.convert blob to variable hexadecimal characters by parsing it into 8
3.to_number function or other function used to convert haxadecimal characters to decimal numbers
4.phythagorean formula to compute length between two points. this is the formula i think LENGTH =
SQRT(power((coordinate_x-prev_coordinate_x),2)+power((coordinate_y-prev_y),2));
after this when i type this
SQL>select user_function(column_A) from table_1 where column_id=1234567
user_functions(column_A)
--output length will be in decimal value already
the function will goes like this
step1 is to get the blob length
00000001000000010000000606D4E833074B69EC06D4E91F074CO18406D50C58074C031E
step2 is parsing of data by eights
00000001 =>1
00000001 =>1
00000006 =>6 (number of coordinates)
06D4E833 => X1
074B69EC => Y1
06D4E91F => X2
074CO184 => Y2
06D50C58 => X3
074C031E => Y3
step3 to_number function used to convert hex char to decimal
step4 compute by phytagorean (NOTE ! when computing length the third parsed eight will tell how many coordinates are there..the number of coordinates is ranging from 2 up to E..above example shows 6 coordinates so it means
LENGTH1 =
SQRT(power((X2-X1),2)+power((Y2-Y1),2));
LENGTH2=
SQRT(power((X3-X2),2)+power((Y3-Y2),2));
TOTAL LENGTH=LENGTH1 + LENGTH2
thanksits my first time to use that.There's got to be a first tiem for anything. Be brave.
btw if theres more easy suggestion pls feel free..Well of course the easiest solution would be if the calling program passed in the parameters as separate arguments instead of glomming them together in a string that has to be parsed. This sort of kluj really ought not to have survived into C21.
Cheers, APC -
Using pl/sql function for each day between two dates.
Hi,
create TABLE EMP(
ID_EMP NUMBER,
DT_FROM DATE,
DT_TO DATE,
CREATE_DATE DATE);
into EMP(ID_EMP, DT_FROM, DT_TO, CREATE_DATE)
Values(100, TO_DATE('07/01/2008 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('04/30/2010 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),TO_DATE('05/08/2009 14:11:21', 'MM/DD/YYYY HH24:MI:SS'));
I have a function called elig_pay_dates(date p_date), which returns the code for person payment eligibility for a particular date. For paid dates it's 'P' and for unpaid dates it's 'N'.
How can I check this function between two dates for each day. Example : 07/01/2008 to 04/30/2010.
By using this function with select I needs to display the dates when there is a change in status.
I am expecting data in following manner from above logic(this is example):
07/01/2008 --- 07/01/2009 ---'P'
07/02/2009 -- 07/25/2009 ----'N'
07/26/2009 -- 01/01/2010 ---'P'
01/02/2010 -- 01/13/2010 --'N'
01/14/2010 -- 01/18/2010 --'P'
01/19/2010 -- 04/30/2010 -- 'N'
I thought of looping for each day date but that seems to be expensive for online application. Is there any way that I can achieve this requirement with sql query ?
Thanks for your help,Certainly not the best way to code the requirement, but it does achieve the result you are looking for in a fairly quick time
create or replace
function test_ret_paid_unpaid (p_date in date)
return varchar2
is
v_ret varchar2(1);
begin
if ( (p_date between to_date('07/02/2009', 'MM/DD/YYYY') and to_date('07/25/2009', 'MM/DD/YYYY') ) or
(p_date between to_date('01/02/2010', 'MM/DD/YYYY') and to_date('01/13/2010', 'MM/DD/YYYY') ) or
(p_date between to_date('01/19/2010', 'MM/DD/YYYY') and to_date('04/30/2010', 'MM/DD/YYYY') )
then v_ret := 'N';
else
v_ret := 'Y';
end if;
return v_ret;
end;
Wrote file afiedt.buf
1 with get_paid_unpaid as
2 (
3 select dt_from start_date, dt_to end_date, dt_from + level - 1 curr_date, test_ret_paid_unpaid(dt_from + level - 1) paid_unpaid,
4 row_number() over (order by dt_from + level - 1) rn_start,
5 row_number() over (order by dt_from + level - 1 desc) rn_end
6 from test_emp
7 connect by level <= dt_to - dt_from + 1
8 ),
9 get_stop_date as
10 (
11 select start_date init_date, end_date, curr_date, paid_unpaid,
12 case when paid_unpaid != lag(paid_unpaid) over (order by curr_date) or rn_start = 1 or rn_end = 1
13 then curr_date
14 else null
15 end start_date,
16 case when paid_unpaid != lead(paid_unpaid) over (order by curr_date) or rn_start = 1 or rn_end = 1
17 then curr_date
18 else null
19 end stop_date
20 from get_paid_unpaid
21 )
22 select period, paid_unpaid
23 from (
24 select init_date, curr_date, start_date, end_date, stop_date,
25 case when paid_unpaid = lead(paid_unpaid) over (order by curr_date)
26 then nvl(start_date, init_date) || ' - ' || lead(stop_date, 1, end_date) over (order by curr_date)
27 else null
28 end period,
29 paid_unpaid
30 from get_stop_date
31 where stop_date is not null or start_date is not null
32 )
33* where period is not null
12:06:10 SQL> /
PERIOD PAID_UNPAID
01-JUL-08 - 01-JUL-09 Y
02-JUL-09 - 25-JUL-09 N
26-JUL-09 - 01-JAN-10 Y
02-JAN-10 - 13-JAN-10 N
14-JAN-10 - 18-JAN-10 Y
19-JAN-10 - 30-APR-10 N
6 rows selected.
Elapsed: 00:00:00.35 -
Sql function for trimming from center
Hi Experts;
Is there any function which can remove/trim from center like i have a number
01-1552963-01 and i want to remove - from it.
01155296301
note that i dont want to use substr and concat ||
regards,RB wrote:
AND With REGEXP_REPLACE
SQL> SELECT REGEXP_REPLACE('01-1552963-01','-','') Sample from dual;
SAMPLE
01155296301
An empty string is considered null in Oracle so that's the same as:
SQL> SELECT REGEXP_REPLACE('01-1552963-01','-') Sample from dual;
SAMPLE
01155296301which, let's face it is pretty pointless when a regular REPLACE function will do the same.
You should only really use the regular expression versions when you have a pattern that needs matching, not just a fixed set of characters. -
SQL function for adding street, city and postal code.
I would like to know how to add to my code to display the addresses.
Any ideas?
SELECT departments.department_name "Department Name", locations.state_province "Mailing Address"
FROM departments, locations
WHERE departments.location_id=locations.location_id
AND locations.country_id='US'
ORDER BY department_name;
OUTPUT SO FAR:
Department Name Mailing Address
Accounting Washington
Administration Washington
Contracting Washington
Executive Washington
IT Texas
Shipping CaliforniaHi,
Do you want the four database columns to appear as one output column, perhaps with a space between the columns?
If so, concatenate the strings into one big string using ||
SELECT departments.department_name "Department Name"
, street_address
|| ' ' || city
|| ' ' || locations.state_province
|| ' ' || postal_code AS "Mailing Address"
FROM departments, locations
WHERE departments.location_id=locations.location_id
AND locations.country_id='US'
ORDER BY department_name;
Message was edited by:
Frank Kulash -
Invalid state in SQL query for a function that was created with no errors.
SQL> CREATE OR REPLACE FUNCTION overlap(in_start1 IN TIMESTAMP, in_end1 IN TIMESTAMP, in_start2 IN TIMESTAMP, in_end2 IN TIMESTAMP) RETURN NUMBER
2 IS
3
4 BEGIN
5 IF (in_start1 BETWEEN in_start2 AND in_end2 OR in_end1 BETWEEN in_start2 AND in_end2 OR in_start2 BETWEEN in_start1 AND in_end1) THEN
6 RETURN 0;
7 ELSE
8 RETURN 1;
9 END IF;
10 END;
11 /
Function created.
SQL> show errors;
No errors.
SQL>
SQL> SELECT * FROM tbl where overlaps(current_time,current_time+1,current_time-1,current_time+2) = 0;
SELECT * FROM tbl where overlaps(current_time,current_time+1,current_time-1,current_time+2) = 0
ERROR at line 1:
ORA-06575: Package or function OVERLAPS is in an invalid state
I do not understand why overlaps is returned as in invalid state in the query, when it was created with no errors earlier. Could anyone help me?Marius
Looking at the logic you are trying to create it looks like you are looking for overlapping time periods.
Consider two date/time ranges:
Range 1 : T1 - T2
Range 2 : T3 - T4
Do they overlap?
1) No: T1 < T4 (TRUE) T2 > T3 (FALSE)
T1 --- T2
T3 --- T4
2) Yes: T1 < T4 (TRUE) T2 > T3 (TRUE)
T1 ---------- T2
T3 --- T4
3) Yes: T1 < T4 (TRUE) T2 > T3 (TRUE)
T1 -------------------- T2
T3 --- T4
4) Yes: T1 < T4 (TRUE) T2 > T3 (TRUE)
T1 ----- T2
T3 --- T4
5) Yes: T1 < T4 (TRUE) T2 > T3 (TRUE)
T1 --- T2
T3 ------------ T4
5) No: T1 < T4 (FALSE) T2 > T3 (TRUE)
T1 --- T2
T3 --- T4Answer: Yes they overlap if:
T1 < T4 AND T2 > T3
So you can code the logic in your SQL as simply:
SELECT *
FROM tbl
WHERE range1_start < range2_end
AND range_1_end > range2_startIf you go around implementing PL/SQL functions for simple logic that can be achieved in SQL alone then you cause context switching between the SQL and PL/SQL engines which degrades performance. Wherever possible stick to just SQL and only use PL/SQL if absolutely necessary.
Maybe you are looking for
-
Very Slow Load Times for PDF Document
Hi, I have a PDF document that takes forever to load in preview (on the order of a minute or more). It is eight pages, but when I try to go to the last two pages, each of which has a drawing created by using Autoshapes in Microsoft Word, again they t
-
Changing data in planning modular
hi i have created the planning function type rsplf1. in planning modular i have actual and planning cube . the standard 0quantity is used in both the cube. how do i process my actual qty in actual cube and store them to planning cube. since i used th
-
How can I measure RMS with the AI-102?
Hi folks, Can anyone tell me a way to measure RMS voltage with the AI-102? I need to measure 0-1 VAC. It seems like there should be a way to collect a sample of voltages and put them in an array to calculate RMS. Since I'm not approacing the limit
-
Flash won't work no matter what i do
I have been having an issue with flash, whether it's video's or games, nothing will load. i have uninstalled and reinstalled it twice, i have cleared the caches and followed every bit of advice to make it work and still nothing. i noticed that i have
-
Using iTunes match will this clear room on the hard drive
using itunes will this clear room on the hard drive