Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
I want to replace a combination of string + operator + digit in a mathematical expression. For example, if I have a string 'Hi, The Current Date is: @CurrentDate+2. Thanks', i want it to be replaced like this- 'Hi, The Current Date is: '31 Aug 2013'. Thanks' when current date is 29 Aug. Can anyone help me to find the best approach to accomplish it? Thanks

Note:
1. The digit to add (which is 2 here) can be any number.
2. There could be space after @CurrentDate i.e: @CurrentDate + 5
3. There could be multiple @CurrentDate in the string with different digits. For Example:
'Hi, The Current Date is: @CurrentDate+2. Please visit again on @CurrentDate + 11'
Posted
Updated 29-Aug-13 1:56am
v3
Comments
[no name] 29-Aug-13 7:27am    
Sorry... the "best approach" to what? Adding 2 days the to the current day? Replacing some text in a string? Concatenating a couple of string together? These are all built in functions (at least in SQL Server) so there is nothing tricky about it. What have you tried? Where are you stuck?
astrovirgin 29-Aug-13 7:51am    
Actually the digit to add (which is 2 here) is not hard coded it can vary to any number. The main problem is to find that digit.
astrovirgin 29-Aug-13 7:57am    
I have added a section Note in the question. Please check.

Check this link[^]
 
Share this answer
 
Comments
astrovirgin 29-Aug-13 7:58am    
Thanks for the reply but this link is not solving the purpose.
Hi,

You can do like bellow sql code...

Declare @str1 varchar(100)
Set @str1='Hi, The Current Date is: @CurrentDate. Thanks'
Declare @num int= null
--SET @num=2
if @num is null
BEGIN
SET @num=0
END
Declare @CurrentDate varchar(max)
Set @CurrentDate=CONVERT(VARCHAR(11),GETDATE()+@num,106)
SELECT REPLACE(@str1,'@CurrentDate',@CurrentDate);
 
Share this answer
 
v2
Comments
astrovirgin 29-Aug-13 7:50am    
Thanks for the reply but this digit 2 is not hard coded it can vary to any number. The main problem is to find that digit.
astrovirgin 29-Aug-13 7:57am    
I have added a section Note in the question. Please check.
Shobhana.n 29-Aug-13 8:36am    
Then you can make that digit as a variable and can be do as following code
Declare @str1 varchar(100)
Set @str1='Hi, The Current Date is: @CurrentDate. Thanks'
Declare @num int= null
--SET @num=2
if @num is null
BEGIN
SET @num=0
END
Declare @CurrentDate varchar(max)
Set @CurrentDate=CONVERT(VARCHAR(11),GETDATE()+@num,106)
SELECT REPLACE(@str1,'@CurrentDate',@CurrentDate);
astrovirgin 29-Aug-13 9:23am    
I don't have the control over the string. The format can not be changed so the string would always return as 'Hi, The Current Date is: @CurrentDate+2. Thanks'. Actually the main problem is to extract the digit for the string.

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