十进制整型转二进制字符串(锁定字符串长的最小值)
1
set ANSI_NULLS ON
2
set QUOTED_IDENTIFIER ON
3
go
4
5
-- =============================================
6
-- Author: HuntFox
7
-- Create date: 2005.11.23
8
-- Description: 十进制整型转二进制字符串(锁定字符串长的最小值)
9
-- =============================================
10
ALTER FUNCTION [dbo].[ToBinarySystemString]
11
(
12
@DecimalSystemInt int,
13
@BinarySystemStringLen int
14
)
15
RETURNS NVARCHAR(31)
16
AS
17
BEGIN
18
-- Declare the return variable here
19
declare @ResultVar varchar(31)
20
set @ResultVar=''
21
while (@DecimalSystemInt<>0)
22
begin
23
set @ResultVar=@ResultVar+convert(char(1),@DecimalSystemInt%2)
24
set @DecimalSystemInt=@DecimalSystemInt/2
25
end
26
set @ResultVar=REVERSE (@ResultVar)
27
while (Len(@ResultVar)<@BinarySystemStringLen)
28
begin
29
set @ResultVar='0'+@ResultVar
30
end
31
RETURN @ResultVar
32
END
33
34
35
36
37
38
39
40
set ANSI_NULLS ON2
set QUOTED_IDENTIFIER ON3
go4

5
-- =============================================6
-- Author: HuntFox7
-- Create date: 2005.11.238
-- Description: 十进制整型转二进制字符串(锁定字符串长的最小值)9
-- =============================================10
ALTER FUNCTION [dbo].[ToBinarySystemString] 11
(12
@DecimalSystemInt int,13
@BinarySystemStringLen int14
)15
RETURNS NVARCHAR(31)16
AS17
BEGIN18
-- Declare the return variable here19
declare @ResultVar varchar(31)20
set @ResultVar=''21
while (@DecimalSystemInt<>0)22
begin23
set @ResultVar=@ResultVar+convert(char(1),@DecimalSystemInt%2)24
set @DecimalSystemInt=@DecimalSystemInt/225
end26
set @ResultVar=REVERSE (@ResultVar)27
while (Len(@ResultVar)<@BinarySystemStringLen)28
begin29
set @ResultVar='0'+@ResultVar30
end31
RETURN @ResultVar32
END33

34

35

36

37

38

39

40


浙公网安备 33010602011771号