在查询中将0-9转换为n

时间:2014-11-07 01:36:02

标签: sql-server tsql sql-server-2008-r2

我在SO上发现了几个使用UDF的例子,但我试图在没有UDF的情况下这样做。

我正在查询表格,抓取最近的@topCount记录数,将数字0到9转换为小写n,并显示唯一值。

它有效,但我希望能以更好的方式获得一些输入,如果存在的话。

如果一个UDF就是答案,那么我就会去打那场战斗。

DECLARE @topCount AS INT;
DECLARE @fromCharacter AS CHAR(1);
DECLARE @toCharacter AS CHAR(1);

SET @topCount = 2000;
SET @toCharacter = 'n';

IF OBJECT_ID('tempdb..#temp_tbl') IS NOT NULL   DROP TABLE #temp_tbl;

SELECT TOP(@topCount)
    LTRIM(RTRIM(CAST(msg_text AS VARCHAR(MAX)))) AS msgText     --msg_text is a datatype text
    , msg_srce_text
    , creat_gdat
INTO #temp_tbl
FROM dbo.appl_log
ORDER BY
    id DESC                                                     --id is the PK
;

SET @fromCharacter = '0';
UPDATE #temp_tbl
SET
    msgText = REPLACE(msgText, @fromCharacter, @toCharacter)
WHERE CHARINDEX(@fromCharacter, msgText) > 0;
UPDATE #temp_tbl
SET
    msg_srce_text = REPLACE(msg_srce_text, @fromCharacter, @toCharacter)
WHERE CHARINDEX(@fromCharacter, msg_srce_text) > 0;

SET @fromCharacter = '1';
UPDATE #temp_tbl
SET
    msgText = REPLACE(msgText, @fromCharacter, @toCharacter)
WHERE CHARINDEX(@fromCharacter, msgText) > 0;
UPDATE #temp_tbl
SET
    msg_srce_text = REPLACE(msg_srce_text, @fromCharacter, @toCharacter)
WHERE CHARINDEX(@fromCharacter, msg_srce_text) > 0;

SET @fromCharacter = '2';
UPDATE #temp_tbl
SET
    msgText = REPLACE(msgText, @fromCharacter, @toCharacter)
WHERE CHARINDEX(@fromCharacter, msgText) > 0;
UPDATE #temp_tbl
SET
    msg_srce_text = REPLACE(msg_srce_text, @fromCharacter, @toCharacter)
WHERE CHARINDEX(@fromCharacter, msg_srce_text) > 0;

SET @fromCharacter = '3';
UPDATE #temp_tbl
SET
    msgText = REPLACE(msgText, @fromCharacter, @toCharacter)
WHERE CHARINDEX(@fromCharacter, msgText) > 0;
UPDATE #temp_tbl
SET
    msg_srce_text = REPLACE(msg_srce_text, @fromCharacter, @toCharacter)
WHERE CHARINDEX(@fromCharacter, msg_srce_text) > 0;

SET @fromCharacter = '4';
UPDATE #temp_tbl
SET
    msgText = REPLACE(msgText, @fromCharacter, @toCharacter)
WHERE CHARINDEX(@fromCharacter, msgText) > 0;
UPDATE #temp_tbl
SET
    msg_srce_text = REPLACE(msg_srce_text, @fromCharacter, @toCharacter)
WHERE CHARINDEX(@fromCharacter, msg_srce_text) > 0;

SET @fromCharacter = '5';
UPDATE #temp_tbl
SET
    msgText = REPLACE(msgText, @fromCharacter, @toCharacter)
WHERE CHARINDEX(@fromCharacter, msgText) > 0;
UPDATE #temp_tbl
SET
    msg_srce_text = REPLACE(msg_srce_text, @fromCharacter, @toCharacter)
WHERE CHARINDEX(@fromCharacter, msg_srce_text) > 0;

SET @fromCharacter = '6';
UPDATE #temp_tbl
SET
    msgText = REPLACE(msgText, @fromCharacter, @toCharacter)
WHERE CHARINDEX(@fromCharacter, msgText) > 0;
UPDATE #temp_tbl
SET
    msg_srce_text = REPLACE(msg_srce_text, @fromCharacter, @toCharacter)
WHERE CHARINDEX(@fromCharacter, msg_srce_text) > 0;

SET @fromCharacter = '7';
UPDATE #temp_tbl
SET
    msgText = REPLACE(msgText, @fromCharacter, @toCharacter)
WHERE CHARINDEX(@fromCharacter, msgText) > 0;
UPDATE #temp_tbl
SET
    msg_srce_text = REPLACE(msg_srce_text, @fromCharacter, @toCharacter)
WHERE CHARINDEX(@fromCharacter, msg_srce_text) > 0;

SET @fromCharacter = '8';
UPDATE #temp_tbl
SET
    msgText = REPLACE(msgText, @fromCharacter, @toCharacter)
WHERE CHARINDEX(@fromCharacter, msgText) > 0;
UPDATE #temp_tbl
SET
    msg_srce_text = REPLACE(msg_srce_text, @fromCharacter, @toCharacter)
WHERE CHARINDEX(@fromCharacter, msg_srce_text) > 0;

SET @fromCharacter = '9';
UPDATE #temp_tbl
SET
    msgText = REPLACE(msgText, @fromCharacter, @toCharacter)
WHERE CHARINDEX(@fromCharacter, msgText) > 0;
UPDATE #temp_tbl
SET
    msg_srce_text = REPLACE(msg_srce_text, @fromCharacter, @toCharacter)
WHERE CHARINDEX(@fromCharacter, msg_srce_text) > 0;

SELECT MIN(creat_gdat) AS fromDateTime, MAX(creat_gdat) AS toDateTime, @topCount AS mostRecent FROM #temp_tbl;

SELECT
    msgText
    , msg_srce_text
    , COUNT(*) AS count
    , MAX(creat_gdat) AS mostRecentDateTime
FROM #temp_tbl
GROUP BY
    msgText
    , msg_srce_text
ORDER BY
    msgText DESC
;

GO

1 个答案:

答案 0 :(得分:1)

使用REPLACE FUNCTION你可以替换所有的数字数据。试试这个..

UPDATE #temp_tbl
SET    msgtext = Replace (Replace (Replace (Replace (Replace (Replace (
                 Replace (Replace (Replace (Replace (
                 msgtext, '0', 'n'), '1', 'n'), '2', 'n'), '3', 'n'), '4', 'n'), 
                 '5', 'n'), '6', 'n'), '7', 'n'), '8', 'n'), '9', 'n'),
       msg_srce_text = Replace (Replace (Replace (Replace (Replace (Replace 
                      (Replace (Replace (Replace (Replace 
                      (msg_srce_text,'0', 'n'), '1', 'n'), '2', 'n'), '3', 'n'), '4', 'n'), 
                 '5', 'n'), '6', 'n'), '7', 'n'), '8', 'n'), '9', 'n')