15,792,870 members
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 2:56am
v3
[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

Solution 1

astrovirgin 29-Aug-13 7:58am
Thanks for the reply but this link is not solving the purpose.

Solution 2

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);

v2
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
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.