SQL Server 将 varbinary(16) 转换为二进制文本
类似于 这个问题(Sql Server 将整数转换为二进制字符串,但是我想将 varbinary(16) 转换为其文本版本,
因为我的代码结果证明了我正在做
create function GetGuidBinaryString (@value varbinary(16))
returns varchar(128)
as
begin
declare @vsresult varchar(128)
declare @inti int
select @inti = 128, @vsresult = ''
while @inti>0
begin
select @vsresult=convert(char(1), @value % 2)+@vsresult
select @value = convert(int, (@value / 2)), @inti=@inti-1
end
return @vsresult
end
create table #values (binvalue varchar(128))
delete from #values
declare @intcount int
select @intcount = 0
while @intcount < 100
begin
insert into #values select dbo.GetGuidBinaryString(convert(varbinary(16),convert(bigint,2147483640) + @intcount))
select @intcount = @intcount+1
end
select * from #values
一些隐式转换,因为该函数只能正确工作。正整数。
Similar to this question (Sql Server convert integer to binary string, but I would like to convert a varbinary(16) to its text version instead.
I am doing something tragically wrong as the results of my code will attest.
create function GetGuidBinaryString (@value varbinary(16))
returns varchar(128)
as
begin
declare @vsresult varchar(128)
declare @inti int
select @inti = 128, @vsresult = ''
while @inti>0
begin
select @vsresult=convert(char(1), @value % 2)+@vsresult
select @value = convert(int, (@value / 2)), @inti=@inti-1
end
return @vsresult
end
create table #values (binvalue varchar(128))
delete from #values
declare @intcount int
select @intcount = 0
while @intcount < 100
begin
insert into #values select dbo.GetGuidBinaryString(convert(varbinary(16),convert(bigint,2147483640) + @intcount))
select @intcount = @intcount+1
end
select * from #values
Perhaps there is some implicit conversion I am doing in the function as the function only works correctly for positive integers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
@value % 2
和@value / 2
正在进行隐式转换。select @value = Convert(int, (@value / 2))
正在显式转换为 int,因此在这里您将获得除法之后存储在 varbinary(16) 中的值的负 int转换为 bigint 大于 2,147,483,647。% 表示负整数,结果为 -1。
我认为不可能使用 % 和 / 将 varbinary(16) 转换为二进制。它们仅适用于 int/bigint 等。
这是一个适用于正 bigint 值的转换例程。我不知道您期望负 bigint 值的表示形式是什么。
在函数调用中将 varbinary(16) 字段转换为 bigint,也许它会执行您想要的操作。
我确信它不适用于您可以存储在 varbinary(16) 字段中的所有可能值。
@value % 2
and@value / 2
is doing an implicit conversion.select @value = convert(int, (@value / 2))
is doing an explicit conversion to int so here you are getting a negative int for the values stored in varbinary(16) that after the division converted to bigint is larger than 2,147,483,647.% for a negative int will give you a -1.
I do not think it is possible to convert varbinary(16) to binary using % and /. They only work on int/bigint etc.
Here is a conversion routine that works for positive bigint values. I do not know what representation you would expect for negative bigint values.
Convert your varbinary(16) field to bigint in the call to the function and perhaps it does what you want.
I am sure it does not work for all possible values that you can store in a varbinary(16) field.