Forum Discussion
HASHBYTES encoding differences
HI all, this is driving me crackers.
I am using the following to create a changeid for a SQL merge statement (ok this may be an old approach and I'm not looking to alter it at this time).
SELECT
HASHBYTES
('md5',
(
SELECT CONCAT_WS
(
'|'
,ISNULL(Col1,0)
,ISNULL(Col2,0)
,ISNULL(Col3,'')
,ISNULL(Col4,'1900-01-01')
,ISNULL(Col5,0)
)
)
) AS ChangeID_New
,MC.*
FROM dbo.MasterTable_changeid MC
I run this type of thing daily in many places in the data warehouse via Visual Studio / SSIS and have no issue. However today, I have added a column to a table and I want to recalculate the ChangeID with the new column and update the table via a SQL update statement rather than running the SSIS package.
Why when I run the HASHBYTE statement, above, in a SELECT it returns the values I'd expect and if I run the same statement in an UPDATE it returns special characters? See attached screen shot of the results... ChangeID_New is what I am expecting and is the result of the SELECT statement, but ChangeID is the result of me running it as an UPDATE statement. I hope this makes sense. I'm guessing there is something to do with ASCII and Unicode, but I don't understand why.