Vistance
Vistance

Reputation: 345

SQL Replace comma in results without using replace

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 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.

Upvotes: 10

Views: 67645

Answers (3)

Alan
Alan

Reputation: 3002

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:

SELECT REPLACE(MyField, ',', ';') AS NewFieldName
FROM MyTable

Upvotes: 23

Kenneth Aalberg
Kenneth Aalberg

Reputation: 126

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:

SELECT field, REPLACE(field,',',';') FROM my_table;

Upvotes: 0

huMpty duMpty
huMpty duMpty

Reputation: 14460

I believe you don't want to replace the value physically in the table, but ok to replace on select

So you can

Select REPLACE(ColumnName,',',';') 
From TableName

Upvotes: 0

Related Questions