Link to home
Start Free TrialLog in
Avatar of Frosty555
Frosty555Flag for Canada

asked on

Regular expression to trim prefix from a name

Is it possible to do this with a regular expression replacement?

I have a SQL string and it looks something like this:

SELECT field1, field2, field3
FROM dbo.BLAH_ABC_TABLE1
INNER JOIN dbo.FOO_BAR_TABLE2 on dbo.FOO_BAR_TABLE2.myfield = field1
INNER JOIN dbo.ABC_TABLE3 on dbo.ABC_TABLE3.myfield2 = field1
ORDER BY dbo.BLAH_ABC_TABLE1.myfield

Anywhere where there is "dbo.XXX_YYY_ZZZZ" I need to replace it with "dbo.ZZZZ". e.g.
      "dbo.AAA_BBB" gets changed to "dbo.BBB"
      "dbo.AAA_BBB_CCC_DDD" gets changed to "dbo.DDD"
      "dbo.TEST" remains unchanged.

So for example, the above string would be converted into

SELECT field1, field2, field3
FROM dbo.TABLE1
INNER JOIN dbo.TABLE2 on dbo.TABLE2.myfield = field1
INNER JOIN dbo.TABLE3 on dbo.TABLE3.myfield2 = field1
ORDER BY dbo.TABLE1.myfield
Avatar of drmweaver
drmweaver

Why not use the declare for a variable and then add if statements. If you do this within a sp you can call it and pass in the DB Name you want to use.
ASKER CERTIFIED SOLUTION
Avatar of amit_g
amit_g
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial