我正在使用sql Server 2008.在我的表中,我有一个名为TestData的二进制类型的列.
TestData列中的示例数据是
1. 0x0001DC780C0030373156635D0C00B8840301009A0600AC 2. 0x0301DC780C0030373156385D0C006499C401009A0600AC
写下面两个查询以获取TestData以“0x0001”开头的行.但他们都没有工作.
SELECT * FROM T_TRANSACTION WHERE CAST(Indicium AS nvarchar(MAX)) LIKE '0x0001%' ----No results found SELECT * FROM T_TRANSACTION WHERE CAST(Indicium AS nvarchar(MAX)) LIKE '0x0001%' ----Returns all the rows
请更正查询以获得预期结果
解决方法
不要转换它,但是视为一个范围(就像你的日期时间值一样)
DECLARE @foo TABLE (TestData varbinary(100) NOT NULL); INSERT @foo (TestData) VALUES (0x0001DC780C0030373156635D0C00B8840301009A0600AC),(0x0001AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA),(0x0001AFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF),(0x0301DC780C0030373156385D0C006499C401009A0600AC),(0x0301FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF),(0x0302000000000000000000000000000000000000000000); SELECT * FROM @foo WHERE TestData >= 0x0001 AND TestData < 0x0002; -- added more digit for clarity of what actually happens SELECT * FROM @foo WHERE TestData >= 0x00010000 AND TestData < 0x00020000; SELECT * FROM @foo WHERE TestData >= 0x0001AA AND TestData < 0x0001AB; SELECT * FROM @foo WHERE TestData >= 0x0301 AND TestData < 0x0302;
这具有能够在TestData上使用索引的好处
编辑,您只需指定所需的数字