假設num
實際上是十六進制數字的字符串表示,我認爲你可以用一對夫婦的用戶定義函數的將其轉換爲整數:
-- Based on Feodor's solution on
-- http://blog.sqlauthority.com/2010/02/01/sql-server-question-how-to-convert-hex-to-decimal/
CREATE FUNCTION fn_HexToInt(@str varchar(16))
RETURNS BIGINT AS BEGIN
SELECT @str=upper(@str)
DECLARE @i int, @len int, @char char(1), @output bigint
SELECT @len=len(@str),@[email protected], @output=case WHEN @len>0 THEN 0 END
WHILE (@i>0)
BEGIN
SELECT @char=substring(@str,@i,1)
, @[email protected]
+(ASCII(@char)
-(case when @char between 'A' and 'F' then 55
else case when @char between '0' and '9' then 48 end
end))
*power(16.,@[email protected])
, @[email protected]
END
RETURN @output
END
-- Example conversion back to hex string - not very tested
CREATE FUNCTION fn_IntToHex(@num int)
RETURNS VARCHAR(16) AS BEGIN
DECLARE @output varchar(16), @rem int
SELECT @output = '', @rem=0
WHILE (@num > 0)
BEGIN
SELECT @rem = @num % 16
SELECT @num = @num/16
SELECT @output = char(@rem + case when @rem between 0 and 9 then 48 else 55 end) + @output
END
RETURN @output
END
select dbo.fn_HexToInt ('7FFF') -- = 32767
select dbo.fn_IntToHex(32767) -- = 7FFF
所以你可以嘗試
UPDATE myTable
SET num = dbo.fn_IntToHex(dbo.fn_HexToInt(num) + 4000)
爲什麼你存儲顯然意味着數值爲* strings *的值?如果它們應該以用戶的十六進制格式顯示/輸入,應該通過遠離數據庫格式化代碼來完成。 –
@Damien_The_Unbeliever - 我認爲這是爲了阻止數字變得難以置信。無論哪種方式,這是現在的方式,我必須使用它... – froadie