Message Boards Message Boards

0
|
4686 Views
|
3 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Increment a String which is a number by 1 10 times?

Posted 6 years ago

I have a SQL statement that I need to change the year in the statement for a period of years. I need to essentially have let's say 10 SQL statements that increment the year by 1 for 10 times. Part of the SQL statement is:

DateFrom = {d' 2013 - 01 - 01'}, DateTo = {d' 2013 - 01 - 01'},

So the year 2013 need to change to 2014 and than 2015 etc. I have come up with the following but can't figure out how to reiterate it.
Any help would be appreciated...

StringReplace["DateTo={d'2013-01-01'},DateTo={d'2013-01-01'}",  "2013" -> "2014"]

gives

"DateTo={d'2014-01-01'},DateTo={d'2014-01-01'}"

Thanks. David

POSTED BY: David Kerr
3 Replies
Posted 6 years ago

Thank you all very much. That gets me headed in the right direction. I had looked at 4 books and the reference pages but it is hard to find your way through the forest of expressions.

POSTED BY: David Kerr

Have a look to the references StringReplace, RegularExpression, and soon you write something like that

In[4]:= Clear[sql0]
sql0 = "DateTo={d'2013-01-01'},DateTo={d'2013-01-01'}";

In[13]:= StringReplace[sql0, 
   RegularExpression["[0-9]{4}"] :> ToString[ToExpression["$0"] + #]] & /@ Range[10]

Out[13]= {"DateTo={d'2014-01-01'},DateTo={d'2014-01-01'}", \
          "DateTo={d'2015-01-01'},DateTo={d'2015-01-01'}", \
          "DateTo={d'2016-01-01'},DateTo={d'2016-01-01'}", \
          "DateTo={d'2017-01-01'},DateTo={d'2017-01-01'}", \
          "DateTo={d'2018-01-01'},DateTo={d'2018-01-01'}", \
          "DateTo={d'2019-01-01'},DateTo={d'2019-01-01'}", \
          "DateTo={d'2020-01-01'},DateTo={d'2020-01-01'}", \
          "DateTo={d'2021-01-01'},DateTo={d'2021-01-01'}", \
          "DateTo={d'2022-01-01'},DateTo={d'2022-01-01'}", \
          "DateTo={d'2023-01-01'},DateTo={d'2023-01-01'}"}
POSTED BY: Dent de Lion

suggest you look at ToString and StringJoin

POSTED BY: Frank Kampas
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract