問題描述
我整個上午都在用這個頭撞墻.
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 中從整數轉換為二進制并返回的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!