我正在SQL Server上的,並試圖建立一個單一的鑰匙從bigint
和string
列的數據組合。爲了最小化表示爲字符串的bigint's
的大小,我使用Base 64編碼。問題是結果包括領先'A的含義base64
零,它增加了結果字段的大小。使用T-SQL
或XQuery
刪除這些領先A
的方法是什麼?SQL服務器:如何刪除領先「的base64從字符串
示例代碼:
DECLARE @binInput VARBINARY(MAX)
SET @binInput = CAST(123 AS VARBINARY(MAX))
SELECT CAST(N'' AS XML).value('xs:base64Binary(sql:variable("@binInput"))', 'varchar(max)')
我有一個導致AAAAew==
,我更願意看到的只是ew
的想法,因爲是使最終的字符串越短,可以和的base64字符串應該比base10更短。
更新1:如由理查德博伊斯建議我試圖BIGINT轉換爲等於串,但它給空爲base64轉換的結果
declare @input bigint
declare @varInput nvarchar(max)
set @input = 123
set @varInput = cast(cast(@input as varbinary(max)) as varchar(max))
select CAST(N'' AS xml).value('xs:base64Binary(sql:variable("@varInput"))', 'varchar(max)')
更新2:當前解決方案是得到一個base64二進制字符串,並刪除前導'A和尾部'='s。這並不完美,所以我們歡迎任何建議。實際代碼:
declare @input bigint
set @input = 1234567890
declare @output varchar(max)
set @output = (select cast(@input as varbinary(max)) for xml path(''),binary base64)
set @output = replace(ltrim(replace(@output,'A',' ')),' ','A') -- remove leading 'A's
set @output = replace(@output,'=','') -- remove trailing '='s
select @output
變化「CAST(CAST(@input爲varbinary(MAX))爲varchar(最大值) )「to」cast(cast(@input as varchar(max))as varbinary(max))「 –
結果仍然爲'null'。問題是,當123被轉換爲一個字符串時,需要三個字符來表示結果,因爲它基於10。當將其轉換爲base64時,它只需要兩個字符。所以它應該這樣轉換:bigint-> binary-> base64字符串 –