473,385 Members | 1,343 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,385 software developers and data experts.

URGENT. Can't get a cast to work...

Hi all,

I have this table:

create table my_table (file_id int not null, property_id int not null,
property_value varchar(255));

In some instances the property_value is a string representing an
integer. I would like to do this:

select file_id from my_table where property_id = 101 and
int(property_value) 60 and
file_id in (select file_id from some_other_table);

Now:

* some_other_table contains 200 file_ids
* my_table contains thousands of rows

If I run this query:

select file_id from my_table where property_id = 101 and file_id in
(select file_id from some_other_table);

I get 50 rows, meaning there are 50 potential matches to test against
int(property_value) 60.

The query fails with SQL0420N Invalid character found in a character
string...etc.

I checked the property_value contents for the selected subset and they
are all proper integer representations.

I've assumed that the query is executed in such a way that DB2 first
checks for

"int(property_value) 60" before it checks for any other condition
and it therefore encounters nulls that it can't cast.

(BTW, I'm aware that the "file_id in (select file_id from
some_other_table)" condition can be expressed as a join, but it won't
solve the problem).

Am I right? HOW DO I FIX THIS?? I'm guessing it's a job for COALESCE,
but I can't get the syntax right either.

Thanks all,

Alejandrina

Apr 25 '07 #1
2 3162
apattin wrote:
Hi all,

I have this table:

create table my_table (file_id int not null, property_id int not null,
property_value varchar(255));

In some instances the property_value is a string representing an
integer. I would like to do this:

select file_id from my_table where property_id = 101 and
int(property_value) 60 and
file_id in (select file_id from some_other_table);

Now:

* some_other_table contains 200 file_ids
* my_table contains thousands of rows

If I run this query:

select file_id from my_table where property_id = 101 and file_id in
(select file_id from some_other_table);

I get 50 rows, meaning there are 50 potential matches to test against
int(property_value) 60.

The query fails with SQL0420N Invalid character found in a character
string...etc.

I checked the property_value contents for the selected subset and they
are all proper integer representations.

I've assumed that the query is executed in such a way that DB2 first
checks for

"int(property_value) 60" before it checks for any other condition
and it therefore encounters nulls that it can't cast.

(BTW, I'm aware that the "file_id in (select file_id from
some_other_table)" condition can be expressed as a join, but it won't
solve the problem).

Am I right? HOW DO I FIX THIS?? I'm guessing it's a job for COALESCE,
but I can't get the syntax right either.

Thanks all,

Alejandrina
--#SET TERMINATOR @
CREATE PROCEDURE softintp(IN arg VARCHAR(255), OUT res INTEGER)
CONTAINS SQL DETERMINISTIC NO EXTERNAL ACTION
BEGIN
DECLARE CONTINUE HANDLER FOR SQLSTATE '22018'
BEGIN SET res = NULL; END;
SET res = INTEGER(arg);
END
@

CREATE FUNCTION softint(arg VARCHAR(255))
RETURNS INTEGER CONTAINS SQL DETERMINISTIC NO EXTERNAL ACTION
BEGIN ATOMIC
DECLARE res INTEGER;
CALL softintp(arg, res);
RETURN res;
END
@
--#SET TERMINATOR ;

db2 =VALUES softint('17');

1
-----------
17

1 record(s) selected.

db2 =VALUES softint('hello');

1
-----------
-

1 record(s) selected.

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Apr 25 '07 #2
I think following can handle almost every character string.
In other words, if VALUES INT(arg) returns value without error,
following also returns same value. And if VALUES INT(arg) returns
error, following will returns NULL.

I'm using DB2 for LUW V8.
"RTRIM(LTRIM(" may be able to be replaced by STRIP on DB2 9.

CREATE FUNCTION softint(arg VARCHAR(255))
RETURNS INTEGER
CONTAINS SQL
DETERMINISTIC
NO EXTERNAL ACTION
RETURN
CASE
WHEN SUBSTR(LTRIM(arg),1,1) NOT IN ('-','+')
AND TRANSLATE(RTRIM(LTRIM(arg)),'*',' 0123456789') = ''
OR SUBSTR(LTRIM(arg),1,1) IN ('-','+')
AND TRANSLATE(RTRIM(LTRIM(SUBSTR(LTRIM(arg),2))),'*','
0123456789') = '' THEN
INT(arg)
END;

Apr 26 '07 #3

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

Similar topics

2
by: Tom Rahav | last post by:
Hello All! I need to extract Socket object from a TcpClient object, in order to get client's IP address. I've found the following article that describes how to derive from TcpClient class and...
28
by: Tamir Khason | last post by:
Follwing the struct: public struct TpSomeMsgRep { public uint SomeId;
6
by: Anonymous | last post by:
Hello, I am loading a usercontrol with the LoadControl method and need to pass some data to the property of that control. How can I do that? THanks
1
by: John | last post by:
Hi all, I did post this about 10 hours ago thinking I would have received an answer now but it is quite urgent. How do I add a COM object to a web form? I notice there's a primary interop...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.