读书人

T-SQL 轮换多个空格

发布时间: 2012-09-05 15:19:34 作者: rapoo

T-SQL 替换多个空格

DECLARE @Demo TABLE(OriginalString NVARCHAR(4000)) INSERT INTO @Demo (OriginalString) SELECT ' I am     Incognito,   What    just    for   a  test!' UNION ALL SELECT '看看                     效果                     吧!' UNION ALL SELECT '也许     大概?!  可能' UNION ALL SELECT '我  不知道啊。 谢谢  !' UNION ALL SELECT 'English    is  Okey!.'--===== Reduce each group of multiple spaces to a single space     -- for a whole table without functions, loops, or other     -- forms of slow RBAR.  In the following example, CHAR(7)     -- is the "unlikely" character that "X" was used for in      -- the explanation. SELECT REPLACE(            REPLACE(                REPLACE(                    LTRIM(RTRIM(OriginalString))                ,'  ',' '+CHAR(7))  --Changes 2 spaces to the OX model            ,CHAR(7)+' ','')        --Changes the XO model to nothing        ,CHAR(7),'') AS CleanString --Changes the remaining X's to nothing   FROM @Demo  WHERE CHARINDEX('  ',OriginalString) > 0

I am Incognito, What just for a test!
看看 效果 吧!
也许 大概?! 可能
我 不知道啊。 谢谢 !
English is Okey!.

读书人网 >SQL Server

热点推荐