patternsqlMajor
sys.fn_varbintohexstr returns NULL if varbinary data is more then 2000 as datalength
Viewed 0 times
2000varbinarynullmoredatadatalengthsysreturnsthenfn_varbintohexstr
Problem
sys.fn_varbintohexstr works fine if VARBINARY data length is upto 2000. If it is greater then 2000, function sys.fn_varbintohexstr returns NULL as result. What I am missing ?
```
DECLARE @testvarbinary VARBINARY(MAX)
SELECT @testvarbinary = 0xFFD8FFE000104A46494600010100000100010000FFDB004300090607080706090807080A0A090B0D160F0D0C0C0D1B14151016201D2222201D1F1F2428342C242631271F1F2D3D2D3135373A3A3A232B3F443F384334393A37FFDB0043010A0A0A0D0C0D1A0F0F1A37251F253737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737FFC0001108007E005C03012200021101031101FFC4001B00000301010101010000000000000000000405060302010007FFC4003A100002010302030407080103050000000001020300041105211231411322516106718191A1B1D11423324252C1E1F03315247243A2D2E2F1FFC4001801000301010000000000000000000000000102030004FFC4001F1100020202020301000000000000000000000102112131034104122251FFDA000C03010002110311003F0096D26DAD2EB4BBBC1646E364000186380727C39FC2A05B218827383E35AC17B728922C7348A24FC60311C5EBACD237918051B9341D195A307C96C64D36D27499EE5FF0777AB31D97D746E97A4A16E39003C232CCC365AABD3AC85C42643C50D9A1FC58EF48454E53E91451B02B2D3ECED8810C4D7328DCB636ACF51BD78BBA63B751FA55B71EEA677921951A2B45FB3DA2F7709F89CF866A7353B66B6004B1852DBAC406E079D4AECA7AD0BEF6E44B8DB881DB0798F6D130D8D9DC420714EAF8DF707E9418B476513721C54C6C1256DE37E3239A9FA1D8D35D681562CBCD1A54FF0019627A6DBE3D54B5A09A26C12411E35FA1C68AD082008F6EF2F41E04797974A5BA9E991CE1C6386451B8E7ED1E345727E81C3B44A6188C926B48D805DCD75240CAC4374D8D0ECC1588C8F7D6AB1927583388851DEF7535D1ED8C8C5C292C7650694440C9270AEFBE6ACF4B8858E9C6E1C0E23B463CFC69E6E9118AB633D3AC3ED320B356C229E295C7C7E9EDA6F74DDB4B1D9DB2948D06000390AE74E84E9FA2F6B201DBCC3888EBBF214D74DB0105AB4B311DAB6ECC7E35CECE840B1DB2C518729961DD894F8D21BBD225BEBB62F9E007BCDD5CFD2AB228CDC66729C2A470C6BE0BFCD3286C91635D86E36DB993D6A6DB2918D91EBE8D3ADAA86C14639DFA5663D1E303A48991C2D8C8E87A57E866D87095C023C3C4560F64ACAF19C77971F434136525049123358B346268D543755C6C1BA8F51FDE95DCD
```
DECLARE @testvarbinary VARBINARY(MAX)
SELECT @testvarbinary = 0xFFD8FFE000104A46494600010100000100010000FFDB004300090607080706090807080A0A090B0D160F0D0C0C0D1B14151016201D2222201D1F1F2428342C242631271F1F2D3D2D3135373A3A3A232B3F443F384334393A37FFDB0043010A0A0A0D0C0D1A0F0F1A37251F253737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737FFC0001108007E005C03012200021101031101FFC4001B00000301010101010000000000000000000405060302010007FFC4003A100002010302030407080103050000000001020300041105211231411322516106718191A1B1D11423324252C1E1F03315247243A2D2E2F1FFC4001801000301010000000000000000000000000102030004FFC4001F1100020202020301000000000000000000000102112131034104122251FFDA000C03010002110311003F0096D26DAD2EB4BBBC1646E364000186380727C39FC2A05B218827383E35AC17B728922C7348A24FC60311C5EBACD237918051B9341D195A307C96C64D36D27499EE5FF0777AB31D97D746E97A4A16E39003C232CCC365AABD3AC85C42643C50D9A1FC58EF48454E53E91451B02B2D3ECED8810C4D7328DCB636ACF51BD78BBA63B751FA55B71EEA677921951A2B45FB3DA2F7709F89CF866A7353B66B6004B1852DBAC406E079D4AECA7AD0BEF6E44B8DB881DB0798F6D130D8D9DC420714EAF8DF707E9418B476513721C54C6C1256DE37E3239A9FA1D8D35D681562CBCD1A54FF0019627A6DBE3D54B5A09A26C12411E35FA1C68AD082008F6EF2F41E04797974A5BA9E991CE1C6386451B8E7ED1E345727E81C3B44A6188C926B48D805DCD75240CAC4374D8D0ECC1588C8F7D6AB1927583388851DEF7535D1ED8C8C5C292C7650694440C9270AEFBE6ACF4B8858E9C6E1C0E23B463CFC69E6E9118AB633D3AC3ED320B356C229E295C7C7E9EDA6F74DDB4B1D9DB2948D06000390AE74E84E9FA2F6B201DBCC3888EBBF214D74DB0105AB4B311DAB6ECC7E35CECE840B1DB2C518729961DD894F8D21BBD225BEBB62F9E007BCDD5CFD2AB228CDC66729C2A470C6BE0BFCD3286C91635D86E36DB993D6A6DB2918D91EBE8D3ADAA86C14639DFA5663D1E303A48991C2D8C8E87A57E866D87095C023C3C4560F64ACAF19C77971F434136525049123358B346268D543755C6C1BA8F51FDE95DCD
Solution
There are many reasons to avoid using the system scalar function
-
It is undocumented and unsupported. You won't find it in Books Online and if you report a problem with it, customer support services are not obliged to assist you.
-
It is a T-SQL scalar function. These can be evil.
-
Microsoft do not assign their best and brightest engineers to develop odd bits of T-SQL like this.
-
The function no doubt performs its internal role adequately, but it will not have been tested for uses outside of that internal requirement, nor for performance.
There are any number of supported ways to perform this conversion correctly (including an XML method), so there is really no reason to use
Perhaps my favourite solution for SQL Server 2005 is to use an extension library like SQL#*, which includes a suitable conversion function in the free version. In SQL Server 2008 and later there is direct support for this conversion using
* I have no association with the SQL# product, aside from using it.
In case you are interested, you can see the definition of the system scalar function using:
Or (for output that breaks new lines):
In SQL Server 2005, the code contains this check:
In later versions:
sys.fn_varbintohexstr:-
It is undocumented and unsupported. You won't find it in Books Online and if you report a problem with it, customer support services are not obliged to assist you.
-
It is a T-SQL scalar function. These can be evil.
-
Microsoft do not assign their best and brightest engineers to develop odd bits of T-SQL like this.
-
The function no doubt performs its internal role adequately, but it will not have been tested for uses outside of that internal requirement, nor for performance.
There are any number of supported ways to perform this conversion correctly (including an XML method), so there is really no reason to use
sys.fn_varbintohexstr at all.Perhaps my favourite solution for SQL Server 2005 is to use an extension library like SQL#*, which includes a suitable conversion function in the free version. In SQL Server 2008 and later there is direct support for this conversion using
CONVERT.-- Example conversion using the SQL# library
SELECT SQL#.Convert_BinaryToHexString (@testvarbinary);* I have no association with the SQL# product, aside from using it.
In case you are interested, you can see the definition of the system scalar function using:
SELECT OBJECT_DEFINITION(OBJECT_ID(N'sys.fn_varbintohexsubstring', N'FN'));Or (for output that breaks new lines):
EXECUTE sys.sp_helptext
@objname = N'sys.fn_varbintohexsubstring';In SQL Server 2005, the code contains this check:
if ( ((@cbytesin * 2) + 2 > 4000) or ((@cbytesin * 2) + 2 < 1) )
return NULLIn later versions:
--the output limit for nvarchar(max) is 2147483648 (2^31) bytes, that is 1073741824 (2^30) unicode characters
if ( ((@cbytesin * 2) + 2 > 1073741824) or ((@cbytesin * 2) + 2 < 1) or ( @cbytesin is null ))
return NULLCode Snippets
-- Example conversion using the SQL# library
SELECT SQL#.Convert_BinaryToHexString (@testvarbinary);SELECT OBJECT_DEFINITION(OBJECT_ID(N'sys.fn_varbintohexsubstring', N'FN'));EXECUTE sys.sp_helptext
@objname = N'sys.fn_varbintohexsubstring';if ( ((@cbytesin * 2) + 2 > 4000) or ((@cbytesin * 2) + 2 < 1) )
return NULL--the output limit for nvarchar(max) is 2147483648 (2^31) bytes, that is 1073741824 (2^30) unicode characters
if ( ((@cbytesin * 2) + 2 > 1073741824) or ((@cbytesin * 2) + 2 < 1) or ( @cbytesin is null ))
return NULLContext
StackExchange Database Administrators Q#46910, answer score: 24
Revisions (0)
No revisions yet.