如何在 SQL Server 中的 where 子句中比较 varbinary
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6648524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to compare varbinary in where clause in SQL Server
提问by Naresh Goradara
I want to compare varbinary
type with byte array. I have tried so far:
我想将varbinary
类型与字节数组进行比较。到目前为止我已经尝试过:
DECLARE @data AS NVARCHAR(MAX)='4283'
Select * from table1 Where bindayData=CAST(@data AS VARBINARY)
But this does not work.
但这不起作用。
I note one strange behaviour of this: when I statically use it like
我注意到一个奇怪的行为:当我静态使用它时
Select * from table1 Where bindayData=CAST('4283' AS VARBINARY)
then it works fine. But when I declare a variable, it doesn't work.
然后它工作正常。但是当我声明一个变量时,它不起作用。
Please share your ideas.
请分享您的想法。
Thanks, Naresh Goradara
谢谢,纳雷什·戈拉达拉
回答by gbn
Try
尝试
DECLARE @data AS NVARCHAR(MAX)='4283'
The string constant '4283'
is non-unicode in the CAST, one byte per character.
This gives 4 bytes varbinary 0x34323833
字符串常量'4283'
在 CAST 中是非 unicode 的,每个字符一个字节。
这给出了 4 个字节的 varbinary0x34323833
When you use NVARCHAR(MAX), then it changed to unicode N'4283'
string with 2 bytes per character.
This gives 8 bytes varbinary, something like 0x0034003200380033
当您使用 NVARCHAR(MAX) 时,它会更改为N'4283'
每个字符 2 个字节的unicode字符串。
这给出了 8 个字节的 varbinary,类似于 0x0034003200380033