简体   繁体   中英

How to convert nvarchar to varbinary accurately

I have a table which has column [password] stored as nvarchar(max) . I want to convert it into varbinary(max) .

I created a new column called [temp] and declared it as varbinary(max) . Then I updated using CONVERT :

update tempuser set [temp]=CONVERT(varbinary(max), CONVERT(nvarchar(max),[password]))

Now in the [temp] column the value is different. For example, one value for [password] started with this:

0x3E6AFF88...

The corresponding entry in [temp] starts with:

0x30783345...

Also when [password] is NULL , [temp] becomes 0x4E554C4C .

You're converting a string that happens to look like a binary value because it starts with 0x . Unfortunately these are not the same thing, and in order for SQL Server to understand that you want to interpret the string as an actual binary value to convert directly, instead of a string to convert to its binary representation, you need to use a style parameter:

UPDATE dbo.tempuser -- please always use the schema prefix
  SET [temp] = CONVERT(VARBINARY(MAX), [password], 1); -- and semi-colons

Note that converting an NVARCHAR(MAX) to NVARCHAR(MAX) is unnecessary.

Now, it is possible that because you chose NVARCHAR(MAX) for some reason, that the column contains garbage that can't be converted, so you may encounter this error:

Msg 8114, Level 16, State 5
Error converting data type nvarchar to varbinary.

In that case, you'll need to find the values that don't start with 0x (or otherwise contain ineligible characters) and fix them.

It seems that your password field isn't actually a varbinary, despite looking like one.

Run these for an example:

SELECT CAST('0x3E6AFF88BEB29D9B234B9E5A5AD329D8D3F33200EC4EE02749C56AD58D040976' AS varbinary(max))
SELECT CAST(CAST('0x3E6AFF88BEB29D9B234B9E5A5AD329D8D3F33200EC4EE02749C56AD58D040976' AS varbinary(max))AS VARCHAR(MAX))

SELECT CAST('dog' AS VARBINARY(MAX))
SELECT CAST(CAST('dog' AS VARBINARY(max))AS VARCHAR(MAX))

You see that your result is the equivalent of your initial string, just in varbinary format.

Just as Hart Co's answer shown , use the following code to convert passwords to hex numbers.

UPDATE dbo.tempuser SET [temp] = CONVERT(VARBINARY(MAX), [password], 1);

If you got

Msg 8114, Level 16, State 5
Error converting data type nvarchar to varbinary.

That means your have one or multiple password contains are a valid hex string. One of reason that is easy to ignore is you have odd number of characters. You can use the following code to check which row causing the error:

SELECT id, TRY_CONVERT(VARBINARY(MAX), [password], 1) AS try_convert
FROM tempuser
WHERE try_convert IS NULL

Instead of return an error, TRY_CONVERT will return NULL if it find something cannot be converted.

If you got rows contains odd number of characters, you can use RIGHT function to left padding zeros to let them valid.

CONVERT(
    varbinary(MAX),
    RIGHT('000000000000'+SUBSTRING([password], 3, LEN([reg_address])), 16),
    2
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM