Create Function DBO. f_dec_bin (@ num bigint, @ length INT)
Returns varchar (64)
As
Begin
-- Author: pbsql
-- @ Num is the positive integer to be converted to a binary string. If it is a negative number, '0' is returned'
-- @ Length indicates the length of the binary string to be returned.
-- If @ length is negative, '0' is returned. If it is 0, the actual length of the binary string is returned.
-- If @ length is greater than 0, the return value is a binary string with the length of @ length (from the low position, the bits are not high enough to fill in '0', and the bits are truncated when the bits exceed the bits)
Declare @ result varchar (64)
Set @ result =''
If @ num <= 0 or @ length <0
Set @ result = '0'
Else
Begin
While @ num <> 0
Select @ result = cast (@ num % 2 as varchar) + @ result, @ num = @ num/2
If @ length> 0
Set @ result = right (replicate ('0', @ length) + @ result, @ length)
End
Return @ result
End
Go
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service