在 SQL Server 中从整数转换为二进制并返回

2021-09-10 00:00:00 sql tsql 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)))

相关文章