locked
CAST or CONVERT to binary from HASH BYTES value RRS feed

  • Question

  • Experts,

    I've below example

    SELECT	HASHBYTES('SHA1', ISNULL(CAST('Default 270-271 Shell Product' AS VARCHAR(8000)),'') + ISNULL(CAST('N' AS VARCHAR(8000)), '') ) AS NOCONVERSION
    		, CAST( HASHBYTES('SHA1', ISNULL(CAST('Default 270-271 Shell Product' AS VARCHAR(8000)),'') + ISNULL(CAST('N' AS VARCHAR(8000)), '') ) AS BINARY(20) ) AS CASTHASHBYTES
    		, CONVERT( BINARY(20), HASHBYTES('SHA1', ISNULL(CAST('Default 270-271 Shell Product' AS VARCHAR(8000)),'') + ISNULL(CAST('N' AS VARCHAR(8000)), '') ) ) AS CONVERTHASHBYTES
    

    Is it necessary to convert or cast to binary? Values are same isn't for all the 3 columns?

    Thank You

    Regards,

    Kumar


    Please do let us know your feedback. Thank You - KG, MCTS

    Tuesday, October 28, 2014 6:42 PM

All replies

  • It will be same only as by default the return type of HASHBYTES is varbinary. binary and varbinary are compatible data types so each can be implicitly converted to the other

    see

    http://msdn.microsoft.com/en-IN/library/ms187928.aspx


    Please Mark This As Answer if it solved your issue
    Please Mark This As Helpful if it helps to solve your issue
    Visakh
    ----------------------------
    My MSDN Page
    My Personal Blog
    My Facebook Page

    • Proposed as answer by Katherine Xiong Wednesday, October 29, 2014 12:05 PM
    Tuesday, October 28, 2014 7:20 PM
  • So that means there is no reason of doing cast/convert operation as the return type of HASHBYTES is binary, is it so?

    Please do let us know your feedback. Thank You - KG, MCTS

    Wednesday, October 29, 2014 2:37 PM