Find numbers in a TEXT, and add a specific number to each number you find

35 Views Asked by At

I have to create a long SQL expression, where I work with Dates in Double format.

For example

SELECT II.Amount from IssuedInvoices II where II.DocDate>=44000 and II.DocDate<=44030

For the project that I am creating, it would save me hours to get a script in any programming language than finds number 44000 and adds 365 to it and then it finds 44030 and adds 365 to it.

I give the script the SQL expression and the script would find all numbers in the SQL expression and add a specific number to each number it finds and then the return value would be the updated SQL expression. Thank you very much.

1

There are 1 best solutions below

0
enzo On

You can use a mapped regular expression. An example in Python:

import re

old_query = "SELECT II.Amount from IssuedInvoices II where II.DocDate>=44000 and II.DocDate<=44030"
new_query = re.sub('[0-9]+', lambda x: str(int(x.group()) + 365), old_query)
print(new_query)

outputs

SELECT II.Amount from IssuedInvoices II where II.DocDate>=44365 and II.DocDate<=44395

Here's the Java version and here's the Javascript version.