Using REPLACE function in SQL Server


This is a small article explaining how to use REPLACE function in SQL queries. In one of my recent projects I had to replace all the - (hyphens) from the search results and i thought share my experience with you.

Syntax for replacing a string is

Code:
REPLACE (string, substring1, substring2)


This function will replace all the occurrences of the sub string 1 with a substring 2.


So replacing � hyphens from a column result will be something like

Code:
SELECT REPLACE(ColumnName,�-�,��) as Col1 FROM TableName


This function can be used in with WHERE condition as well

For example,

Code:
UPDATE TABLENAME SET  REPLACE(ColumnName,�-�,��) =�somevalue�


Hope these examples help in your SQL queries.

Thanks

Commentaires

Posts les plus consultés de ce blog

XAJAX with PHP – The future of web development

XAJAX with PHP – The future of web development

Database connection pooling in ADO.Net