我想在SQL中将’&’字符替换为’&’。

huangapple go评论57阅读模式
英文:

I want to replace '&' character with '&' in SQL

问题

以下是翻译好的部分:

这是一个特殊需求,他们希望将此结果用于部署。请查找所有DDL和DML命令以及我的尝试。

CREATE TABLE my_table (
  my_column VARCHAR2(100)
);

INSERT INTO my_table (my_column) VALUES ('Apples & Bananas');

SELECT REPLACE(my_column, '&', '&') AS modified_column
FROM my_table;

SELECT REPLACE(my_column, CHR(38), '&') AS modified_column
FROM my_table;

请提供一个SQL查询以获得结果。

我得到的输出:

MY_COLUMN
Apples & Bananas

我想要的输出:

MODIFIED_COLUMN
Apples & Bananas
英文:

This is special requirnment this result they want to use for there deployment .please find all ddl and dml commands with my trials

CREATE TABLE my_table (
  my_column VARCHAR2(100)
);

INSERT INTO my_table (my_column) VALUES ('Apples & Bananas');

SELECT REPLACE(my_column, '&', '&') AS modified_column
FROM my_table;

SELECT REPLACE(my_column, CHR(38), '&') AS modified_column
FROM my_table;

Please suugest a sql query which will give result

Output I am getting:

MY_COLUMN
Apples & Bananas

Output I want:

MODIFIED_COLUMN
Apples & Bananas

答案1

得分: 2

你的目标字符 & 和替换字符 & 是相同的。替换应该是 &

SELECT REPLACE(my_column, ''&'', ''&'') AS modified_column
FROM my_table;

查看 在线演示

但实际上,你不应该以这种方式进行 HTML 转义。相反,应该使用具有适当的 HTML 转义函数的编程语言,例如 JavaScript 或 Python。

英文:

Your target character & and replacement & are the same thing. The replacement should be &:

<!-- language: sql -->

SELECT REPLACE(my_column, &#39;&amp;&#39;, &#39;&amp;amp;&#39;) AS modified_column
FROM my_table;

See live demo.

But really you shouldn't be doing HTML escaping this way. Rather, use a programming language which has a proper HTML escaping function, such as JavaScript or Python.

答案2

得分: 0

SQL> set define off
SQL> CREATE TABLE my_table
2 (
3 my_column VARCHAR2 (100)
4 );

Table created.

SQL> INSERT INTO my_table (my_column)
2 VALUES ('Apples & Bananas');

1 row created.

SQL> SELECT * FROM my_table;

MY_COLUMN

Apples & Bananas

SQL> UPDATE my_table
2 SET my_column = REPLACE (my_column, '&', '&amp;');

1 row updated.

SQL> SELECT * FROM my_table;

MY_COLUMN

Apples &amp; Bananas

SQL>

英文:

replace should work OK.

SQL&gt; set define off
SQL&gt; CREATE TABLE my_table
  2  (
  3     my_column   VARCHAR2 (100)
  4  );

Table created.

SQL&gt; INSERT INTO my_table (my_column)
  2       VALUES (&#39;Apples &amp; Bananas&#39;);

1 row created.

SQL&gt; SELECT * FROM my_table;

MY_COLUMN
--------------------------------------------------------------------------------
Apples &amp; Bananas

SQL&gt; UPDATE my_table
  2     SET my_column = REPLACE (my_column, &#39;&amp;&#39;, &#39;&amp;amp;&#39;);

1 row updated.

SQL&gt; SELECT * FROM my_table;

MY_COLUMN
--------------------------------------------------------------------------------
Apples &amp;amp; Bananas

SQL&gt;

答案3

得分: 0

你尝试过这样吗:

SELECT REPLACE(my_column, '&', '&') AS modified_column FROM my_table;

因为您当前的替换字符与'&'相同。所以,请使用'&'代替。

英文:

Have you tried this:

SELECT REPLACE(my_column, &#39;&amp;&#39;, &#39;&amp;amp;&#39;) AS modified_column FROM my_table;

as your current and replace characters are same '&'. So, use '&amp;' instead.

答案4

得分: 0

尝试将字符串值中的 '&' 分离,如下所示:

SELECT REPLACE(my_column, '&#39;&amp;&#39;', '&#39;&amp;&amp;&#39;') AS modified_column
FROM my_table

结果

英文:

Try to separate '&' in the String value like:

SELECT REPLACE(my_column, &#39;&amp;&#39;, &#39;&amp;&#39;||&#39;amp;&#39;) AS modified_column
FROM my_table

Result

huangapple
  • 本文由 发表于 2023年6月13日 14:01:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76462047.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定