Converting from integer to binary and back in SQL Server(在 SQL Server 中从整数转换为二进制并返回)
问题描述
我整个上午都在用这个头撞墙.
I've been banging my head against the wall with this one all morning.
以下 SQL 代码及其结果对我来说毫无意义:
The following SQL code and its' result makes no sense to me:
select CONVERT(INT, CONVERT(BINARY(30),2691485888))
导致:
-1060082528
什么?为什么结果不等于我原来的整数?
What? Why doesn't the result equal my original integer?
我的整个目标是将一个整数转换为字节并将这些字节存储到数据库中,但是没有让这个基本示例工作,我被卡住了.谁能解释一下我做错了什么?
My whole objective is to convert an integer into bytes and store those bytes into the database, but without getting this basic example to work I am stuck. Can anyone explain what I'm doing wrong?
顺便说一下,我使用的是 Sql Server 2005 (9.0.4340)
By the way, I am using Sql Server 2005 (9.0.4340)
推荐答案
正如我在之前的评论中所指出的,2,691,485,888 比 INT 可以容纳的要大.
As I noted in my earlier comment, 2,691,485,888 is larger than what an INT can hold.
这会起作用:
select CONVERT(BIGINT, CONVERT(BINARY(30), CONVERT(BIGINT, 2691485888)))
这篇关于在 SQL Server 中从整数转换为二进制并返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 SQL Server 中从整数转换为二进制并返回
基础教程推荐
- MySQL 中的类型:BigInt(20) 与 Int(20) 2021-01-01
- mysql选择动态行值作为列名,另一列作为值 2021-01-01
- 如何在 SQL 中将 Float 转换为 Varchar 2021-01-01
- oracle区分大小写的原因? 2021-01-01
- 什么是 orradiag_<user>文件夹? 2022-01-01
- 二进制文件到 SQL 数据库 Apache Camel 2021-01-01
- 在 MySQL 中:如何将表名作为存储过程和/或函数参数传递? 2021-01-01
- 表 './mysql/proc' 被标记为崩溃,应该修复 2022-01-01
- 如何根据该 XML 中的值更新 SQL 中的 XML 2021-01-01
- 在多列上分布任意行 2021-01-01
