想发布上述问题的解决方案,在其他地方提问,但没有声望点,因此请提出并同时回答,因为其他人可能会觉得此解决方案有用..:
CREATE FUNCTION [dbo].[u_FN_CleanString](@string VARCHAR(MAX))
RETURNS VARCHAR(MAX)
AS
BEGIN
;
WITH DummyTable(DummyColumn) AS (SELECT 1
FROM (VALUES (1),(1),(1),(1),(1),(1),(1),(1)) DummyTable(DummyColumn))
-- ASCII 0-31 (join 5 times: 2->4->8->16->32 rows)
, Unprintables(Chrctr) AS (SELECT TOP(32) CHAR(ROW_NUMBER() OVER (ORDER BY (SELECT NULL))-1)
FROM DummyTable a,DummyTable b,DummyTable c,DummyTable d,DummyTable e
UNION
-- ASCII 128-255 (join 7 times: 2->4->8->16->32->64->128 rows)
SELECT TOP(128) CHAR(ROW_NUMBER() OVER (ORDER BY (SELECT NULL))+127)
FROM DummyTable a,DummyTable b,DummyTable c,DummyTable d,DummyTable e,DummyTable f,DummyTable g)
-- Remove extended ASCII characters
SELECT @string=REPLACE(@string,Chrctr,'')
FROM Unprintables
-- Do not replace Carriage Returns or Line Feeds
WHERE Chrctr NOT IN (CHAR(10),CHAR(13))
RETURN RTRIM(@string)
END
答案 0 :(得分:0)
评论太久了。
只是要考虑的事情。您的函数可能会产生意外/不良结果。
假设您有一个字符串 import time
import pymongo
client = pymongo.MongoClient(
'localhost', 27017)
db = client["Bubble"]
cursor = db.BELFRA_SEMI.find(cursor_type=pymongo.CursorType.TAILABLE_AWAIT)
while cursor.alive:
print "Starting tailing."
for doc in cursor:
print doc
# We end up here if the find() returned no documents or if the
# tailable cursor timed out (no new documents were added to the
# collection for more than 1 second).
time.sleep(1)
print "Slept for 1 second"
,您的函数将返回 'Red{tab}{tab}{tab}hat'
。
因此,与其使用 'Redhat'
(而不是破坏性较大的东西)(而不是空格),而不是使用 @string=REPLACE(@string,Chrctr,'')
然后我们将进行最后的清理,以删除重复的空格
@string=REPLACE(@string,Chrctr,' ')
返回的值为 Return ltrim(rtrim(replace(replace(replace(@string,' ','†‡'),'‡†',''),'†‡',' ')))
。
全面披露::清理方法由Gordon不久前提供。我只是对其进行了调整,以使用更多的独特字符,例如'†‡',而不是'> <'