我是T-SQL的新手。我需要你的帮助来从我在Pivoting后提取的列中的字符串或行中删除多余的字符。
字符串看起来像:?<span style="color:rgb background color then actual string.
我想在提取数据时删除这些额外/特殊字符。
我只能找到从字符串的开头或结尾删除/替换但不确定如何从字符串中删除上述字符
谢谢
答案 0 :(得分:0)
CREATE FUNCTION dbo.PatExclude8K
(
@String VARCHAR(8000),
@Pattern VARCHAR(50)
)
/*******************************************************************************
Purpose:
Given a string (@String) and a pattern (@Pattern) of characters to remove,
remove the patterned characters from the string.
Usage:
--===== Basic Syntax Example
SELECT CleanedString
FROM dbo.PatExclude8K(@String,@Pattern);
--===== Remove all but Alpha characters
SELECT CleanedString
FROM dbo.SomeTable st
CROSS APPLY dbo.PatExclude8K(st.SomeString,'%[^A-Za-z]%');
--===== Remove all but Numeric digits
SELECT CleanedString
FROM dbo.SomeTable st
CROSS APPLY dbo.PatExclude8K(st.SomeString,'%[^0-9]%');
Programmer Notes:
1. @Pattern is not case sensitive (the function can be easily modified to make it so)
2. There is no need to include the "%" before and/or after your pattern since since we
are evaluating each character individually
Revision History:
Rev 00 - 10/27/2014 Initial Development - Alan Burstein
Rev 01 - 10/29/2014 Mar 2007 - Alan Burstein
- Redesigned based on the dbo.STRIP_NUM_EE by Eirikur Eiriksson
(see: http://www.sqlservercentral.com/Forums/Topic1585850-391-2.aspx)
- change how the cte tally table is created
- put the include/exclude logic in a CASE statement instead of a WHERE clause
- Added Latin1_General_BIN Colation
- Add code to use the pattern as a parameter.
Rev 02 - 11/6/2014
- Added final performane enhancement (more cudo's to Eirikur Eiriksson)
- Put 0 = PATINDEX filter logic into the WHERE clause
Rev 03 - 5/16/2015
- Updated code to deal with special XML characters
*******************************************************************************/
RETURNS TABLE WITH SCHEMABINDING AS
RETURN
WITH
E1(N) AS (SELECT N FROM (VALUES (NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL)) AS X(N)),
itally(N) AS
(
SELECT TOP(CONVERT(INT,LEN(@String),0)) ROW_NUMBER() OVER (ORDER BY (SELECT NULL))
FROM E1 T1 CROSS JOIN E1 T2 CROSS JOIN E1 T3 CROSS JOIN E1 T4
)
SELECT NewString =
((
SELECT SUBSTRING(@String,N,1)
FROM iTally
WHERE 0 = PATINDEX(@Pattern,SUBSTRING(@String COLLATE Latin1_General_BIN,N,1))
FOR XML PATH(''),TYPE
).value('.[1]','varchar(8000)'));
GO
我不清楚你想要摆脱或保留哪些角色,但这里 一些例子(注意我的评论):
-- remove letters:
SELECT * FROM dbo.PatExclude8K('abc123!', '[a-zA-Z]');
-- Returns: 123!
;
-- remove numbers:
SELECT * FROM dbo.PatExclude8K('abc123!', '[0-9]');
--Returns: abc!
;
-- only include letters and numbers
SELECT * FROM dbo.PatExclude8K('###abc123!!!', '[^0-9a-zA-Z]');
--Returns: abc123
;
-- Remove spaces (note the spaces):
SELECT * FROM dbo.PatExclude8K('XXX 123 ZZZ', ' ');
--Returns: XXX123ZZZ
;
-- only include letters and "!, ? or ."
SELECT * FROM dbo.PatExclude8K('123# What?!... ', '[^a-zA-Z!?.]');
--Returns: What?!...