10

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.

2
  • Some example would be helpful. Commented Dec 1, 2014 at 16:20
  • 5
    Out of interest, why not use REPLACE, as it does exactly what you're asking for? Commented Dec 1, 2014 at 16:20

3 Answers 3

23

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
Sign up to request clarification or add additional context in comments.

1 Comment

Don't forget the As keyword! Can be a little confusing to some without.
0

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

Comments

0

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;

1 Comment

Check your Syntax on Replace() - only 2 parameters?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.