473,465 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Do calculation in Char filed

Hello all,

is there any way, can we work around with mathematical calculation in
char filed, like if i am storing '3+5' in character field, can make a
calculation on the above field and get the values 8 .

thanks and regards
situ

May 22 '07 #1
7 2230
situ wrote:
Hello all,

is there any way, can we work around with mathematical calculation in
char filed, like if i am storing '3+5' in character field, can make a
calculation on the above field and get the values 8 .
Not natively in DB2. You need something that parses the string, converts
the numbers to an integer or floating point data type, and then calculates
your result. Writing such a UDF should be rather trivial, given that there
are lots of C/Java examples out there that implement a "calculator". Just
wrap those into a UDF.

--
Knut Stolze
DB2 z/OS Utilities Development
IBM Germany
May 22 '07 #2
On May 22, 1:34 pm, Knut Stolze <sto...@de.ibm.comwrote:
situ wrote:
Hello all,
is there any way, can we work around with mathematical calculation in
char filed, like if i am storing '3+5' in character field, can make a
calculation on the above field and get the values 8 .

Not natively in DB2. You need something that parses the string, converts
the numbers to an integer or floating point data type, and then calculates
your result. Writing such a UDF should be rather trivial, given that there
are lots of C/Java examples out there that implement a "calculator". Just
wrap those into a UDF.

--
Knut Stolze
DB2 z/OS Utilities Development
IBM Germany
Thanks ...

May 22 '07 #3
Maybe hooking up a LISP interpreter would be the easiest...

Cheers
Serge

PS: This goes way beyond "duck-typing" as I know it....

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
May 22 '07 #4
Serge Rielau wrote:
Maybe hooking up a LISP interpreter would be the easiest...

Cheers
Serge

PS: This goes way beyond "duck-typing" as I know it....
Actually, there is a quite simple way to do that with an external UDF. The
following (untested) C code is still missing proper error/NULL handling and
it assumes that "bc" is installed at the DB2 server.

void SQL_API_FN calculator(
SQLUDF_VARCHAR *formula,
SQLUDF_DOUBLE *result,
SQLUDF_NULLIND *formula_ind,
SQLUDF_NULLIND *result_ind,
SQLUDF_TRAIL_ARGS)
{
FILE *pipe = NULL;
char str[1000] = { '\0' };

sprintf(command, "echo \"%s\" | bc", formula);
pipe = popen("bc", "r");
fgets(str, sizeof str, pipe);
pclose(pipe);
*result = strtod(str);
*result_ind = 0;
}

Use this as entry point for an external UDF with a VARCHAR(980) as input
parameter and returning a DOUBLE. You can choose whichever maximum length
suits you - just adjust the size of the char array in the C code.

--
Knut Stolze
DB2 z/OS Utilities Development
IBM Germany
May 22 '07 #5
Is there any function for converting String to number & vise versa ?

May 23 '07 #6
swami wrote:
Is there any function for converting String to number & vise versa ?
Of course: CHAR() and INT(), DOUBLE(), DECIMAL(), ...
Also you can use the "CAST-specification".

Cheers
Serge

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
May 23 '07 #7
Make UDF in that let DB2 do calculation by using dynamic SQL.
The weak point of this strategy is need to specify MODIFIES SQL DATA
because of using dynamic SQL. Therefor, the UDF must be TABLE UDF.
There are some restriction to use TABLE UDF.
Followings are one example:

CREATE FUNCTION Calculate (inStr VARCHAR(100))
RETURNS TABLE (Result FLOAT)
MODIFIES SQL DATA
DETERMINISTIC
NO EXTERNAL ACTION
BEGIN ATOMIC
DECLARE Result FLOAT;
CALL Calculate(inStr, Result);
RETURN VALUES Result;
END
!

CREATE PROCEDURE Calculate (IN inStr VARCHAR(100)
,OUT Result FLOAT)
MODIFIES SQL DATA
DETERMINISTIC
NO EXTERNAL ACTION
BEGIN NOT ATOMIC
DECLARE CalcSelect VARCHAR(130) DEFAULT '';
DECLARE PrepSelect STATEMENT;
DECLARE C1 CURSOR FOR PrepSelect;
SET CalcSelect = 'SELECT '||COALESCE(inStr,'CAST(NULL AS FLOAT)')
||' FROM SYSIBM.SYSDUMMY1';
PREPARE PrepSelect FROM CalcSelect;
OPEN C1;
FETCH C1 INTO Result;
CLOSE C1;
END
!

Sample execution:
-------------------- Commands Entered --------------------
SELECT deptno
, mgrno
, INT(Result) INT_Result
FROM Department D
, TABLE( Calculate(mgrno||'*10+5') ) C!
----------------------------------------------------------

DEPTNO MGRNO INT_RESULT
------ ------ -----------
A00 000010 105
B01 000020 205
C01 000030 305
D01 - -
D11 000060 605
D21 000070 705
E01 000050 505
E11 000090 905
E21 000100 1005

9 record(s) selected.

May 23 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: kekekeke | last post by:
create function dbo.iso6346_char_to_number ( @char char ) returns int as begin return case upper(@char) when '0' then 0
3
by: gbb0330 | last post by:
here is my code Sub test() Dim Rst As New ADODB.Recordset Dim SQLstmnt As String Dim RQ0 As Integer Dim CurrentListedItemsNUM As Long Dim SumLQav0 As Integer
2
by: Peteroid | last post by:
The following code: char x_char = '0' ; String^ x_str = c_char.ToString( ) ; results in: x_str = = "48" and not:
4
by: mmeldrum | last post by:
Hoping someone can help me out here. I have a form which performs calcualtions in it. The form was build using a table with the exact same fields. I placed the one expression in the actual field...
13
by: Maria Mela | last post by:
Hello everyone... Anybody can help me on this question, please? i´ve this code in my application: /*for Card creation*/ typedef struct card { etc,etc } Card; typedef Card *PtrCrt;
5
by: The alMIGHTY N | last post by:
Hi all, Let's say I have a simple math formula: sum (x * y / 1000) / (sum z / 1000) I have to do this across 50 items, each with an x, y and z value, when the page first loads AND when a...
4
by: voroojak | last post by:
Hi i am working with report service in sql server 2005. I have a problem with calculation. what i want to do is something like this =IIf Fields!total_spb_week_this_year.Value= 0 then 0 else ...
4
by: wrldruler | last post by:
Hello, First, I know it's against "Access Law" to save calculations in a table, but....I want/need to. I currently have sub-totals being calculated inside a form, using DMax, DCount, and...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.