2014-11-07 6 views
0

저는 UDF를 사용하는 여러 가지 예를 발견했지만, UDF없이 이것을하려고합니다.쿼리에서 0-9를 n으로 변환합니다.

가장 최근의 @topCount 레코드 수를 채우고 숫자 0에서 9를 소문자 n으로 변환하고 고유 값을 표시하는 테이블을 쿼리합니다.

효과가 있지만 더 좋은 방법으로 입력이 이루어지기를 바랍니다.

UDF가 답이라면, 나는 그 싸움과 싸울 것입니다. REPLACE FUNCTION를 사용하여

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 
+0

msgText 데이터의 넓은 범위를 갖는다. 때로는 XML 메시지입니다. 다른 때에는 문장 구조의 메시지이므로 A-Z, 공백 및 문장 부호가 사용됩니다. ASCII에서 우주에서 물결표까지, 그리고 가능한 확장 ASCII도 가능합니다. – Kennah

답변

1

유 모든 숫자 data.Try 바꿀 수 ..이

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') 
관련 문제