SQL 在结果中替换逗号而不使用替换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27232581/
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
SQL Replace comma in results without using replace
提问by Vistance
I feel like this should be simple enough to do, but have not found any solutions that didn't use replace so far. I have the following select statement I am running, and for some of the columns there are commas separating the values. I would like to replace these commas with semicolons, however I only want to do it in the select statement. I don't want it to alter the values in the tables at all. This is not a one off statement either, or I'd just replace all the commas with semicolons and then revert back.
我觉得这应该很简单,但到目前为止还没有找到任何不使用替换的解决方案。我正在运行以下选择语句,对于某些列,值之间用逗号分隔。我想用分号替换这些逗号,但是我只想在 select 语句中这样做。我根本不希望它改变表中的值。这也不是一次性声明,或者我只是用分号替换所有逗号,然后再还原。
SELECT a.Category_Id, a.Category_Name, ISNULL(b.Category_Alias, '') as Category_Alias,
ISNULL(b.SUPPORT_NAMES, '') as SUPPORT_NAMES
FROM Categories a
INNER JOIN CategoryInfo b on b.Category_Id=a.Category_Id
For the Category_Alias column, the records are actually stored like CS, Customer Support
and I want that to show up as CS; Customer Support
just for the select statement.
对于 Category_Alias 列,记录实际上是像这样存储的CS, Customer Support
,我希望它CS; Customer Support
仅显示为 select 语句。
回答by Alan
I believe you may be confused as to what the REPLACE function is doing. You can use REPLACE within your SELECT statement without altering the data in the database:
我相信您可能对 REPLACE 函数在做什么感到困惑。您可以在 SELECT 语句中使用 REPLACE 而不更改数据库中的数据:
SELECT REPLACE(MyField, ',', ';') AS NewFieldName
FROM MyTable
回答by huMpty duMpty
回答by Kenneth Aalberg
Most SQL servers implement an inline replace function. Most of them are named replace(), and can also be used in a select statement. Example from MySQL:
大多数 SQL 服务器都实现了内联替换功能。它们中的大多数被命名为replace(),也可以在select 语句中使用。来自 MySQL 的示例:
SELECT field, REPLACE(field,',',';') FROM my_table;