Quick one today:
Recently, ran into this error, about an error with implicit conversion.
Msg 402, Level 16, State 1, Line 45 The data types nvarchar and varbinary are incompatible in the add operator.
This is the query:
-- -- Generate script to CREATE/migrate Sql logins -- SELECT 'CREATE LOGIN [' + L.name + '] WITH PASSWORD = ' + L.password_hash + ' HASHED, sid = ' + L.sid + ', CHECK_EXPIRATION = ' + CASE WHEN L.is_expiration_checked = 0 THEN 'OFF, ' ELSE 'ON, ' END + 'CHECK_POLICY = ' + CASE WHEN L.is_policy_checked = 0 THEN ' OFF, ' ELSE 'ON, ' END + 'DEFAULT_DATABASE = [' + L.default_database_name + '], ' + 'DEFAULT_LANGUAGE = [' + L.default_language_name + ']' FROM sys.sql_logins AS L WHERE type = 'S' -- SqlLogin ORDER BY L.name ASC GO
On the surface it seems fairly easy to fix, right? Just find the VARBINARY literal in the query and make the conversion EXPLICIT using a CONVERT() function.
In this case, it is ‘L.password_hash‘ & ‘L.sid‘.
As indicated in BoL, password_hash is a VARBINARY(256) dataype and sid is a VARBINARY(85). To concatenate VARBINARY values (represented in hex), with the rest of the query, written in VARCHAR, we need to use an an undocument function called: fn_varbintohexstr.
Here our goal is to keep the hex representation intact (visual); and just convert the binary to VARCHAR, so they could be concatenated with the rest of the SQL script (written in VARCHAR). We do not want to convert the contents into VARCHAR, just the format to VARCHAR.
-- -- Generate script to CREATE/migrate Sql logins -- SELECT 'CREATE LOGIN [' + L.name + '] WITH PASSWORD = ' + master.sys.fn_varbintohexstr(L.password_hash) + ' HASHED, sid = ' + master.sys.fn_varbintohexstr(L.sid) + ', CHECK_EXPIRATION = ' + CASE WHEN L.is_expiration_checked = 0 THEN 'OFF, ' ELSE 'ON, ' END + 'CHECK_POLICY = ' + CASE WHEN L.is_policy_checked = 0 THEN ' OFF, ' ELSE 'ON, ' END + 'DEFAULT_DATABASE = [' + L.default_database_name + '], ' + 'DEFAULT_LANGUAGE = [' + L.default_language_name + ']' FROM sys.sql_logins AS L WHERE type = 'S' -- SqlLogin ORDER BY L.name ASC GO
With this function, we are able to convert the contents of VARBINARY variable/column (represented in hexadecimal) to VARCHAR (again represented in hex), so we could generate a proper SQL script.
_Sqltimes