. Comparison of image type data in SQL Server

Source: Internet
Author: User

Original:. Comparison of image type data in SQL Server

In SQL Server, if you are comparing data for text, ntext, or image data types. You will be prompted:the text, ntext, and image data types cannot be compared or sorted unless the is NULL or Like operator is used. However, image does not support like comparisons.
So how do you compare the pictures in the database?
For the processing of this large object, there are special function Dbms_lob.compare in Oracle, and there is no special processing function in SQL Server.
You can only intercept data from image data by using the SUBSTRING function for a period of time, up to a maximum of 8060 bytes (8k),
The varbinary type data is then compared. Here is an example of a function that compares image: Note: the type of the local variable is any system-provided or user-defined data type. The variable cannot be a text, ntext, or image data type, please use it directly in the query.
/** * * * object:userdefinedfunction [dbo].    [Funccompareimage] Script date:2016-09-01 11:18:53 * * * * **/SETAnsi_nulls onGOSETQuoted_identifier onGOALTER FUNCTION [dbo].[Funccompareimage](@a1 IMAGE,@a2 IMAGE )RETURNS INT--if match, return 1 as    BEGIN        DECLARE @n INT ,            @i INT ,            @j INT; DECLARE @b1 VARBINARY(8000) ,            @b2 VARBINARY(8000); SET @n = 1; IF datalength(@a1)<> datalength(@a2)--different length            SET @n = 0; ELSE            BEGIN                SET @i = 0; SET @j =(datalength(@a1)- 1)/ 8000 + 1;  while @i <= @j                    BEGIN                        SET @b1 = SUBSTRING(@a1,@i * 8000 + 1,                                             Case @i                                               when @j                                               Then datalength(@a1)% 8000                                              ELSE 8000                                            END); SET @b2 = SUBSTRING(@a2,@i * 8000 + 1,                                             Case @i                                               when @j                                               Then datalength(@a2)% 8000                                              ELSE 8000                                            END); IF @b1 <> @b2                            BEGIN                                SET @n = 0;  Break; END; SET @i = @i + 1; END; END; RETURN(@n); END;


. Comparison of image type data in SQL Server

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.