Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Query to Replace part of string in sql server | Replace function example

May 15, 2012
Introduction:

In this article I will explain what is the replace function in SQL Server and how to use Replace() function in SQL Server.
Description:
In previous post I explained article substring function in SQL server. Now I will explain what is Replace () function in SQL server and how to use it in our applications. In one application I got requirement like replace particular part of the string with another value to achieve this functionality I realized it’s better to use Replace () function.

Replace() function

This function is used to replace the all the occurrences of specified string value with another string value. Generally Replace function will take 3 arguments and that declaration will be like this


Replace('string1','string2','string3')
In this function

1st Argument specifies the string value

2nd Argument specifies required string to be updated in complete string

3rd Argument specifies replace required string value in complete string

Examples of Replace() Function:

EX1:

Declaration of replace function will be like this


SELECT REPLACE(' Dasari Suresh', 'Dasari', 'Aspdotnet')
Output: 'Aspdotnet Suresh'

In above query we declared string as 'Dasari Suresh', and declared string to replace 'Dasari' string value with 'Aspdotnet'string value.

Ex2:

Suppose if we want to use Replace() function in table then our function declaration will be like this


SELECT  REPLACE (ColumnName,CompareText,ReplaceText) as ' Replace' FROM TableName
Ex: SELECT REPLACE(UserName,'Dasari', 'Aspdotnet') as 'Replace' FROM UserInformation

Above query will replace UserName which contains 'Dasari' value with 'Aspdotnet' string value like this

Output:      

Replace
Aspdotnet Suresh
Suresh Aspdotnet

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

0 comments :

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.