2012-03-28 122 views
1

可以在sql server中將varchar(32)(十六進制字符串,如0x81f2cf442269e111b99c1cc1deedf59c)轉換爲bigint嗎?將varchar(32)轉換爲bigint

我已經試過這樣:

select convert(bigint, convert (varbinary(16), '0x81f2cf442269e111b99c1cc1deedf59c', 1)) 

,但我不知道,它的作品具有更高和更低的數值。

+0

爲什麼你需要這樣的東西? – 2012-03-28 09:10:54

+0

將此字符串存儲在bigint列中 – malinois 2012-03-28 09:11:47

+0

我認爲這些帖子可能會幫助您http://blog.sqlauthority.com/2010/02/01/sql-server-question-how-to-convert-hex-to-decimal /和http://dpatrickcaldwell.blogspot.com/2009/05/converting-hexadecimal-or-binary-to.html – 2012-03-28 09:16:09

回答

4

是可能的一個varchar(32)(十六進制字符串等 0x81f2cf442269e111b99c1cc1deedf59c)轉換爲SQL Server中的BIGINT?

2個答案與範例。兩者根本錯誤。

不能做。任何人都在意第一次運行基本的數學檢查?

32十六進制= 16字節。 比特:8字節。

你所有的代碼是一回事 - 沒用。你不能將16字節的32位十六進制字符串轉換爲8字節的數字。僅在極少數情況下(高8字節全0)。

0

我希望你能用下面的方式去,請你試試這個。

DECLARE @HexValue Varchar(32) 
SET @HexValue = '0x81f2cf442269e111b99c1cc1deedf59c' 

Declare @Query NVARCHAR(100) 
Declare @Parameters NVARCHAR(50) 
Declare @ReturnValue BIGINT 

SET @Query = N'Select @Result = Convert(BIGINT,' + @HexValue + ')' 
SET @Parameters = N'@Result BIGINT output' 
EXEC MASTER.dbo.Sp_executesql @Query, @Parameters, @ReturnValue OUTPUT 

SELECT @ReturnValue 

感謝您的時間。