locked
How to identify a varbinary data type RRS feed

  • Question

  • I am pulling data from a SQL Server database.  One of the fields returned is a varbinary datatype.  How to identify this datatype in an IF statement in a PowerShell script?  For example, "IF [SQL_varbinary_datatype] -eq [some_PowerShell_datatype] THEN ... do something...

    If the SQL data type is checked in a debug session, PowerShell indicates that it is "Byte[]" (i.e., a byte array), of base type System.Array.  But if this type is used in the conditional statement, PowerShell does not recognize it (e.g., ... if( $sql_value -eq [System.Byte[]]::Value ) ... PowerShell will not evaluate this statement to $true and drop into the conditional code.

    I am hoping that someone knows how to identify this datatype in PowerShell!

    Tuesday, June 6, 2017 4:13 PM

Answers

  • One way is this:

    $x = [byte[]](1,2,3,4,5,6)
    $x.GetType().Name -eq 'byte[]'

    Without more of your script it is hard to guess at what you are trying to do.  Each column in a data table has a type that can be tested directly.


    \_(ツ)_/

    • Marked as answer by Phoenix07 Tuesday, June 6, 2017 7:10 PM
    Tuesday, June 6, 2017 5:41 PM

All replies

  • Use varbinary(max) when the column data entries exceed 8,000 bytes.

    Variable-length binary data. n can be a value from 1 through 8,000. max indicates that the maximum storage size is 2^31-1 bytes. The storage size is the actual length of the data entered + 2 bytes. The data that is entered can be 0 bytes in length. The ANSI SQL synonym for varbinary is 

    Tuesday, June 6, 2017 4:25 PM
  • One way is this:

    $x = [byte[]](1,2,3,4,5,6)
    $x.GetType().Name -eq 'byte[]'

    Without more of your script it is hard to guess at what you are trying to do.  Each column in a data table has a type that can be tested directly.


    \_(ツ)_/

    • Marked as answer by Phoenix07 Tuesday, June 6, 2017 7:10 PM
    Tuesday, June 6, 2017 5:41 PM
  • That works!  Sometimes it is just a matter of getting the syntax correct!  Thank you!
    Tuesday, June 6, 2017 7:10 PM