Click here to Skip to main content
15,886,007 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to compare the Image which already inserted in the Image Data Type column and the Image Object which passed through the SQL Stored Procedure, Is it possible to compare through SQL script?

What I have tried:

I tried with the below script, but it is not working

create table foo
(
img image
)
go

select *
from foo a, foo b
WHERE convert(varbinary,a.img) = convert(varbinary,b.img)
Posted
Updated 17-May-18 8:26am
Comments
RedDk 17-May-18 13:57pm    
Sure (good idea, and one that's on my work hit-list ...).

You could use RIGHT, LEFT, MIDDLE, etc ... any STRING comparitor ... after converting the SELECT return to proper type. You ARE talking "compare" for analysis or "find-in-str" type of processing, yes?

Use CONVERT or CAST. See BOL for details.
MadMyche 17-May-18 15:31pm    
I;d be inclined to add another column for a CheckSum; and have the insert function also insert the files checksum.
DB Server will use less grunt to run the query then, because you can just a pair of much shorter strings

1 solution

You haven't specified a length in your CONVERT call. According to the documentation[^], the data will be truncated to the first 30 bytes.

Specify max as the length, and the comparison should work:
SQL
SELECT *
FROM foo As a INNER JOIN foo As b
ON CONVERT(varbinary(max), a.img) = CONVERT(varbinary(max), b.img);

If it doesn't, then you'll need to explain what the problem is.

NB: The records will only match if the bytes are exactly the same. If a single byte is different, the images are considered different, even if they look the same.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900