Skip to main content
Inspiring
April 15, 2008
解決済み

SQL LIKE statement to text field fomr CF MX 6.1

  • April 15, 2008
  • 返信数 4.
  • 1262 ビュー
Hi.

Here's my scenario. I am running SQL Server 2000 SP4 on a Windows 2003 Server running IIS 6. Using ColdFusion MX 6.1 Updater version.

I am storing HTML page code (some complete, some just clips) in a text type field.

I need to check to see if someone has already saved that exact code already before. So I do the following query:

<cfquery name="checkforexisting" datasource="mydatasource">
SELECT smallfieldtogetareturn
FROM MyTable
WHERE MyTextField LIKE '%#mystringoflongdata#%'
</cfquery>

Then to see if it posted:

<cfif checkforexisting.RecordCount GT 0>
do the don't clip optional processing
</cfif>

What is killing me is that it finds the match SOMETIMES. Just not ALL the time. I have tried no percent signs when passing the ColdFusion variable, only one on the right (which has solved these types of problems before).

If I pull the record back OUT of the database in a page by itself (retrieving it by key field), it finds every other copy of the code EVERY time.

I have put this into a stored procedure, which seemed to help (?).

I even have resorted to creating a temp table first with the clip, then running a query to retrieve the record that was just put in, and comparing THAT to my primary table (both have text type fields). The reason for this, is that if I write a single page that has just the retrieval of the temp record, and that compare is run, it finds EVERY copy of it in the primary table. I also thought that maybe there was a strong problem (I had tried HTMLEditFormat, HTMLCodeFormat, JavaCast since it was coming from a java program on the browser end).

I am not terribly familiar with using large text fields, or dealing with these large pieces of HTML code as something to compare!

- Mike
このトピックへの返信は締め切られました。
解決に役立った回答 Dan_Bracuk
The sql works the same no matter how large the string is. The part between the percentage sign has to be matched exactly. If you are only getting a match sometimes, then something is different with your test string. It could be a capital letter somewhere, or some extra whitespace.

I rarely work with SQL Server so I don't know it that well. However, I'm pretty sure it has upper and lower functions. That will help you with the capital letter problem.

The whitespace problem is harder.

返信数 4

MichaelSJudd作成者
Inspiring
April 18, 2008
Thanks Dan. The whitespace problem is definitely a headache, but I tried the character pseudo-hash, removed the whitespace, and voila!

Thanks as always.

- Mike
MichaelSJudd作成者
Inspiring
April 18, 2008
Thanks Dan. I thought that was the case (about the length).

I will try the cfprocessingdirective tag for whitespace and see if that works.

Then I am going to try a sort of hash - I'll pull the first 1000 chars, then the last, and put them in a table. If there's a match, then at least I can compare to a single record.

I will post back shortly.

- Mike
Inspiring
April 18, 2008
MichaelSJudd wrote:

As Dan indicated your most likely issue is slight differences in
capitalization and|or white space or other 'invisible' difference
between your strings. One can have nearly infinite variety in HTML code
that will display identically in a browser, the standard is very
forgiving of formatting differences.

Something I have done in the past to compare HTML content is to use the
hash() function. It will not help you eliminate the differences but it
would show when something is different then it appears to be.

#hash(mystringoflongdata)# will return a hexadecimal number representing
the string. Two *exactly* the same strings will produce the same
number. Then it is very easy to compare these numbers to see if two
long strings are the same.

To use this with your database data, you would probably have to store
the hash value of the string in a field when you store the HTML data.

HTH
Ian
Dan_Bracuk解決!
Inspiring
April 18, 2008
The sql works the same no matter how large the string is. The part between the percentage sign has to be matched exactly. If you are only getting a match sometimes, then something is different with your test string. It could be a capital letter somewhere, or some extra whitespace.

I rarely work with SQL Server so I don't know it that well. However, I'm pretty sure it has upper and lower functions. That will help you with the capital letter problem.

The whitespace problem is harder.