In access , Australian dates in SQL statements are delimited and formatted as #d/m/yyyy#. For example:
SELECT * FROM mytable WHERE mydate BETWEEN #2/1/2001# AND #4/3/2001# AND more...
I need to grab this SQL statement and massage it so I can present it to SQLite as:
SELECT * FROM mytable WHERE mydate BETWEEN '2001-01-02' AND '2001-03-04' AND more...
The task, then, is to convert #xd/xm/yyyy# to 'yyyy-mm-dd'. The “Hello, World!” of regex examples.
Using vbaregexp with .Global = TRUE, I’m able to replace the delimiters and re-order the elements in one step:
vOldPattern = "#([0-9]{1,2})/([0-9]{1,2})/([0-9]{4})#"
vNewPattern = "'$3-$2-$1'"
This gives the following intermediate result:
SELECT * FROM mytable WHERE mydate BETWEEN '2001-1-2' AND '2001-3-4' AND more...
I thought I could pad all the single digits with a single pass:
vOldPattern = "-(\d[-'])"
vNewPattern = "-0$1"
but this approach padded only the first (month) digit. I gave up and ran through two passes, matching first on
"-(\d-)"
and then on
"-(\d')"
So, three passes to alter the format and tweak the content. Surely there’s a single search/replace command for this task. Can somebody volunteer a more elegant solution?
And, since I have to compile and export the runtime, I guess I can’t use late binding but instead need to load the reference to the MS VBScript Regular Expressions. I’m developing in Win7/32, but the app needs to be backwards-compatible (compatible?) with WinXP. Which version (1.0 or 5.5) of the VBScript RegEx library should I load?
Cheers!
You can use a positive lookahead to get around padding only the first zero – which I think occurred for you as the last
-is consumed in testing for a non-digit for your second-So it can be reduced to two patterns as below
I don’t think it can be done in a single shot but I’d like to see it.
I used
2/12/2001rather than
2/1/2001to prove the replacement handles mixed strings