Skip to content

Instantly share code, notes, and snippets.

@pattertall
Created May 9, 2019 15:27
Show Gist options
  • Save pattertall/133ee436496cbc15e17744afe0599719 to your computer and use it in GitHub Desktop.
Save pattertall/133ee436496cbc15e17744afe0599719 to your computer and use it in GitHub Desktop.
// SQL CLR function for converting 32 nullable INTs into a binary string
[SqlFunction(DataAccess = DataAccessKind.Read)]
public static SqlBinary NullableIntsToBinaryInline(
SqlInt32 col01,
SqlInt32 col02,
SqlInt32 col03,
SqlInt32 col04,
SqlInt32 col05,
SqlInt32 col06,
SqlInt32 col07,
SqlInt32 col08,
SqlInt32 col09,
SqlInt32 col10,
SqlInt32 col11,
SqlInt32 col12,
SqlInt32 col13,
SqlInt32 col14,
SqlInt32 col15,
SqlInt32 col16,
SqlInt32 col17,
SqlInt32 col18,
SqlInt32 col19,
SqlInt32 col20,
SqlInt32 col21,
SqlInt32 col22,
SqlInt32 col23,
SqlInt32 col24,
SqlInt32 col25,
SqlInt32 col26,
SqlInt32 col27,
SqlInt32 col28,
SqlInt32 col29,
SqlInt32 col30,
SqlInt32 col31,
SqlInt32 col32
)
{
var byteArray = new byte[132];
// Convert each into to a byte representation and append to the byte array
// NULL values are represented as 0
BitConverter.GetBytes(col01.IsNull ? 0 : col01.Value).CopyTo(byteArray, 000);
BitConverter.GetBytes(col02.IsNull ? 0 : col02.Value).CopyTo(byteArray, 004);
BitConverter.GetBytes(col03.IsNull ? 0 : col03.Value).CopyTo(byteArray, 008);
BitConverter.GetBytes(col04.IsNull ? 0 : col04.Value).CopyTo(byteArray, 012);
BitConverter.GetBytes(col05.IsNull ? 0 : col05.Value).CopyTo(byteArray, 016);
BitConverter.GetBytes(col06.IsNull ? 0 : col06.Value).CopyTo(byteArray, 020);
BitConverter.GetBytes(col07.IsNull ? 0 : col07.Value).CopyTo(byteArray, 024);
BitConverter.GetBytes(col08.IsNull ? 0 : col08.Value).CopyTo(byteArray, 028);
BitConverter.GetBytes(col09.IsNull ? 0 : col09.Value).CopyTo(byteArray, 032);
BitConverter.GetBytes(col10.IsNull ? 0 : col10.Value).CopyTo(byteArray, 036);
BitConverter.GetBytes(col11.IsNull ? 0 : col11.Value).CopyTo(byteArray, 040);
BitConverter.GetBytes(col12.IsNull ? 0 : col12.Value).CopyTo(byteArray, 044);
BitConverter.GetBytes(col13.IsNull ? 0 : col13.Value).CopyTo(byteArray, 048);
BitConverter.GetBytes(col14.IsNull ? 0 : col14.Value).CopyTo(byteArray, 052);
BitConverter.GetBytes(col15.IsNull ? 0 : col15.Value).CopyTo(byteArray, 056);
BitConverter.GetBytes(col16.IsNull ? 0 : col16.Value).CopyTo(byteArray, 060);
BitConverter.GetBytes(col17.IsNull ? 0 : col17.Value).CopyTo(byteArray, 064);
BitConverter.GetBytes(col18.IsNull ? 0 : col18.Value).CopyTo(byteArray, 068);
BitConverter.GetBytes(col19.IsNull ? 0 : col19.Value).CopyTo(byteArray, 072);
BitConverter.GetBytes(col20.IsNull ? 0 : col20.Value).CopyTo(byteArray, 076);
BitConverter.GetBytes(col21.IsNull ? 0 : col21.Value).CopyTo(byteArray, 080);
BitConverter.GetBytes(col22.IsNull ? 0 : col22.Value).CopyTo(byteArray, 084);
BitConverter.GetBytes(col23.IsNull ? 0 : col23.Value).CopyTo(byteArray, 088);
BitConverter.GetBytes(col24.IsNull ? 0 : col24.Value).CopyTo(byteArray, 092);
BitConverter.GetBytes(col25.IsNull ? 0 : col25.Value).CopyTo(byteArray, 096);
BitConverter.GetBytes(col26.IsNull ? 0 : col26.Value).CopyTo(byteArray, 100);
BitConverter.GetBytes(col27.IsNull ? 0 : col27.Value).CopyTo(byteArray, 104);
BitConverter.GetBytes(col28.IsNull ? 0 : col28.Value).CopyTo(byteArray, 108);
BitConverter.GetBytes(col29.IsNull ? 0 : col29.Value).CopyTo(byteArray, 112);
BitConverter.GetBytes(col30.IsNull ? 0 : col30.Value).CopyTo(byteArray, 116);
BitConverter.GetBytes(col31.IsNull ? 0 : col31.Value).CopyTo(byteArray, 120);
BitConverter.GetBytes(col32.IsNull ? 0 : col32.Value).CopyTo(byteArray, 124);
// Build a NULL bitmap to differentiate between a NULL value and a true 0 for any given input column
uint u0 = 0; uint u1 = 1;
uint nullBitmap =
(col01.IsNull ? (u1 << 00) : u0) +
(col02.IsNull ? (u1 << 01) : u0) +
(col03.IsNull ? (u1 << 02) : u0) +
(col04.IsNull ? (u1 << 03) : u0) +
(col05.IsNull ? (u1 << 04) : u0) +
(col06.IsNull ? (u1 << 05) : u0) +
(col07.IsNull ? (u1 << 06) : u0) +
(col08.IsNull ? (u1 << 07) : u0) +
(col09.IsNull ? (u1 << 08) : u0) +
(col10.IsNull ? (u1 << 09) : u0) +
(col11.IsNull ? (u1 << 10) : u0) +
(col12.IsNull ? (u1 << 11) : u0) +
(col13.IsNull ? (u1 << 12) : u0) +
(col14.IsNull ? (u1 << 13) : u0) +
(col15.IsNull ? (u1 << 14) : u0) +
(col16.IsNull ? (u1 << 15) : u0) +
(col17.IsNull ? (u1 << 16) : u0) +
(col18.IsNull ? (u1 << 17) : u0) +
(col19.IsNull ? (u1 << 18) : u0) +
(col20.IsNull ? (u1 << 19) : u0) +
(col21.IsNull ? (u1 << 20) : u0) +
(col22.IsNull ? (u1 << 21) : u0) +
(col23.IsNull ? (u1 << 22) : u0) +
(col24.IsNull ? (u1 << 23) : u0) +
(col25.IsNull ? (u1 << 24) : u0) +
(col26.IsNull ? (u1 << 25) : u0) +
(col27.IsNull ? (u1 << 26) : u0) +
(col28.IsNull ? (u1 << 27) : u0) +
(col29.IsNull ? (u1 << 28) : u0) +
(col30.IsNull ? (u1 << 29) : u0) +
(col31.IsNull ? (u1 << 30) : u0) +
(col32.IsNull ? (u1 << 31) : u0);
BitConverter.GetBytes(nullBitmap).CopyTo(byteArray, 128);
return new SqlBinary(byteArray);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment