我在sql server 2008中有一个表,其中一列包含字符串和特殊字符的字段 ABC;#34; PQR。现在我想要一个函数从该字段中删除特殊字符和数字;因此输出将类似于abcpqr。
答案 0 :(得分:2)
试试这个
DECLARE @str VARCHAR(400)
DECLARE @expres VARCHAR(50) = '%[~,@,#,$,%,&,*,(,),.,!,0-9]%'
SET @str = '(remove) ~special~ 10 *characters. 3 5 from string 1 in sql!'
WHILE PATINDEX( @expres, @str ) > 0
SET @str = Replace(REPLACE( @str, SUBSTRING( @str, PATINDEX( @expres, @str ), 1 ),''),'-',' ')
SELECT @str
答案 1 :(得分:0)
使用以下功能
/*********************************
Removes any characters from
@myString that do not meet the
provided criteria.
*********************************/
CREATE FUNCTION dbo.GetCharacters(@myString varchar(500), @validChars varchar(100))
RETURNS varchar(500) AS
BEGIN
While @myString like '%[^' + @validChars + ']%'
Select @myString = replace(@myString,substring(@myString,patindex('%[^' + @validChars + ']%',@myString),1),'')
Return @myString
END
Go
Declare @testStr varchar(1000),
@i int
Set @i = 1
while @i < 255
Select
@TestStr = isnull(@TestStr,'') + isnull(char(@i),''),
@i = @i + 1
Select @TestStr
Select dbo.GetCharacters(@TestStr,'a-z')
Select dbo.GetCharacters(@TestStr,'0-9')
Select dbo.GetCharacters(@TestStr,'0-9a-z')
Select dbo.GetCharacters(@TestStr,'02468bferlki')
答案 2 :(得分:0)
试试这个: -
DECLARE @var varchar(255) = 'abc;#34;pqr';
SELECT
CONVERT(varchar(255),(
SELECT CASE
WHEN ( ASCII(UPPER(SUBSTRING(@var, Number, 1))) BETWEEN 65 and 90 )
THEN SUBSTRING(@var, Number, 1)
END
FROM
(
Select top(255) number FROM [master]..spt_values
where type = 'p'
) AS n
WHERE Number <= LEN(@var)
FOR XML PATH(''))) as Result
结果
abcpqr