对 SQL oracle 中的特定列使用 replace with select 语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19941814/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using replace with select statement for a specific column in SQL oracle
提问by user2475677
Lets say I have a following table named INFO:
假设我有一个名为 INFO 的下表:
NAME CITY PROFESSION
A New-Mexico Software-Developer
B Tampa Software-Analyst
I only wish to replace the "-" in the PROFESSION column with a '/'. What should be the query to display ALL contents of the table INFO , with the above mentioned change?
我只想将 PROFESSION 列中的“-”替换为“/”。显示表 INFO 的所有内容的查询应该是什么,上面提到的变化?
I tried :
我试过 :
SELECT REPLACE(PROFESSION,'-','/') , * from INFO;
But this does not work.
但这不起作用。
回答by Przemyslaw Kruglej
When you have any other columns in your SELECT
list, you can't use plain asterisk sign (*
). You have to use table name or alias with it:
如果SELECT
列表中有任何其他列,则不能使用纯星号 ( *
)。您必须使用表名或别名:
SELECT REPLACE(PROFESSION,'-','/') , info.* from INFO;
What you want, I guess, is:
我猜你想要的是:
SELECT name, city, REPLACE(PROFESSION,'-','/') AS profession FROM info;
Test:
测试:
CREATE TABLE info (
name VARCHAR2(20),
city VARCHAR2(20),
profession VARCHAR2(20)
);
INSERT INTO info VALUES ('A', 'New-Mexico', 'Software-Developer');
INSERT INTO info VALUES ('B', 'Tampa', 'Software-Analyst');
COMMIT;
SELECT name, city, REPLACE(PROFESSION,'-','/') AS profession FROM info;
Output:
输出:
NAME CITY PROFESSION -------------------- -------------------- -------------------- A New-Mexico Software/Developer B Tampa Software/Analyst