Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

During my SQL Server 2012 Express learning adventures, I came across the REPLACE function taught by the W3School.

My limited understanding of it as follows:
SQL
SELECT REPLACE('SQL Tutorial', 'SQL', 'Code Project');

That you can replace the word(s), in this example the above 'SQL' word to 'Code Project'.

However, I would have used the CASE WHEN function as follows:
SQL
CASE
WHEN fieldname  = 'SQL Tutorial'
THEN 'Code Project Tutorial'
ELSE fieldname
END AS 'fieldname'


The CASE WHEN makes more sense and I'm wondering when would you apply the REPLACE function? Could the REPLACE function be used instead of the CASE WHEN function?

Suppose, the following could be used but then again what would be the point of it?

SQL
CASE
WHEN fieldname = 'SQL Tutorial'
THEN REPLACE('SQL Tutorial', 'SQL', 'Code Project')
ELSE fieldname
END AS 'fieldname' 


I'm just a little curious.
Thank you.

What I have tried:

I am learning SQL Server 2012 Express
Posted
Updated 9-Aug-18 2:15am
v2

Replace is one the - extremely limited - string manipulation functions of SQL, and doesn;t work like your CASE WHEN example.
Yes,
SELECT REPLACE('SQL Tutorial', 'SQL', 'Code Project');
will give you Code Project Tutorial, and so will
CASE
WHEN fieldname  = 'SQL Tutorial'
THEN 'Code Project Tutorial'
ELSE fieldname
END AS 'fieldname'
But ... the REPLACE version will also change "My SQL Tutorial" to "My CodeProject Tutorial" and "This is an introduction to the MySQL database" to "This is an introduction to the MyCode Project database" where the CASE WHEN version won't change anything.

Your "more sense" version using CASE WHEN is only "clearer" because you are using fixed items to provide your results - and almost all SQL does not do that, it works with generics rather than fixed output.
 
Share this answer
 
Comments
Skipton Corp 9-Aug-18 8:01am    
Thank you OriginalGriff for the clarity.
OriginalGriff 9-Aug-18 8:17am    
You're welcome!
REPLACE is a string function that allows to replace particular characters in a particular string.

CASE WHEN is like a conditional function that takes decisions base on the conditions provided in the statement.

In your case the below given code can be used because the condition in robust or static .

CASE
WHEN fieldname  = 'SQL Tutorial'
THEN 'Code Project Tutorial'
ELSE fieldname
END AS 'fieldname'


But the other solution can be used if you are checking if the fieldname contains "SQL" sub string.

For example as follows:

CASE
WHEN CHARINDEX('SQL',fieldname)>0
THEN REPLACE(fieldname, 'SQL', 'Code Project')
ELSE fieldname
END AS 'fieldname' 


In the above code the condition is dynamic and it is more feasible to use a replace function.
 
Share this answer
 
Comments
Skipton Corp 14-Aug-18 6:55am    
Thank you for the solution.
Zalak Artist 16-Aug-18 7:28am    
Welcome :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900