您的位置:首页 > 博客中心 > 数据库 >

T-SQL编写程序,将十进制数值转换为二进制字符串后输出

时间:2022-03-14 02:12

declare @ch nchar(10),@n int,@a int
set @n=20
set @ch=''
print cast(@n as varchar(5))+'的二进制为:'
while @n<>0
begin
	set @a=@n%2
	set @n=@n/2
	set @ch=char(48+@a)+@ch
end
print @ch

结果为:

20的二进制为:
10100    

热门排行

今日推荐

热门手游