473,387 Members | 1,771 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

DB2 UDF...need to eliminate IF statement

chigasakigaijin
I have a UDF I would like setup on a DB2 mainframe server. We have a test version of the database located on a windows server. I have been testing my UDF with this. So far, it works exactly as it should:

Expand|Select|Wrap|Line Numbers
  1. CREATE FUNCTION DATE2SQL (DATEI DATE)
  2.             RETURNS DATE
  3.             LANGUAGE SQL
  4. BEGIN atomic
  5.            DECLARE RESULT DATE;
  6.            IF CAST(DATEI AS VARCHAR(10)) < '1753-01-01'
  7.                            THEN SET RESULT =  '1753-01-01' ;
  8.            ELSE SET RESULT = DATEI;
  9.            END IF;
  10.            RETURN RESULT;
  11. END@
What it does: Pull a date from DB2, if it's pre-Jan. 1, 1753, return Jan. 1, 1753, or else return the date as is (be it null, July 3, 2005, whatever).

Why it does it: Because I'm importing the db2 database into MS SQL server, where dates pre-1753 are not accepted.

What's the problem: The problem is I have been told this UDF, while it works on the windows test box, will not function when it's rolled out to the mainframe, because of the IF statement.

What can I do to replace the IF statement so that the UDF functions on the mainframe, and will still do what I need it to do.

Any help would be greatly appreciated!

chigasakigaijin
Jan 2 '08 #1
4 4938
docdiesel
297 Expert 100+
Hi,

I guess you are using this UDF to export your data, in something like
Expand|Select|Wrap|Line Numbers
  1. SELECT date2sql(mydate) FROM ...
before importing it to M$-SQL? Then you easily could forget about the UDF and use a CASE in your SELECT statement:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.   CASE WHEN mydate < '1753-01-01'
  3.   THEN '1753-01-01'
  4.   ELSE mydate
  5.   END
  6. FROM
  7.   mytable ...
Is this some help for you?

Regards, Bernd
Jan 3 '08 #2
Thanks for the reply!

I'm just trying to do this efficiently, because of the size of this project (about 200 tables, about 2000 fields), so

Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2. date2sql(fieldname1) as fieldname1, 
  3. date2sql(fieldname2) as fieldname2, 
  4. date2sql(fieldname3) as fieldname3
  5. FROM tablename
is a lot less hassle than

Expand|Select|Wrap|Line Numbers
  1. SELECT
  2. CASE WHEN CAST(fieldname1 as varchar(10)) < '1753-01-01'
  3. THEN '1753-01-01'
  4. ELSE fieldname1
  5. END as fieldname1
  6. CASE WHEN CAST(fieldname2 as varchar(10)) < '1753-01-01'
  7. THEN '1753-01-01'
  8. ELSE fieldname2
  9. END as fieldname2
  10. CASE WHEN CAST(fieldname3 as varchar(10)) < '1753-01-01'
  11. THEN '1753-01-01'
  12. ELSE fieldname3
  13. END as fieldname3
  14. FROM tablename
So I'd like to get the UDF functioning on the mainframe (by replacing the IF statement.) I just don't know anything about UDF programming in DB2, and I don't have access to anyone in my organization who can help me....the big question is, how can I replace an IF statement in a DB2 UDF?

Thanks again for your reply!
Jan 4 '08 #3
docdiesel
297 Expert 100+
Hi,

sorry, I'm not a big expert in UDFs. Nevertheless, I think that the CASE way is faster than using the UDF. (Imho this should be worth a test.) If you want your export calls to be just a "SELECT * FROM ...", you could create some views on base of the "SELECT ... CASE ..." statements.

Regards,

Bernd
Jan 4 '08 #4
After doing some research, I found that you can use CASE statements in DB2 UDF's without any corresponding SELECT statement, so it works just like an IF statement.

Original Code (IF statement):
Expand|Select|Wrap|Line Numbers
  1. CREATE FUNCTION DATE2SQL (DATEI DATE)
  2.  RETURNS DATE
  3.  LANGUAGE SQL
  4. BEGIN atomic
  5.  DECLARE RESULT DATE;
  6.  IF CAST(DATEI AS VARCHAR(10)) < '1753-01-01'
  7.  THEN SET RESULT =  '1753-01-01' ;
  8.  ELSE SET RESULT = DATEI;
  9.  END IF;
  10. RETURN RESULT;
  11. END@
  12.  
Updated Code (CASE statement):
Expand|Select|Wrap|Line Numbers
  1. CREATE FUNCTION DATE2SQL (DATEI DATE)
  2.  RETURNS DATE
  3.  LANGUAGE SQL
  4. BEGIN atomic
  5.  DECLARE RESULT DATE;
  6.  SET RESULT =  (CASE
  7.  WHEN (CAST(DATEI AS VARCHAR(10)) < '1753-01-01') 
  8.  THEN '1753-01-01'
  9.  ELSE DATEI
  10.  END); 
  11. RETURN RESULT;
  12. END@
This solves my problem, whew! Thanks for the suggestions!/

chigasakigaijin
Jan 4 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Gianni Mariani | last post by:
I have a couple of template methods that take any integer type however, the first "if" statement becomes a constant expression when T is an unsigned type. #include <limits> template...
14
by: Pratts | last post by:
I am a new one who have joined u plz try to help me bcoz i could not find ny sutiable answer foer this Question Qus>>why do we need classes when structures provide similar functionality??
2
by: edself | last post by:
Greetings, I am semi-new to Access and have a query question. I presume the solution is easy, but need some help. I have created a database with a Contact table. The contact table contains...
106
by: xtra | last post by:
Hi Folk I have about 1000 procedures in my project. Many, many of them are along the lines of function myfuntion () as boolean on error goto er '- Dim Dbs as dao.database Dim Rst as...
0
by: Tiga | last post by:
Right off I know nothing about programming so any help will be useful. We have an application that I have manipulated by creating an IF statement which is below. ...
48
by: phillip.s.powell | last post by:
MySQL 3.23.58 - 4.0.17 (yep, several database server instances, don't ask) I have database Spring with table Students I have database Summer with table Students I am tasked to produce a...
56
by: Ed Jay | last post by:
I note in an Eric Meyers book that he expresses one goal of using CSS is to eliminate all <brtags. Why? -- Ed Jay (remove 'M' to respond by email)
4
by: sara | last post by:
i am studying a computer engineering and i started taking programming using C++ since month i have question i think it`s easy for you all *prof.programmer* but it`s bit diffecult for me plzz i...
18
by: chilipepas | last post by:
Hello. I receive from a database a variable number of results. In my page, I can show a maximum of 36 results. I want to "neatly" eliminate the exceeding results. For example: say, we...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.