日期:2014-05-18  浏览次数:20826 次

SQLSERVER里如何剔除:字符串中除了数字的字符,另外如何剔除除了大小写字母的字符
SQLSERVER里如何剔除:字符串中除了数字的字符,另外如何剔除除了大小写字母的字符

------解决方案--------------------
declare @str varchar(1000)
set @str = '21838asdf21jj '
while patindex( '%[^0-9]% ',@str)> 0
set @str = stuff(@str,patindex( '%[^0-9]% ',@str),1, ' ')
select @str

--结果
2183821

(所影响的行数为 1 行)
------解决方案--------------------
create table T(col varchar(100))
insert T select '1aa23 '
insert T select 'bb45 '
insert T select '67cc '

while @@rowcount <> 0
update T set col=stuff(col, patindex( '%[^0-9]% ', col), 1, ' ')
where patindex( '%[^0-9]% ', col)> 0

select * from T

--result
col
----------------------------------------------------------------
123
45
67

(3 row(s) affected)