If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Register UDF?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-09-08, 09:20
Tyveleyn Tyveleyn is offline
Registered User
 
Join Date: Aug 2006
Location: The Netherlands
Posts: 248
Register UDF?

Hi, in my UDB 8.2 I created an UDF that represents the builtin 'CURRENT DATE' function. It has the name 'TODAY' and is nothing more than a SQL expression UDF. When run in the Development center it returns today's date. But when incorporated in a SQL statement (in the Control Center or my application) like this:
Code:
SELECT * FROM TABLENAME WHERE LASTDATE < TODAY;
I get:
Quote:
SQL0206N "TODAY" is not valid in the context where it is used.
SQLSTATE=42703
It's executed by the same user as the owner of the UDF and according to the properties in the Control Center that user has EXECUTE privileges.
What's missing here?

Thanks in advance and greetings,
Hans
Reply With Quote
  #2 (permalink)  
Old 10-09-08, 11:13
Tyveleyn Tyveleyn is offline
Registered User
 
Join Date: Aug 2006
Location: The Netherlands
Posts: 248
Rephrase question

Hi, regarding the question above I can say that the solution is to use the UDF as 'TODAY()'. Since it's meant as a translation of the Informix builtin SQL function 'TODAY' I need to use it without the parentheses. Is that possible?

Tanks,
Hans
Reply With Quote
  #3 (permalink)  
Old 10-09-08, 13:22
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
If you are using a UDF, then you have to have the parenthesis. Why not use CURRENT DATE?

Andy
Reply With Quote
  #4 (permalink)  
Old 10-09-08, 13:47
Tyveleyn Tyveleyn is offline
Registered User
 
Join Date: Aug 2006
Location: The Netherlands
Posts: 248
Hi, the application I'm working on is designed for usage with a Informix database and there TODAY is the builtin equivalent of DB2's CURRENT DATE. The database on my laptop I use for development is DB2 and I don't want to alter the applications code to fit the DB2 SQL syntax. A builtin function CURRENT (for representing a DATETIME value) would also be fit for my needs but I don't believe that's in DB2.
Would a Java written stored procedure or a routine in C be able to do the trick by your knowledge?

Thanks anyway,
Hans
Reply With Quote
  #5 (permalink)  
Old 10-09-08, 14:11
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
You are going to have to change the code. DB2 has no TODAY special register. You do not need to create anything in C or java.

Andy
Reply With Quote
  #6 (permalink)  
Old 10-10-08, 00:41
Tyveleyn Tyveleyn is offline
Registered User
 
Join Date: Aug 2006
Location: The Netherlands
Posts: 248
OK thanks. I think there might be another solution which I'm trying to find now.

Regards,
Hans
Reply With Quote
  #7 (permalink)  
Old 10-10-08, 01:32
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
My idea is to create UDF(for example, today_() ) on Informix and DB2.
Reply With Quote
  #8 (permalink)  
Old 10-10-08, 02:19
Tyveleyn Tyveleyn is offline
Registered User
 
Join Date: Aug 2006
Location: The Netherlands
Posts: 248
Thanks, but I thought of another similar function in both databases. For that I need to get the date of the first day of a given month in SQL. E.g. I need the date of the first day of today's month minus 5 months, what will be 2008-05-01. In Informix that's no problem for me but I'm still trying in DB2. Any suggestions in that matter are appreciated.

Greetings,
Hans
Reply With Quote
  #9 (permalink)  
Old 10-10-08, 02:58
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
An example in DB2.
Code:
------------------------------ Commands Entered ------------------------------
CREATE FUNCTION Firstday
(in_date  DATE
,in_month INTEGER
)
 RETURNS DATE
 LANGUAGE SQL
 CONTAINS SQL
 DETERMINISTIC
 NO EXTERNAL ACTION
RETURN
in_date - in_month MONTHS
- (DAY(in_date - in_month MONTHS) - 1) DAYS
;
------------------------------------------------------------------------------
DB20000I  The SQL command completed successfully.

------------------------------ Commands Entered ------------------------------
VALUES Firstday(DATE('2008-10-10'), 5);
------------------------------------------------------------------------------

1         
----------
2008-05-01

  1 record(s) selected.


------------------------------ Commands Entered ------------------------------
VALUES Firstday(DATE('2008-10-31'), 1);
------------------------------------------------------------------------------

1         
----------
2008-09-01

  1 record(s) selected.
Reply With Quote
  #10 (permalink)  
Old 10-10-08, 04:44
Tyveleyn Tyveleyn is offline
Registered User
 
Join Date: Aug 2006
Location: The Netherlands
Posts: 248
Hi, the function as you wrote it is not deployeable in my development center. But the return expression works nicely in a single SQL statement so I got the picture. Thanks a lot!

Greetings,
Hans
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On