Click here to Skip to main content
15,886,742 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
how to find replace a extact match word in sqlserver
SQL
declare strsql varchar(max)
set strsql = The Date Should be between @TransactionDate and @TransactionDate2 
replace(strsql, @TransactionDate, 2014-11-27)



for example in this case if i want replace only TransactionDate with some value in string then it should be replace only TransactionDate but Not TransactionDate2.

regards,
Rajshree
Posted
Updated 27-Nov-14 3:56am
v2
Comments
Sinisa Hajnal 27-Nov-14 2:33am    
And what is the problem? What part of the above replace doesn't work?
rajshreelande 27-Nov-14 8:34am    
problem is @TransactionDate havinf different value and @TransactionDate1 having differnt value when i m trying to replace @TransactionDate it would replace TractionDate2also as
The Date Should be between '2014-11-27'and '2014-11-27'2 and this wrong so dats why i hve to replace the exact match.
André Kraak 27-Nov-14 3:05am    
The quickest way would be to use @TransactionDate1 instead of just @TransactionDate.
rajshreelande 27-Nov-14 8:34am    
Reply
[Modify the comment.] [Delete the comment.]
problem is @TransactionDate havinf different value and @TransactionDate1 having differnt value when i m trying to replace @TransactionDate it would replace TractionDate2also as
The Date Should be between '2014-11-27'and '2014-11-27'2 and this wrong so dats why i hve to replace the exact match.

1 solution

I think your are looking for this

SQL
declare @strsql varchar(max)
set @strsql = 'The Date Should be between TransactionDate and TransactionDate2'
set @strsql=SPACE(1)+@strsql+SPACE(1)


select Rtrim(Ltrim(replace(@strsql, SPACE(1)+'TransactionDate'+SPACE(1),SPACE(1)+'2014-11-27'+SPACE(1))))
 
Share this answer
 
Comments
Sinisa Hajnal 27-Nov-14 9:14am    
Complex, but look right.

@rajshreelande: why couldn't you rename the variable TransactionDate with TransactionDate1? It would make everything easier.
Shweta N Mishra 27-Nov-14 9:18am    
in his example it looks like a variable but i think its not true , He is looking for a string instead.

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