2012-11-07 56 views
0

我正在使用此語法,如果只有它沒有出現錯誤,它應該工作得很好。SQL Server:類似於語法和添加運算符錯誤

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)

我有數據,像這樣:(得到它使用select)

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),它給出了甚至不包含我想數據結果找到。

+0

如果可能的話,我會盡量避免將這兩個字節放在一個單獨的字段中。你可以改變CHAR_DATA1表的設計(我懷疑不是,僅僅是因爲它的名字是「狡猾的」)。 – corlettk

+0

對不起,但我無法更改設計。我堅持這個非常糟糕的設計,我需要鍛鍊。請指教。 :) – user1553142

回答

1
where Convert(varChar(4000), CHAR_DATA,2) like '%' + Convert(varChar(4), CONVERT(BINARY(2), 9998),2)+'%' 
+0

當然!在TEXT(不是二進制)中進行比較......一旦你想到Bluddy就顯而易見了。乾杯。 – corlettk

0

我嘗試了選擇,像條款,主要是因爲我不需要你的模式(表)這樣做,並發現了一個bug(我認爲)已經:

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獲得)。

乾杯。基思。