2017-06-07 11 views
0

Der folgende CodeVarbinäre Werte verketten und die Null-Werte ignorieren?

select hashbytes('SHA1', cast(a as varbinary(max))) FROM (select 0 a, null b) t 

kehrt 0x9069CA78E7450A285173431B3E52C5C25299E473.

Und der folgende Code gibt null zurück.

select hashbytes('SHA1', cast(a as varbinary(max)) 
    + cast(b as varbinary(max)) 
    + cast(c as varbinary(max)) 
FROM (select 0 a, null b, null c) t -- There may be many columns 

Ich versuchte, die null auf 0, aber der Hash geändert zu konvertieren.

select hashbytes('SHA1', cast(a as varbinary(max)) 
    + isnull(cast(b as varbinary(max)), 0)) 
    + isnull(cast(c as varbinary(max)), 0)) 
FROM (select 0 a, null b, null c) t 

Wie die Null varbinary Werte zu ignorieren, wenn verketten?

Antwort

0

Sie müssen den Standort von isnull() Anrufe wie folgt ändern:

select hashbytes('SHA1', cast(isnull(a, '') as varbinary(max)) + cast(isnull(b, '') as varbinary(max)) + cast(isnull(c, '') as varbinary(max))) FROM (select 0 a, null b, null c) t;

ich es in SQL Server 2012

getestet
Verwandte Themen