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

What's wrong with this simple SQL UDF?

Folks,

I'm writing an SQL UDF to replace DB2's LIKE (which has some nasty limitations regarding concatenation), and I've reached a problem that I can't figure out. It must be a coding or logic problem on my part, but I just can't see it. Hopefully someone else can spot it.

The following source is a bare-bones version of the UDF to illustrate the problem.

connect to gallery2@
create function g2_like (subject varchar(1024), pattern varchar(1024)) returns integer
begin atomic

declare subject_position, pattern_position integer;

-- returns 1 if the pattern is matched
-- returns the first failing position in the pattern if the pattern is not matched

set subject_position = 1;
set pattern_position = 1;

while pattern_position <= length(pattern) do

-- -- compare the two characters at the current positions
-- if ((substr(pattern, pattern_position, 1) = '_') or
-- (substr(pattern, pattern_position, 1) = substr(subject, subject_position, 1)))
-- then -- Current two characters match (including '_' wildcard)
-- -- nop, next iteration please;
-- else -- Current two characters do not match

if substr(pattern, pattern_position, 1) = '%'
then -- but the pattern character is '%'
-- removed for simplification
else -- mismatch
return pattern_position;
end if;

-- end if;

set subject_position = subject_position + 1;
set pattern_position = pattern_position + 1;

end while;

return 1;
end@

-- should match ('%' is in column 11 of the pattern)
values g2_like ('The quick brown fox jumped over the lazy dog',
'The quick % fox jumped over the lazy dog')@
connect reset@
When I run it with the outer 'if' commented out, it runs fine, the pattern matches.

values g2_like ('The quick brown fox jumped over the lazy dog', 'The quick % fox
jumped over the lazy dog')

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

1 record(s) selected.

But when I un-comment the outer "if", it no longer matches, and it says pattern_position is 12, where it should be 11.

values g2_like ('The quick brown fox jumped over the lazy dog', 'The quick % fox
jumped over the lazy dog')

1
-----------
12

1 record(s) selected.

Can anyone offer any suggestions as to what's wrong?

Thanks.

--
--------------------
Larry Menard
"Defender of Geese and of All Things Natural"
Nov 16 '05 #1
2 1796
Larry,

Actually, the UDF is failing when the outer IF is commented out - as
how you wrote it above. The problem is that it is failing on the very
first character then, and returning 1 as the location of the mismatch,
which unfortunately matches your success condition.

If you chance the success flag to be -1, you will see this.

The reason that 12 is returning when you don't comment out the outer IF
is that the twelfth position, which doesn't match, happens to have a %
in the pattern at the 11th position, so the code decides that that is
fine, and proceeds on to the 12th position, where the space doesn't
match the 'r'.

-Chris

Nov 17 '05 #2
It always seems so simple when someone else explains it. ;-)

Thanks, Chris.

--
--------------------
Larry Menard
"Defender of Geese and of All Things Natural"
"Chris" <cc******@msn.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
Larry,

Actually, the UDF is failing when the outer IF is commented out - as
how you wrote it above. The problem is that it is failing on the very
first character then, and returning 1 as the location of the mismatch,
which unfortunately matches your success condition.

If you chance the success flag to be -1, you will see this.

The reason that 12 is returning when you don't comment out the outer IF
is that the twelfth position, which doesn't match, happens to have a %
in the pattern at the 11th position, so the code decides that that is
fine, and proceeds on to the 12th position, where the space doesn't
match the 'r'.

-Chris

Nov 17 '05 #3

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

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
1
by: learningGuy | last post by:
Can someone tell me what is wrong with this simple code? I get an exception every time at the myFile.Open() line. I have included the code that I think is needed to for you to answer this below:...
125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
46
by: Keith K | last post by:
Having developed with VB since 1992, I am now VERY interested in C#. I've written several applications with C# and I do enjoy the language. What C# Needs: There are a few things that I do...
2
by: Phil Certain | last post by:
Hi, Relative newbie to .Net but experienced with classic ASP. I am trying to create a simple business object to contain commonly used functions. This is what I have done: 1 - Created a simple...
10
by: desperateloser | last post by:
1st - This is likely pathetically simple and not at all what this site is for, being mostly a snafu using regular Acess stuff and not at all coding or scripting. Please be merciful - I'm really very...
2
by: Tarik Monem | last post by:
OK! I've gone through a few tutorials and I cannot understand what I'm doing wrong casting_registration.php <table> <tr> <td> <form enctype="multipart/form-data" action="thankyou.php"...
20
by: Daniel.C | last post by:
Hello. I just copied this code from my book with no modification : #include <stdio.h> /* count characters in input; 1st version */ main() { long nc; nc = 0;
24
by: MU | last post by:
Hello I have some code that sets a dropdownlist control with a parameter from the querystring. However, when the querystring is empty, I get an error. Here is my code: Protected Sub...
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: 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
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...
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
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,...
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...
0
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...

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.