Sql server 2008 中varbinary查询

时间:2023-03-08 17:02:10
Sql server 2008 中varbinary查询

sqlserver2008中遇到一个坑爹的问题,使用以下语句添加的数据

insert into testtable (
username,
password,
productcode
)
select 'testname',
ENCRYPTBYPASSPHRASE(
'test passphrase',
'testpassword'
), 'productcode'

竟然不能用以下查询语句解出

SELECT t.id as id, t.username as username, t.password as password, t.productcode as productcode FROM (
SELECT id, username,
CAST(DECRYPTBYPASSPHRASE( 'test passphrase', password ) as varchar(20))
as password, productcode
FROM testtable
) t
WHERE t.username='testname'

而当使用了nvarchar时却能够成功做到

SELECT t.id as id, t.username as username, t.password as password, t.productcode as productcode FROM (
SELECT id, username,
CAST(DECRYPTBYPASSPHRASE( 'test passphrase', password ) as nvarchar(20))
as password, productcode
FROM testtable
) t
WHERE t.username='testname'