正则表达式删除数字和' - '并使用@#替换所有其他数据替换换行符

时间:2014-11-26 18:36:04

标签: sql-server regex

  

45378 - 结肠镜检查,诊断(单独手术)

     

45331 - Flexi乙状结肠镜检查与活组织检查

     

45378 - 结肠镜检查,诊断(单独手术)

     

45331 - Flexi乙状结肠镜检查与活组织检查

     

45378 - 结肠镜检查,诊断(单独手术)

     

45331 - Flexi乙状结肠镜检查与活组织检查

如何在sql server

中获取如下所示的上述文本
@# Colonoscopy, diagnostic (separate procedure) 
@# Flexi Sigmoidoscopy with biopsy 
@# Colonoscopy, diagnostic (separate procedure) 
@# Flexi Sigmoidoscopy with biopsy 
@# Colonoscopy, diagnostic (separate procedure) 
@# Flexi Sigmoidoscopy with biopsy

1 个答案:

答案 0 :(得分:0)

使用您的示例提供了以下内容,但数值的长度在PATINDEX和STUFF用法中是硬编码的:

 DECLARE @Text NVARCHAR(MAX)

 SET @Text = '45378 - Colonoscopy, diagnostic (separate procedure) 45331 - Flexi Sigmoidoscopy with biopsy 45378 - Colonoscopy, diagnostic (separate procedure) 45331 - Flexi Sigmoidoscopy with biopsy 45378 - Colonoscopy, diagnostic (separate procedure) 45331 - Flexi Sigmoidoscopy with biopsy'

 WHILE (PATINDEX('%[0-9][0-9][0-9][0-9][0-9]%', @Text) > 0)
 BEGIN
        SET @Text = STUFF(@Text, PATINDEX('%[0-9][0-9][0-9][0-9][0-9]%', @Text), 5, '@#')
 END

 SELECT @Text