0

I have the following code

select 
    Name, UniqueId, 
    checksum(Name) as CheckName, 
    checksum(UniqueId) as CheckId
from 
    DataManagementPipeline.dbo.pod_1801_energex_vegetation_zones 
where 
   Name <> UniqueId 

The results are the following

Name        UniqueId        CheckName      CheckId
********************************************************
VZ-4820/73  VZ-4820/73     -1880307869     -21513965
VZ-400706   VZ-400706       591267130      536293334

The values are the same (white space and all) yet they are appearing as different and interestingly the checksums are different. Is it an encoding issue as to why they are different? Any ideas?

5
  • I think datatype for " Name, UniqueId," is nvarchar. Commented Oct 14, 2016 at 4:49
  • The datatypes are both varchar. Name is varchar(max) and UniqueId is varchar(100). But both strings are obviously less that this so it should not make a difference. Commented Oct 14, 2016 at 4:50
  • If this condition "Name <> UniqueId" satisfies values will not be retrieved as of my knowledge...how can u generate output for this Commented Oct 14, 2016 at 4:52
  • That is the mystery. They are the same values in notepad++, etc. They look the same, yet they satisfy <> . The checksum shows something is strange but I can not put my finger on it. Commented Oct 14, 2016 at 4:55
  • 1
    If both datatype are same ,the values retrieved in output will be same Name UniqueId CheckName CheckId VZ-4820/73 VZ-4820/73 1347317154 1347317154 VZ-400706 VZ-400706 1426899811 1426899811 Commented Oct 14, 2016 at 4:58

2 Answers 2

2

CHECKSUM will return different values, if the types are different. See more at MSDN Checksum. I think in your case, Name & UniqueId are of different types. Please see the example code below

CREATE TABLE test(origname varchar(36), uniqueid nvarchar(36))

INSERT INTO test(origname,uniqueid)
values ('venkat',N'venkat')

SELECT CHECKSUM(origname), CHECKSUM(uniqueid) FROM test

-- Returned Values 
178987073   1792344567
Sign up to request clarification or add additional context in comments.

6 Comments

Name and UniqueId are varchar(max) and varchar(100) respectively.
I have tried with ur case as"varchar(max) and varchar(100) ". I can able to get the output where as vales are same
The fact that they are both varchar and the same size should not make them unequal. So the mystery continues.
@Raja Yuvi are you saying that they satisfy <> and are thus returned or they do not satisfy <>?
@EmptyWaterHoles if " Name <> UniqueId" values should not be retrieved , if " Name = UniqueId" the values retrieved will/must be same.
|
2

The mystery solved is that the spacing on the end of UniqueId was ASCII character(10) and the not the white space I thought it was. Hence why they were coming up as not equal. So i added this code

ltrim(rtrim(Name)) <> rtrim(ltrim(REPLACE(REPLACE(UniqueId, CHAR(13), ''), CHAR(10), ''))) 

In notepad++ I thought I had added the ASCII(10)(linefeed) but apparently not. I will accept the answer by @Venkataraman R as he is correct. Thanks for your help.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.