2013-05-16 13 views
0

關於Need to convert Text field to Varchar temporarily so that I can pass to a stored procedure客戶端數據庫使用文本字段,我使用nvarchar的

我嘗試中投,但我還是得到了倖存的CRLF播放嚴重破壞了我的系統。我無法改變客戶端上的任何東西,我甚至無法創建功能。我本身並不意味着要改變客戶端的任何事情,所以這一切都很好。

有沒有辦法讓我從文本字段中刪除任何和所有的CRLF,製表符和其他ascii代碼作爲SELECT FROM腳本的一部分?

至於我允許查詢數據庫是SQL Server 11.0.2100

回答

1

如果你仔細想想較小一套特殊的字符替換/刪除,你可以在你SELECT使用嵌套REPLACE()

-- preparation of the example 
CREATE TABLE #tt (id int identity (1,1), col text) 
GO 
INSERT INTO #tt (col) VALUES (N'this is a text 
multiline 
3rd line') 
GO 
-- run of the example 
SELECT REPLACE(REPLACE(REPLACE(CAST(col as varchar(MAX)) , 
    CHAR(9), '<9>'), -- replace '<9>' for '' to achieve removal 
    CHAR(10), '<10>'), 
    CHAR(13), '<13>') AS NewText 
    FROM #tt 

--cleanup 
--DROP TABLE #tt 

輸出:

(1 row(s) affected) 
NewText 
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
this is a text<13><10>multiline<13><10>3rd line 

(1 row(s) affected) 
相關問題