Home  >  Article  >  Database  >  sqlserver replace函数 批量替换数据库中指定字段内指定字符串参

sqlserver replace函数 批量替换数据库中指定字段内指定字符串参

WBOY
WBOYOriginal
2016-06-07 18:00:011131browse

SQL Server有 replace函数,可以直接使用;Access数据库的replace函数只能在Access环境下用,不能用在Jet SQL中,所以对ASP没用,在ASP中调用该函数会提示错误.

语法
REPLACE ( 'string_e­xpression1' , 'string_e­xpression2' , 'string_e­xpression3' )
参数说明
'string_e­xpression1'
待搜索的字符串表达式。string_e­xpression1 可以是字符数据或二进制数据。
'string_e­xpression2'
待查找的字符串表达式。string_e­xpression2 可以是字符数据或二进制数据。
'string_e­xpression3'
替换用的字符串表达式。string_e­xpression3 可以是字符数据或二进制数据。

通俗理解即格式为:
Update 表名 SET 要替换的列=REPLACE(要替换的列,被替换的字符,替换后的字符)
示例SQL语句:
Update tableName SET columeName = REPLACE(columeName, 'a', 'b')

但是值得注意的一点是,SQL Server有 replace函数,可以直接使用;Access数据库的replace函数只能在Access环境下用,不能用在Jet SQL中,所以对ASP没用,在ASP中调用该函数会提示错误:表达式中 'REPLACE' 函数未定义。在Asp中可以写一个函数实现该功能。
示例函数:
代码如下:
function replace(title)
{
replace(title,'aaa','bbbb')
return(title)
}
bbb=replace(title)
update ..... set title='"&bbb&"'

ASP+access批量替换指定字符参考代码:
代码如下:
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("数据库名.mdb")
Set rs = Server.Createobject("ADODB.Recordset")
sql="Select * from [表名]"
rs.open sql,conn,1,3
while not rs.eof
rs("字段名")=replace(rs("字段名"),"被替换的字符","替换为的字符")
rs.update
rs.movenext
wend
rs.close
set rs=nothing
conn.close
set conn=nothing
%>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn