Hi,
I created a sql function to calculate the difference between two dates (age = age + 1 if month of birth >= 6 than age = age +1), in
datef = today date
datei = date of birth
CREATE FUNCTION QGPL/DIFDATA ( DATEI CHAR , DATEF CHAR )
RETURNS INT
LANGUAGE SQL
BEGIN
DECLARE YEAR_AGE INTEGER DEFAULT 0;
DECLARE MONTH_AGE INTEGER DEFAULT 0;
DECLARE AGE INTEGER DEFAULT 0;
SET YEAR_AGE =
INT(substr ( digits(DATE(SUBSTR(CHAR( DATEF ), 1, 4)
||'-'||
SUBSTR(CHAR( DATEF ), 5, 2)
||'-'||
SUBSTR(CHAR( DATEF ), 7, 2))
-
DATE(SUBSTR(CHAR( DATEI ), 1, 4)
||'-'||
SUBSTR(CHAR( DATEI ), 5, 2)
||'-'||
SUBSTR(CHAR( DATEI ), 7, 2)) ) , 1, 4 ) ) ;
SET MONTH_AGE =
INT(substr ( digits(DATE(SUBSTR(CHAR( DATEF ), 1, 4)
||'-'||
SUBSTR(CHAR( DATEF ), 5, 2)
||'-'||
SUBSTR(CHAR( DATEF ), 7, 2))
-
DATE(SUBSTR(CHAR( DATEI ), 1, 4)
||'-'||
SUBSTR(CHAR( DATEI ), 5, 2)
||'-'||
SUBSTR(CHAR( DATEI ), 7, 2)) ) , 5, 2 ) ) ;
SET AGE = ANOS ;
CASE WHEN MONTH_AGE >= 6 THEN SET AGE = YEAR_AGE + 1; END CASE;
RETURN YEAR_AGE ;
END
and in a sql statement line in Iseries (AS400), it created the function in QGPL with the name DIFDATA.
Now i need to use the function in a sql statement to calculate the age and i'm doing this:
SELECT NAME , QGPL/DIFDATA( 20110928 , 19000629) as YEAR_AGE FROM
MYLIBL/TEST
However i got this message:
SQL State: 42703
Code of seller: -206
Message: [SQL0206] Column QGPL is not specified in the tables. Cause. . . . . : QGPL is not a table column * N * N. If the table is * N, QGPL is not a column of any table or view that can be referenced. Recovery. . : Do one of the following and try the request again: - Make sure that the column names and table are correctly specified in the instruction. - If this is a SELECT statement, ensure that all required tables were named in the FROM clause. - If the column was intended to be a correlated reference, qualify the column with the correct table designator.
I created a sql function to calculate the difference between two dates (age = age + 1 if month of birth >= 6 than age = age +1), in
datef = today date
datei = date of birth
CREATE FUNCTION QGPL/DIFDATA ( DATEI CHAR , DATEF CHAR )
RETURNS INT
LANGUAGE SQL
BEGIN
DECLARE YEAR_AGE INTEGER DEFAULT 0;
DECLARE MONTH_AGE INTEGER DEFAULT 0;
DECLARE AGE INTEGER DEFAULT 0;
SET YEAR_AGE =
INT(substr ( digits(DATE(SUBSTR(CHAR( DATEF ), 1, 4)
||'-'||
SUBSTR(CHAR( DATEF ), 5, 2)
||'-'||
SUBSTR(CHAR( DATEF ), 7, 2))
-
DATE(SUBSTR(CHAR( DATEI ), 1, 4)
||'-'||
SUBSTR(CHAR( DATEI ), 5, 2)
||'-'||
SUBSTR(CHAR( DATEI ), 7, 2)) ) , 1, 4 ) ) ;
SET MONTH_AGE =
INT(substr ( digits(DATE(SUBSTR(CHAR( DATEF ), 1, 4)
||'-'||
SUBSTR(CHAR( DATEF ), 5, 2)
||'-'||
SUBSTR(CHAR( DATEF ), 7, 2))
-
DATE(SUBSTR(CHAR( DATEI ), 1, 4)
||'-'||
SUBSTR(CHAR( DATEI ), 5, 2)
||'-'||
SUBSTR(CHAR( DATEI ), 7, 2)) ) , 5, 2 ) ) ;
SET AGE = ANOS ;
CASE WHEN MONTH_AGE >= 6 THEN SET AGE = YEAR_AGE + 1; END CASE;
RETURN YEAR_AGE ;
END
and in a sql statement line in Iseries (AS400), it created the function in QGPL with the name DIFDATA.
Now i need to use the function in a sql statement to calculate the age and i'm doing this:
SELECT NAME , QGPL/DIFDATA( 20110928 , 19000629) as YEAR_AGE FROM
MYLIBL/TEST
However i got this message:
SQL State: 42703
Code of seller: -206
Message: [SQL0206] Column QGPL is not specified in the tables. Cause. . . . . : QGPL is not a table column * N * N. If the table is * N, QGPL is not a column of any table or view that can be referenced. Recovery. . : Do one of the following and try the request again: - Make sure that the column names and table are correctly specified in the instruction. - If this is a SELECT statement, ensure that all required tables were named in the FROM clause. - If the column was intended to be a correlated reference, qualify the column with the correct table designator.