0

我正在尝试从BINARY列中查找特定的数据行。

列中的示例数据BINARY(2000)

0x0600700701000C006B6173616E74696E676B6F000000000003000A0078009C0000000000E612101E000000000000000000006B031813361E00000000000000000000D4014B13141C0000000000000000000053017C13261E00000000000000000000A102E113361E000000000000000000009E02FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2201073200000000000000000000D604D11C101C000000000000000000008C01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0170000000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0000220047003700280016000F0001000B0003003F000000480200001D0000003101000000000000562B00003710000000000000134B000000000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFB517010000000000000000000000000012060200000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF191404000000000000000000000000009A13051D00000000000000000000A0029A13051D00000000000000000000A002BC3B0700000000000000000000000000FE050817000000000000000000000000FE05091E00000000000000000000000012060200000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF191404000000000000000000000000009A13051D00000000000000000000A0029A13051D00000000000000000000A002FB050F0D000000000000000000000100FB05101E000000000000000000000100FB05111E000000000000000000000100FE13121D00000000000000000000B402FE13121D00000000000000000000B402E213141E00000000000000000000BC02E213141E00000000000000000000BC023613161D00000000000000000000FE003613161D00000000000000000000FE00FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFB05191E000000000000000000000100FE13121D00000000000000000000B402FE13121D00000000000000000000B402E213141E00000000000000000000BC02E213141E00000000000000000000BC023613161D00000000000000000000FE003613161D00000000000000000000FE000413201E00000000000000000000CC000413201E00000000000000000000CC002E01222C000000000000000000008F052E01222C000000000000000000008F05FB05241E000000000000000000000100FB05251E000000000000000000000100FB05261E000000000000000000000100FB05271E0000000000000000000001000413201E00000000000000000000CC000413201E00000000000000000000CC002E01222C000000000000000000008F052E01222C000000000000000000008F05FB052C1E000000000000000000000100FC052D1E000000000000000000000000A9262E2E000000000000000000000000A9262E2E000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2E01222C000000000000000000008F052E01222C000000000000000000008F05FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA9262E2E000000000000000000000000A9262E2E000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000070000005E000000000000000000000000000000000000000500000000000000FFFF000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

该数据中有 1 个结果2201。(尝试搜索 2201 的数据,您将得到 1。)

现在我正在尝试使用这个查询:(它应该给我 512 行结果)

SELECT * 
FROM table
WHERE binaryfield LIKE '%2201%'
ORDER BY key

但没有运气得到结果。

注意:2201 实际上是 290。我试图在字节序的数据中查找十进制值 290,即十六进制的 2201。

更新:

试过这个查询(搜索:​​15584)

SELECT *
FROM CHAR_DATA1
WHERE CHAR_DATA LIKE '%'+CAST(0xE03C AS nvarchar(MAX))+'%'
ORDER BY CHAR_KEY;

现在它返回几乎所有行。甚至手动检查了 E03C 的行,但没有找到,但是在运行查询时它被返回。很奇怪。想法?

4

1 回答 1

2

LIKE运算符仅适用于字符数据类型。尝试对字符串进行CAST ,如下所示:

SELECT * 
  FROM table
 WHERE CAST(binaryfield AS nvarchar(MAX)) LIKE '%290%'
 ORDER BY key;

- 或者 -

SELECT * 
  FROM table
 WHERE CAST(binaryfield AS nvarchar(MAX)) LIKE '%'+CAST(0x2201 AS nvarchar(MAX))+'%'
 ORDER BY key;

此外,如果您想查看数据的子集(例如文件头),您可以执行以下操作:

SELECT * 
  FROM table
 WHERE CAST(LEFT(binaryfield, 8) AS nvarchar(MAX)) -- Only compares the first 8 bytes
         LIKE '%'+CAST(0x2201 AS nvarchar(MAX))+'%'
    OR CAST(SUBSTRING(binaryfield, 34, 4) AS nvarchar(MAX)) -- Compares bytes 34-38
         LIKE '%'+CAST(0x2201 AS nvarchar(MAX))+'%'
 ORDER BY key;

编辑

关于您更新的问题,这是否会给您带来您期望的结果:

 SELECT *
   FROM CHAR_DATA1
  WHERE CHAR_DATA COLLATE SQL_Latin1_General_CP1_CI_AS
          LIKE '%'+CAST(0xE03C AS nvarchar(MAX))+'%'
 ORDER BY CHAR_KEY;

您不必将字符数据转换/转换为二进制。

于 2012-11-07T02:53:08.483 回答