001priyank
001priyank

Reputation: 517

Difference in the checksum SQL statements

What is the difference between the 2 different statements below? Please explain the output.

 SELECT CHECKSUM(CONVERT(NVARCHAR,30))

Result : 51136012

 DECLARE @AA NVARCHAR
SET @AA= CONVERT(NVARCHAR,30)
SELECT CHECKSUM(@AA)

Result: 38

Upvotes: 0

Views: 173

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269445

The default lengths are different. In the first, it is something like 32. But when you do:

 declare @aa nvarchar;

The default length is 1. So, the second is only using the first character.

In SQL Server, always use lengths with the varchar() types:

SELECT CHECKSUM(CONVERT(NVARCHAR(255), 30))

DECLARE @AA NVARCHAR(255);
SET @AA= CONVERT(NVARCHAR(255), 30);
SELECT CHECKSUM(@AA);

Upvotes: 2

Related Questions