Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL Server将varchar字段与二进制文件(16)进行比较_Sql_Sql Server_Sql Server 2005 - Fatal编程技术网

SQL Server将varchar字段与二进制文件(16)进行比较

SQL Server将varchar字段与二进制文件(16)进行比较,sql,sql-server,sql-server-2005,Sql,Sql Server,Sql Server 2005,我有两个表-表A的主键列类型为binary(16),另一个表B的外键引用同一列,但数据类型为varchar(50)。因此,表A的值类似于0x0007914BFFEC4603A600045492EFA1a,表B的值存储为0007914BFFEC4603A600045492EFA1a 我如何比较这两列,这将给我 0007914BFFEC4603A6900045492EFA1A = 0x0007914BFFEC4603A6900045492EFA1A 您需要将二进制(16)转换为字符串。如何做到这一

我有两个表-表A的主键列类型为binary(16),另一个表B的外键引用同一列,但数据类型为varchar(50)。因此,表A的值类似于
0x0007914BFFEC4603A600045492EFA1a
,表B的值存储为
0007914BFFEC4603A600045492EFA1a

我如何比较这两列,这将给我

0007914BFFEC4603A6900045492EFA1A = 0x0007914BFFEC4603A6900045492EFA1A

您需要将二进制(16)转换为字符串。如何做到这一点的示例可以在下面的问题中找到。此问题将varbinary转换为字符串,但同样的技术可用于二进制列或变量:

下面是如何执行此操作的示例代码:

declare @bin binary(16), @str varchar(50)
set @bin = 0x0007914BFFEC4603A6900045492EFA1A
set @str = '0007914BFFEC4603A6900045492EFA1A'

select @bin as'binary(16)', @str as 'varchar(50)'

-- the binary value is not equal to the string value
-- this statement returns 'binary value is not equal to string'
if @bin = @str select 'binary value is equal to string'
else select 'binary value is not equal to string'

declare @binstr varchar(50)
select @binstr = convert(varchar(50), @bin, 2)
select @binstr

-- the converted string value matches the other string
-- the result of this statement is "converted string is equal"
if @binstr = @str select 'converted string is equal'
else select 'converted string is NOT equal'
要在联接中使用此选项,可以在内部联接的
ON
子句或
WHERE
子句中包含转换:

select *
from TableA
inner join TableB
    on TableB.char_fk = convert(varchar(50), TableA.bin_pk, 2)
更新

对于SQL Server 2005,您可以使用XML方法:


您也可以使用未记录的函数sys.fn\u varbintohextr,但是,您应该避免使用它的原因有很多

使用样式2转换
,以获得十六进制字符串的二进制表示形式

... where TableA.bin_pk = CONVERT(VARBINARY, TableB.char_fk, 2) 

正确的做法是在同一数据类型中设置两个字段。要执行此操作,请创建一个新表,例如temp并使用select into和convert:

select field1,...,convert(varchar(50),varbinary(16),fieldToConvert)...,fieldN

into myNewTable

找到了答案。我需要使用

master.dbo.fn_varbintohexstr (@source) 

它将一个varbinary转换为varchar,然后在我的场景中很好地进行比较。

添加的sql-server-2005标记可能重复,这解释了为什么不起作用这不起作用,我需要将binary转换为varchar,或者将varchar转换为abel,以等同于2-0x0007914bfec4603a6900045492efa1a和“0007914bfec4603a6900045492efa1a”。这些是我在上面的代码中使用的示例值。代码将二进制文件转换为varchar进行比较。代码不起作用了,或者你还需要其他东西吗?我提供了一个如何在连接中进行转换的示例。我很困惑。当我运行这段代码时,它给出了完全相同的值,第二次比较返回“converted string is equal”。我用一个SQL 2005解决方案更新了我的答案,该解决方案使用XML和指向一些原因的链接,以避免使用未记录的函数
sys.fn_varbintohextr
.CONVERT(VARBINARY,'0007914bfec4603a6900045492efa1a',2)给出0x303030373931344246464543343630334136393030303435343932454641
master.dbo.fn_varbintohexstr (@source)