I have a table of hexadecimal values stored in a varchar(32)
column. The values can be anywhere between 000FFF100000
and 000FFFA90FFF
I would like to find all rows between 000FFF300000
and 000FFF7FFF05
.
Any suggestions?
sql serversql-server-2016
I have a table of hexadecimal values stored in a varchar(32)
column. The values can be anywhere between 000FFF100000
and 000FFFA90FFF
I would like to find all rows between 000FFF300000
and 000FFF7FFF05
.
Any suggestions?
Best Answer
Assuming that you can't do the sensible thing and change the datatype of the column to be
VARBINARY(10)
, then just convert the value in theVARCHAR
column intoVARBINARY(10)
and then test to see if it is in that range. For example:returns only the following row:
The style number of "2" in the
CONVERT
function allows a string of hex bytes without the preceding "0x" to be converted into binary without assuming that the hex bytes are encoded characters.IF you will be doing such queries frequently, then it would be best to do the following:
ALTER TABLE {schema_name}.{table_name} ADD {column_name} AS (CONVERT(VARBINARY(10), [{varchar_column_name}], 2));
)