In Sql Server, how to convert binary strings to binary?(在Sql Server中,如何将二进制字符串转换为二进制?)
问题描述
我有一些字符串格式的数据代表二进制数据(例如'0x0002').是否有一些功能或技巧可以将它们从文字字符串转换为二进制?也就是说,我希望 '0x0002' 变为 0x0002,而 SELECT CAST('0x0002' AS BINARY(20)) 显然不会那样做.我确实提出了一些非常缓慢的过程,包括构建 SQL 语句并将它们分配给一个变量并执行它(例如EXEC(@Query)"),但我正在寻找一些我不需要做的事情那个.
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
推荐答案
旧代码,SQL 7,sp_hexadecimal
SQL 2005(可能 + 2000),master.dbo.fn_varbintohexstr
SQL 2008,原生
这篇关于在Sql Server中,如何将二进制字符串转换为二进制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在Sql Server中,如何将二进制字符串转换为二进制?
基础教程推荐
- 从字符串 TSQL 中获取数字 2021-01-01
- 带更新的 sqlite CTE 2022-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- 带有WHERE子句的LAG()函数 2022-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
