--====================================
--标题: 应用实例之SQL SERVER 正则替换
--作者:maco_wang
--时间:2012-03-24
--说明:MS-SQL SERVER 中的正则替换
--补充说明:支持一下CSDN社区支持的活动
--====================================
/*
假设测试数据为:
col
----------------------
192abc168xx0yya101baaa
10hh176bag98job121zero
hello112u19aa110beyp45
a80abab230pppp120qu224
121nile21reply30vall90
想要得到的结果(把字段中的连续的字母替换成'.'):
col
--------------
192.168.0.101
10.176.98.121
112.19.110.45
80.230.120.224
121.21.30.90
*/
--测试数据:
if object_id('[tb]') is not null drop table [tb]
create table [tb] (col varchar(100))
insert into [tb]
select '192abc168xx0yya101baaa' union all
select '10hh176bag98job121zero' union all
select 'hello112u19aa110beyp45' union all
select 'a80abab230pppp120qu224' union all
select '121nile21reply30vall90'
--本示例在SQL SERVER 2005版本即可适用。
--正常思路
--a)游标循环截取(略)
--b)自定义函数
go
create function [dbo].[fn_replace]
(
@str nvarchar(100)
)
returns varchar(100)
as
begin
while patindex('%[a-z]%',@str) > 0
begin
set @str = stuff(@str,patindex('%[a-z]%',@str),1,N'.');
end
while (charindex('..',@str)<>0)
begin
set @str=replace(@str,'..','.')
end
if(left(@str,1)='.') set @str=right(@str,len(@str)-1)
if(right(@str,1)='.') set @str=left(@str,len(@str)-1)
return @str
end
go
select dbo.[fn_replace](col) as col from [tb]
/*
col
---------------
192.168.0.101
10.176.98.121
112.19.110.45
80.230.120.224
121.21.30.90
*/
--c)其他方法,这里我主要介绍一下正则替换,因为patindex和like 能够支持的正则还是非常少的
--正则替换
--开启xp_cmdshell
--不开启会提示:SQL Server blocked access to procedure 'xp_cmdshell'
go
sp_configure 'show advanced options', 1
go
reconfigure
go
sp_configure 'xp_cmdshell', 1
go
reconfigure
--开启sp_OACreate
--不开启会提示:SQL Server blocked access to procedure 'sys.sp_OACreate'
go
sp_configure 'show advanced options', 1;
go
reconfigure;
go
sp_configure 'ole automation procedures', 1;
go
reconfigure;
go
--创建函数
create function [dbo].[regexReplace]
(
@source varchar(8000), --字符串
详细解决方案
SQL SERVER 正则轮换实例分享-【叶子】
热度:39 发布时间:2016-04-24 20:34:21.0
相关解决方案
- myeclipse联接sql server 2008看不到用户表
- Server Tomcat v7.0 Server不能起步
- 在哪找的SQL Server 2005数据库驱动文件?为什么sql server2005安装盘上面没有找到
- The server encountered an internal error () that prevented it from fulfilling th解决方案
- JSP运行异常 STRUTS + SQL SERVER 2005
- jsp连接sql server 2005时出现的异常
- jsp连接sql server 2005数据库是异常!
- 大家帮帮忙呀The server encountered an internal error () that prevented it解决方法
- ASP上Set GetSession("conn")=server.CreateObject("adodb.connection")
- Server Apache Tomcat v6.0.35 at localhost failed to start
- JDBC 连接sql server 2005老是失败怎么处理?恳请高手帮忙。
- SOS-(奇怪现象)服务器暂不可用500 internal server error,该怎么解决
- tomcat没法运行Could not publish to the server. Error while expanding
- sql server 设置非空默认值之后,hibernate添加出现异常
- WEBstruts The server encountered an internal error () that prevented it fr解决思路
- jsp连接sql server 2005错误
- 可以安装在win7系统的sql server
- 出现异常:cvs [server aborted]:"passwd" requires write access to the repository
- exchange server 2000提供全部用户邮件到达的java api吗
- jdbc 直连sql server 2000 连不下
- Error 500-Internal Server Error 如何解决
- JAVA连接SQL SERVER 2000数据库,该如何解决
- Server returned HTTP response code: 403 for URL如何解决
- IM聊天系统的有关问题,用Tigase Server 如何实现挤线功能
- com.microsoft.sqlserver.jdbc.SQLServerException: 用户 'sa' 登录失败。该用户与可托 SQL Server
- Error 500--Internal Server Error
- Error 500-Internal Server Error
- jbuilder2006 连接 sql server 2000新有关问题
- java.sql.SQLException: [Microsoft][ODBC SQL Server Driver]l??????? hstmt
- eclipse中不能筑tomcat server