<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Sybase数据库技术，数据库恢复专家 &#187; str_replace</title>
	<atom:link href="http://www.dbainfo.net/tag/str_replace/feed" rel="self" type="application/rss+xml" />
	<link>https://www.dbainfo.net</link>
	<description>提供Sybase ASE及Sybase SQL Anywhere数据库修复服务，电话：13811580958(微信)，QQ：289965371！We have many years of experience in recovering data from damanged Sybase devices. Contact us by Phone: +86 13811580958 Wechat: 13811580958 Email: 289965371@qq.com</description>
	<lastBuildDate>Sat, 14 Jun 2025 16:28:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>ASE15.0.3中替换字符串函数str_replace的奇怪的用法</title>
		<link>https://www.dbainfo.net/ase-replace-space-character-with-null-by-str_replace-function.htm</link>
		<comments>https://www.dbainfo.net/ase-replace-space-character-with-null-by-str_replace-function.htm#comments</comments>
		<pubDate>Sat, 12 Jun 2010 10:41:02 +0000</pubDate>
		<dc:creator>dbainfo</dc:creator>
				<category><![CDATA[Sybase ASE]]></category>
		<category><![CDATA[str_replace]]></category>

		<guid isPermaLink="false">http://www.dbainfo.net/?p=745</guid>
		<description><![CDATA[在ASE12.5.x平台中没有替换字符串的函数，也就是说没有类似replace的函数。对于replace这个词，我想有编程经验的肯定一看字 面意思就知道是将某一字符串中指定的字符串替换为另外的字符串。可惜的是这么简单的功能在ase12.5.x中没有相应的函数，只有一个可怜的stuff 函数。 stuff函数仅仅能够替换某一个字符串中某一个位置处的字符串为另外的字符串，如果这个字符串中有多处需要被替换呢？抱歉，stuff歇菜了。 一个简单的类似replace的功能还得需要写个过程或者函数来实现。 写起来效率低执行起来效率更低， so pooor. 谁要是实现了用简单的一两条sql语句顶替replace的功能，麻烦告诉我一声。谢谢！ 幸运的是，ASE15.0版本中引入了类似replace的函数，但是人家的名字叫str_replace，不知道为啥？replace这个词和那个关键字冲突吗？ 不解ZZZZZZZ~~~ str_replace函数的语法很简单： &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; st_replace(&#34;string_expression1&#34;, &#34;string_expression2&#34;, &#34;string_expression3&#34;) 就是：将字符串1中所包含的所有 的字符串2都用字符串3统统替换。 一个简单的例子： 1&#62; select str_replace(&#39;This is Andkylee!My nAme is andkylee!&#39;,&#39;A&#39;,&#39;a&#39;) 2&#62; go &#160;------------------------------------- &#160;This is andkylee!My name is andkylee! (1 row affected) &#160; 但是， 网上有人发帖说str_replace不能替换字符串中的空格为&#8220;（也就是说删除字符串中的所有空格）。不看官方文档，想当然的就写出来这样的语句：select str_replace(&#39;123 456 &#39;,&#39; &#39;,&#39;&#39;) 但是很可惜，执行失败。 1&#62; select len(&#39;123 456 &#39;),str_replace(&#39;123 456 &#39;,&#39; &#39;,&#39;&#39;) [...]]]></description>
			<content:encoded><![CDATA[<p>在ASE12.5.x平台中没有替换字符串的函数，也就是说没有类似replace的函数。对于replace这个词，我想有编程经验的肯定一看字 面意思就知道是将某一字符串中指定的字符串替换为另外的字符串。可惜的是这么简单的功能在ase12.5.x中没有相应的函数，只有一个可怜的stuff 函数。</p>
<p>stuff函数仅仅能够替换某一个字符串中某一个位置处的字符串为另外的字符串，如果这个字符串中有多处需要被替换呢？抱歉，stuff歇菜了。</p>
<p>一个简单的类似replace的功能还得需要写个过程或者函数来实现。 写起来效率低执行起来效率更低， so pooor.</p>
<p>谁要是实现了用简单的一两条sql语句顶替replace的功能，麻烦告诉我一声。谢谢！</p>
<p>幸运的是，ASE15.0版本中引入了类似replace的函数，但是人家的名字叫str_replace，不知道为啥？replace这个词和那个关键字冲突吗？ 不解ZZZZZZZ~~~</p>
<p>str_replace函数的语法很简单：</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>&nbsp;&nbsp; st_replace(&quot;string_expression1&quot;, &quot;string_expression2&quot;, &quot;string_expression3&quot;)</strong></p>
<p>就是：将字符串1中所包含的<strong>所有</strong> 的字符串2都用字符串3统统替换。</p>
<p>一个简单的例子：</p>
<blockquote>
<p>1&gt; select str_replace(&#39;This is Andkylee!My nAme is andkylee!&#39;,&#39;A&#39;,&#39;a&#39;)<br />
		2&gt; go<br />
		&nbsp;-------------------------------------<br />
		&nbsp;This is andkylee!My name is andkylee!<br />
		(1 row affected)</p>
</blockquote>
<p>&nbsp;</p>
<p>但是， 网上有人发帖说str_replace不能替换字符串中的空格为&ldquo;（也就是说删除字符串中的所有空格）。不看官方文档，想当然的就写出来这样的语句：select str_replace(&#39;123 456 &#39;,&#39; &#39;,&#39;&#39;)</p>
<p>但是很可惜，执行失败。</p>
<blockquote>
<p>1&gt; select len(&#39;123 456 &#39;),str_replace(&#39;123 456 &#39;,&#39; &#39;,&#39;&#39;) ,len(str_replace(&#39;123 456 &#39;,&#39; &#39;,&#39;&#39;))<br />
		2&gt; go<br />
		&nbsp;----------- -------- -----------<br />
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 8 123 456&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 8<br />
		(1 row affected)</p>
</blockquote>
<p>&nbsp;</p>
<p>如上可以看到select str_replace(&#39;123 456 &#39;,&#39; &#39;,&#39;&#39;)并没有替换其中的空格为空（亦即删除其中的所有空格），用函数str_replace执行前后的字符串是相同的。</p>
<p>通过查阅官方文档中关于str_replace的介绍，有下面的两点需要注意：</p>
<p><strong>1. Adaptive Server 将空字符串常量自动转换为 1 个空格的字符串，以便将该字符串与 NULL 值区分开。</strong></p>
<p><strong>2. str_replace 在第三个参数中接受 NULL，将其视为尝试用 NULL 替换 string_expression2，有效地将 str_replace 转换成&ldquo;字符串切除&rdquo;<br />
	操作。</strong></p>
<p>这两条的意思是说：在函数str_replace中&quot;&quot;相当于&quot; &quot;, NULL 相当于&ldquo;&rdquo;（空字符）。</p>
<p>&nbsp;<span style="font-size: medium;"><strong>&quot;&quot;相当于&quot; &quot;</strong> </span></p>
<p>示例：</p>
<blockquote>
<p>1&gt; select str_replace(&quot;cde fghi &quot;,&quot;&quot;,&quot;_&quot;)<br />
		2&gt; go<br />
		&nbsp;---------<br />
		&nbsp;cde_fghi_<br />
		(1 row affected)<br />
		1&gt; select str_replace(&quot;cde fghi &quot;,&quot; &quot;,&quot;_&quot;)<br />
		2&gt; go<br />
		&nbsp;---------<br />
		&nbsp;cde_fghi_<br />
		(1 row affected)</p>
</blockquote>
<p>&nbsp;</p>
<p>两个空格还是2个空格意思:</p>
<p>1&gt; select str_replace(&quot;cde fghi &quot;,&quot;&nbsp; &quot;,&quot;_&quot;)<br />
	2&gt; go</p>
<p>	&nbsp;---------<br />
	&nbsp;cde fghi</p>
<p>	(1 row affected)</p>
<p><span style="font-size: medium;"><strong>NULL 相当于&ldquo;&rdquo;（空字符）</strong> </span></p>
<p>示例如下：</p>
<blockquote>
<p>1&gt; select str_replace(&quot;cde fghi &quot;,&quot; &quot;,null),len(str_replace(&quot;cde fghi &quot;,&quot; &quot;,null))<br />
		2&gt; go<br />
		&nbsp;--------- -----------<br />
		&nbsp;cdefghi&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 7<br />
		(1 row affected)<br />
		1&gt; select str_replace(&quot;cde fghi &quot;,&quot;&quot;,null),len(str_replace(&quot;cde fghi &quot;,&quot; &quot;,null))<br />
		2&gt; go<br />
		&nbsp;--------- -----------<br />
		&nbsp;cdefghi&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 7<br />
		(1 row affected)</p>
</blockquote>
<p>&nbsp;</p>
<p><span style="font-size: medium;"><strong>总结：</strong> </span></p>
<p>在函数str_replace中NULL 相当于&ldquo;&rdquo;（空字符），&quot;&quot;相当于&quot; &quot; 。其它都是正常的。</p>
<p>通过本文中的例子，可以看出Sybase ASE中存在一些不太符合常规习惯的地方。</p>
<p>&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;-<br />
	&mdash;- 本文为andkylee个人原创，请在尊重作者劳动成果的前提下进行转载；<br />
	&mdash;- 转载务必注明原始出处 :<a href="../" target="_blank" title="http://www.dbainfo.net"> http://www.dbainfo.net</a><br />
	&mdash;- 关键字：str_replace 15.0 null 空格 替换<br />
	&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;-</p>
<div style="clear: both; margin: 10px 0pt; border: 1px dashed rgb(153, 153, 153); font-size: 12px; padding: 5px 10px;">
<li>本文链接地址：<a href="https://www.dbainfo.net/ase-replace-space-character-with-null-by-str_replace-function.htm">https://www.dbainfo.net/ase-replace-space-character-with-null-by-str_replace-function.htm</a>；</li>
<li>本文为dbainfo个人原创，请在尊重作者劳动成果的前提下进行转载；</li>
<li>转载务必注明原始出处 : <a href="https://www.dbainfo.net/">Sybase数据库技术，数据库恢复专家</a>；</li>
<li>对《<a href="https://www.dbainfo.net/ase-replace-space-character-with-null-by-str_replace-function.htm">ASE15.0.3中替换字符串函数str_replace的奇怪的用法</a>》有何疑问或见解，请在本文下方发表；</li>
<li>对网站还有其他问题或建议，请提交在<a href="https://www.dbainfo.net/messages" target="_blank">留言板</a>，谢谢！</li>
</div>
<h2  class="related_post_title">相关文章</h2><ul class="related_post"><li>2010-04-27 -- <a href="https://www.dbainfo.net/sybase-ase15-replace-string-funciton-str_replace.htm" title="ASE12.x与15.x中的字符串替换功能的实现">ASE12.x与15.x中的字符串替换功能的实现</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>https://www.dbainfo.net/ase-replace-space-character-with-null-by-str_replace-function.htm/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ASE12.x与15.x中的字符串替换功能的实现</title>
		<link>https://www.dbainfo.net/sybase-ase15-replace-string-funciton-str_replace.htm</link>
		<comments>https://www.dbainfo.net/sybase-ase15-replace-string-funciton-str_replace.htm#comments</comments>
		<pubDate>Tue, 27 Apr 2010 11:07:29 +0000</pubDate>
		<dc:creator>dbainfo</dc:creator>
				<category><![CDATA[Sybase ASE]]></category>
		<category><![CDATA[ASE]]></category>
		<category><![CDATA[str_replace]]></category>
		<category><![CDATA[stuff]]></category>

		<guid isPermaLink="false">http://www.dbainfo.net/?p=690</guid>
		<description><![CDATA[在ASE12.x版本中Sybase没有提供类似replace的函数。在ASE15.x版本中添加了替换某个字符串中指定的字符为另一个字符的函数：str_replace。 使用方法为： 1&#62; select str_replace(&#39;aaaa&#124;bbb&#124;ccc&#124;&#39;,&#39;&#124;&#39;,&#39;;&#39;) 2&#62; go &#160;------------- &#160;aaaa;bbb;ccc; 上面的例子为将字符串aaaa&#124;bbb&#124;ccc&#124;中的&#124;替换为； 但是，在ASE12.x中需要上面上面的功能，就稍微一点麻烦了。需要结合两个函数:charindex,stuff来循环替换。 两个函数的用法分别为： &#160;charindex - Returns an integer representing the starting position of an expression. &#160;&#160;&#160; charindex(expression1, expression2) &#160;&#160; &#160;stuff - Returns the string formed by deleting a specified number of characters from one string and replacing them with another string. &#160;&#160;&#160; stuff(char_expr1&#124;uchar_expr1, start, length,char_expr2&#124;uchar_expr2) 实现的算法为： [...]]]></description>
			<content:encoded><![CDATA[<p>在ASE12.x版本中Sybase没有提供类似replace的函数。在ASE15.x版本中添加了替换某个字符串中指定的字符为另一个字符的函数：<strong>str_replace</strong>。<br />
	使用方法为：</p>
<blockquote>
<p>
		1&gt; select str_replace(&#39;aaaa|bbb|ccc|&#39;,&#39;|&#39;,&#39;;&#39;)<br />
		2&gt; go</p>
<p>		&nbsp;-------------<br />
		&nbsp;aaaa;bbb;ccc;</p>
</blockquote>
<p>上面的例子为将字符串aaaa|bbb|ccc|中的|替换为；</p>
<p>
	但是，在ASE12.x中需要上面上面的功能，就稍微一点麻烦了。需要结合两个函数:charindex,stuff来循环替换。<br />
	两个函数的用法分别为：</p>
<p>	&nbsp;charindex - Returns an integer representing the starting position of an expression.<br />
	&nbsp;&nbsp;&nbsp; charindex(expression1, expression2)<br />
	&nbsp;&nbsp; <br />
	&nbsp;stuff - Returns the string formed by deleting a specified number of characters from one string and replacing them with another string.<br />
	&nbsp;&nbsp;&nbsp; stuff(char_expr1|uchar_expr1, start, length,char_expr2|uchar_expr2)</p>
<p>	实现的算法为： </p>
<blockquote>
<p>
		declare @my_var char(25)<br />
		select @my_var = &#39;abc|ert|rfrfrf|&#39;<br />
		while charindex(&#39;|&#39;, @my_var) &gt; 0<br />
		begin<br />
		select @my_var = stuff(@my_var, charindex(&#39;|&#39;, @my_var), 1, &#39;;&#39;)<br />
		end<br />
		select @my_var </p>
</blockquote>
<p>
	执行结果为：</p>
<blockquote>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1&gt; declare @my_var char(25)<br />
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2&gt; select @my_var = &#39;abc|ert|rfrfrf|&#39;<br />
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3&gt; while charindex(&#39;|&#39;, @my_var) &gt; 0<br />
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 4&gt; begin<br />
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 5&gt; select @my_var = stuff(@my_var, charindex(&#39;|&#39;, @my_var), 1, &#39;;&#39;)<br />
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 6&gt; end<br />
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 7&gt; select @my_var<br />
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 8&gt; go<br />
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -------------------------<br />
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; abc;ert;rfrfrf;</p>
</blockquote>
<p>&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;-<br />
	&mdash;- 本文为andkylee个人原创，请在尊重作者劳动成果的前提下进行转载；<br />
	&mdash;- 转载务必注明原始出处 :<a href="../" target="_blank" title="http://www.dbainfo.net"> http://www.dbainfo.net</a><br />
	&mdash;- 关键字：替换&nbsp; 字符串&nbsp; replace str_replace stuff charindex<br />
	&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;-</p>
<div style="clear: both; margin: 10px 0pt; border: 1px dashed rgb(153, 153, 153); font-size: 12px; padding: 5px 10px;">
<li>本文链接地址：<a href="https://www.dbainfo.net/sybase-ase15-replace-string-funciton-str_replace.htm">https://www.dbainfo.net/sybase-ase15-replace-string-funciton-str_replace.htm</a>；</li>
<li>本文为dbainfo个人原创，请在尊重作者劳动成果的前提下进行转载；</li>
<li>转载务必注明原始出处 : <a href="https://www.dbainfo.net/">Sybase数据库技术，数据库恢复专家</a>；</li>
<li>对《<a href="https://www.dbainfo.net/sybase-ase15-replace-string-funciton-str_replace.htm">ASE12.x与15.x中的字符串替换功能的实现</a>》有何疑问或见解，请在本文下方发表；</li>
<li>对网站还有其他问题或建议，请提交在<a href="https://www.dbainfo.net/messages" target="_blank">留言板</a>，谢谢！</li>
</div>
<h2  class="related_post_title">相关文章</h2><ul class="related_post"><li>2019-04-12 -- <a href="https://www.dbainfo.net/sybase-ase-asa-recovery-service.htm" title="Sybase ASE及SQL Anywhere数据库修复专家">Sybase ASE及SQL Anywhere数据库修复专家</a> (0)</li><li>2015-01-16 -- <a href="https://www.dbainfo.net/one-way-to-recover-master-configuration-area.htm" title="master数据库配置区域(configuration area)损坏的一种修复方法">master数据库配置区域(configuration area)损坏的一种修复方法</a> (1)</li><li>2014-10-10 -- <a href="https://www.dbainfo.net/ase-object-name-length-limit.htm" title="ASE对象名称长度限制">ASE对象名称长度限制</a> (0)</li><li>2014-06-09 -- <a href="https://www.dbainfo.net/count_all_tables_rowcount.htm" title="统计用户数据库内所有表的行数">统计用户数据库内所有表的行数</a> (0)</li><li>2013-11-19 -- <a href="https://www.dbainfo.net/showplan_html_format.htm" title="以HTML格式显示ASE查询计划">以HTML格式显示ASE查询计划</a> (0)</li><li>2013-11-11 -- <a href="https://www.dbainfo.net/ase-15-0-3-for-windows-installation5.htm" title="Sybase ASE 15.0.3 for windows平台安装文档 – 5、安装后的配置工作">Sybase ASE 15.0.3 for windows平台安装文档 – 5、安装后的配置工作</a> (1)</li><li>2013-11-11 -- <a href="https://www.dbainfo.net/ase-15-0-3-for-windows-installation4.htm" title="Sybase ASE 15.0.3 for windows平台安装文档 – 4、安装Sybase数据库服务">Sybase ASE 15.0.3 for windows平台安装文档 – 4、安装Sybase数据库服务</a> (0)</li><li>2013-11-11 -- <a href="https://www.dbainfo.net/ase-15-0-3-for-windows-installation3.htm" title="Sybase ASE 15.0.3 for windows平台安装文档 – 3、升级到 Sybase 15.0.3 ESD #3">Sybase ASE 15.0.3 for windows平台安装文档 – 3、升级到 Sybase 15.0.3 ESD #3</a> (0)</li><li>2013-11-11 -- <a href="https://www.dbainfo.net/ase-15-0-3-for-windows-installation2.htm" title="Sybase ASE 15.0.3 for windows平台安装文档 – 2、安装Sybase ASE程序">Sybase ASE 15.0.3 for windows平台安装文档 – 2、安装Sybase ASE程序</a> (0)</li><li>2013-11-11 -- <a href="https://www.dbainfo.net/ase-15-0-3-for-windows-installation1.htm" title="Sybase ASE 15.0.3 for windows平台安装文档 – 1、安装前的准备工作">Sybase ASE 15.0.3 for windows平台安装文档 – 1、安装前的准备工作</a> (1)</li></ul>]]></content:encoded>
			<wfw:commentRss>https://www.dbainfo.net/sybase-ase15-replace-string-funciton-str_replace.htm/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
