0

我正在使用这种语法,如果它没有出错,它应该可以正常工作。

SELECT
   e.user_key,
   e.char_key,
   CONVERT(VARCHAR,substring(e.char_data, 9, 16)) AS name,
   p.CHAR_DATA
FROM 
   CHAR_DATA0 AS e
INNER JOIN  
   CHAR_DATA1 AS p ON e.CHAR_KEY = p.CHAR_KEY
WHERE 
   p.CHAR_DATA LIKE '%'+CAST(cast(reverse(CONVERT(BINARY, 9998)) as BINARY(2)) AS BINARY(2))+'%'
ORDER BY 
   char_key

我需要帮助。如何更改此代码以使其正常工作?

CHAR_DATA列是类型BINARY(2000)

我有这样的数据:(使用选择获得)

 0x04005C03020F2789080100000F278908010000FFFFFFFFFFFFFF0E2787080100000E278708010000FFFFFFFFFFFFFFBA3B8C08000000BB3B8C080000000F2789080100000F278908010000FFFFFFFFFFFFFF0E2787080100000E278708010000FFFFFFFFFFFFFFBC3B8C08000000BD3B8C080000000F2789080100000F278908010000FFFFFFFFFFFFFF0E2787080100000E278708010000FFFFFFFFFFFFFFBE3B8C08000000BF3B8C080000000F2789080100000F278908010000FFFFFFFFFFFFFF0E2787080100000E278708010000FFFFFFFFFFFFFFC03B8C08000000C13B8C08000000B5388B08020000B5388B08020000FFFFFFFFFFFFFF0E2787080100000E278708010000FFFFFFFFFFFFFFBA3B8C08000000BB3B8C08000000B5388B08020000B5388B08020000FFFFFFFFFFFFFF0E2787080100000E278708010000FFFFFFFFFFFFFFBC3B8C08000000BD3B8C080000000E2789080100000E278908010000FFFFFFFFFFFFFF0E2787080100000E278708010000FFFFFFFFFFFFFFBE3B8C08000000BF3B8C080000000E2789080100000E278908010000FFFFFFFFFFFFFF0E2787080100000E278708010000FFFFFFFFFFFFFFC03B8C08000000C13BFFFF000000FFFFFFFFFFFFFFFFFFFFFFFFFFFF0F27FFFF0100000F27FFFF0100000E2789080000000E278908000000FFFFFFFFFFFFFF7E107F08000000EC168408000000B0177D080000000F27FFFF0100000F27FFFF0100000E2789080000000E27890800000052167C081E000052167C081E0000E2167D08000000271F2005000000FFFFFFFFFFFFFFFFFFFFFFFFFFFF0E2787080100000E27870801000052167C081E000052167C081E0000FFFFFFFFFFFFFF13127F08000000C813FFFF1E0000C813FFFF1E00000E2787080100000E27870801000028127A08000000BF3B2D05000000FFFFFFFFFFFFFF13127F08000000C813FFFF1E0000C813FFFF1E0000FFFFFFFFFFFFFFFFFFFFFFFFFFFF28127A08000000FFFFFFFFFFFFFF1B1A80081A00001B1A80081A0000E915FFFF1B0000E915FFFF1B000023067A0800000024147B0800000020120D05000000084792080100001B1A80081A00001B1A80081A0000E915FFFF1B0000E915FFFF1B000025067A0800000024147B0800000020120D05000000E21610050000000000009D000000FFFF000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

如果其中包含我要查找的条目,我需要在这些行中的某个位置进行搜索。

我要查找的始终是 2 个字节。所以我在上面的语法中寻找 0x0E27 。

这是一种有效的语法,但不是我想要做的:

SELECT 
   CONVERT(INT,cast(reverse(substring(char_data, 37, 2)) as BINARY(2))) AS helm 
FROM CHAR_DATA0 
WHERE CHAR_KEY = 5

另一个是如果我使用 nvarchar(MAX) 它给出的结果甚至不包含我要查找的数据。

4

2 回答 2

1
where Convert(varChar(4000), CHAR_DATA,2 ) like '%' + Convert(varChar(4), CONVERT(BINARY(2), 9998),2 )+'%'
于 2012-11-07T07:07:10.323 回答
0

我尝试只选择 like 子句,主要是因为我不需要您的架构(表)这样做,并且已经发现了一个错误(我认为):

SELECT '%' + CAST( CAST(REVERSE(CONVERT(BINARY, 9998)) as BINARY(2)) AS BINARY(2) ) + '%'

产生:

Msg 402, Level 16, State 1, Line 1
The data types varchar and binary are incompatible in the add operator.

...其中(将整体结果类型更改为 varchar):

SELECT '%' + CAST( CAST(REVERSE(CONVERT(BINARY, 9998)) as BINARY(2)) AS VARCHAR(2) ) + '%'

产生(我们需要一些运气在这里的编码):

%'%    -- that is: percent, apostrophe, beam (the musical note), percent

也许这就是整个问题(尽管我对此表示怀疑)?试试看,让我们知道你的进展。我对此表示怀疑,因为我猜如果这是整个问题,那么您会从错误消息中自己解决(我认为您会从 SQL-Server 获得)。

干杯。基思。

于 2012-11-07T07:10:45.310 回答