我必须将所有带零的数字和所有带有*的字母替换为表格中的空字符串。
例如:
UPDATE TABLE
SET COLUMN = replace(replace(replace(COLUMN, 1, 0), 2, 0), 3, 0)
但是这需要这么多嵌套,任何人都可以建议我是否可以在里面替换所有数字并将所有字母组合在一起替换函数?
答案 0 :(得分:0)
如何使用数字或计数表并使用以下查询:
CREATE TABLE #temp
(
MyCol VARCHAR(MAX)
)
INSERT INTO #temp
VALUES
('asdfssgfg022556747dsgfasdg gdgg sdasf52356'),
('gdagasgd gdsdfgdfg dasfasdf ititi 557757 0090891554534456')
;WITH CTE
AS
(
SELECT MyCol, ROW_NUMBER() OVER (ORDER BY (SELECT NEWID())) AS RN
FROM #temp
),
CTE2
AS
(
SELECT RN, Number AS CharPosition, SUBSTRING(MyCol, Number, 1) as Char,
CASE WHEN ISNUMERIC(SUBSTRING(MyCol, Number, 1)) = 1 THEN '0'
WHEN SUBSTRING(MyCol, Number, 1) LIKE '%[a-zA-Z]%' THEN '*'
ELSE ' ' END AS replacement
FROM CTE
CROSS JOIN Numbers
WHERE Number <= LEN(MyCol)
),
CTE3
AS
(
SELECT
CTE2.RN,
CAST((SELECT '' + CTE3.replacement
FROM CTE2 CTE3
WHERE CTE2.RN = CTE3.RN
FOR XML PATH('') ,TYPE) AS varchar(max)) MyCol
FROM CTE2
GROUP BY CTE2.RN
)
UPDATE CTE
SET CTE.MyCol = C2.MyCol
FROM CTE C1
INNER JOIN CTE3 C2
ON C1.RN = C2.RN
SELECT *
FROM #temp