在SQL Server中,如何转换成二进制字符串为二进制? [英] In Sql Server, how to convert binary strings to binary?

查看:786
本文介绍了在SQL Server中,如何转换成二进制字符串为二进制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在字符串格式重新presents二进制数据的一些数据(例如'0×0002')。有一些功能或诡计,我可以将这些从文字字符串为二进制?也就是说,我想'0×0002,成为为0x0002,和SELECT CAST('0×0002'AS BINARY(20))显然不会这么做。我也拿出一些痛苦而缓慢的过程,涉及建立SQL语句,并将其分配给一个变​​量和执行的(例如EXEC(@Query)),但是我正在寻找的东西,我没有做这一点。

如果有帮助,这里要说的是,你可以在测试这个示例表:

  CREATE TABLE #T(BinaryString VARCHAR(100))
INSERT INTO #T VALUES('0x0000000000000000000000000000000000000002') - 二进制=整数2
INSERT INTO #T VALUES('0x000000000000000000000000000000000000007B') - 二进制=整数123


解决方案

旧code,SQL 7 ,sp_hexadecimal

<一个href=\"http://decipherinfosys.word$p$pss.com/2008/06/11/converting-a-hex-string-to-a-varbinary-or-vice-versa/\"相对=nofollow> SQL 2005(+ 2000也许),master.dbo.fn_varbintohexstr

<一个href=\"http://decipherinfosys.word$p$pss.com/2008/07/31/sql-server-2008-converting-binary-and-character-values/\"相对=nofollow> SQL 2008,原生

I have some data in string format that represents binary data (e.g. '0x0002'). Is there some function or trick where I can convert these from literal strings to binary? That is, I want '0x0002' to become 0x0002, and SELECT CAST('0x0002' AS BINARY(20)) obviously won't do that. I did come up with some painfully slow process that involves building up SQL Statements and assigning them to a variable and executing that (e.g. "EXEC (@Query)"), but I'm looking for something where I don't have to do that.

If it helps, here is a sample table that you can test this on:

CREATE TABLE #T (BinaryString VARCHAR(100))
INSERT INTO #T VALUES('0x0000000000000000000000000000000000000002') -- Binary = the integer 2
INSERT INTO #T VALUES('0x000000000000000000000000000000000000007B') -- Binary = the integer 123

解决方案

Older code, SQL 7, sp_hexadecimal

SQL 2005 (+ 2000 maybe), master.dbo.fn_varbintohexstr

SQL 2008, native

这篇关于在SQL Server中,如何转换成二进制字符串为二进制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆