string hex
2600 0x2600
0100 0x0100
0400 0x0400
0000 0x0000
0000 0x0000
0000 0x0000
0000 0x0000
0000 0x0000
string hex
2600 0x2600
0100 0x0100
0400 0x0400
0000 0x0000
0000 0x0000
0000 0x0000
0000 0x0000
0000 0x0000
That was just a way to see what the pieces are. To put them together into a single hex value, you can do the following:
DECLARE @IPv6 VARBINARY(16) = 0x;
SELECT @IPv6 += CONVERT(BINARY(2), RIGHT('0000' + prt.[SplitVal], 4), 2)
FROM SQL#.String_Split4k('2600:100:400:0:0:0:0:0', ':', 1) prt;
SELECT @IPv6;
-- 0x26000100040000000000000000000000
You can create a function to handle this conversion. You can then store these VARBINARY(16)
values and create an index on them, and then convert the string coming in and store that in a variable to use in the query.
Here is that same approach, but using the built-in STRING_SPLIT
function:
DECLARE @IPv6 VARBINARY(16) = 0x;
SELECT @IPv6 += CONVERT(BINARY(2), RIGHT('0000' + prt.[SplitVal][value], 4), 2)
FROM SQL#.String_Split4kSTRING_SPLIT('2600:100:400:0:0:0:0:0', ':', 1) prt;
SELECT @IPv6;
-- 0x26000100040000000000000000000000
You can create a functionJust keep in mind that some folks are hesitant to handle this conversion. You can then store theseuse VARBINARY(16)STRING_SPLIT
values and create an index on them, and then convertin this type of situation because here the string comingorder that the split parts are returned is important as it can change the value if they came back in a different order, and storethe built-in function doesn't return a row number. I personally don't see how the returned values could come back out of order, but that inis just an educated guess and not a variable toguarantee. If you want a guarantee, then don't use in the querySTRING_SPLIT
built-in function.