4

Oracle and Sql server using different prefix for parameters in parametrized string.

Sql using @p1

ORA using :p1

I would like to use in my SQL just @ and in case that ORA database is used all : character should be replaces with @.

Can you please help me to create regular expression?

Here is some example SQL:

update test_table set text = :p1 where text = 'jana:klara' or some_value = :value or info = 'two'

Similar question and alternative solutions can be found here.

2 Answers 2

2

You can use this pattern for your search search:

(?<=\W):(?=\w+)

For instance:

string output = Regex.Replace(input, @"(?<=\W):(?=\w+)", "@");

Here's the meaning of the pattern:

  • (?<=\W) - The (?<= ... ) syntax declares a positive look-behind. In other words, any match must be preceded by the contents of the look-behind. In this case, it's declaring that matches must be preceded by a non-word character.
  • : - Matches just the colon
  • (?=\w+) - The (?= ... ) syntax declares a positive look-ahead. In other words, any match must be followed by the contents of the look-ahead. In this case, it's declaring that matches must be followed by a one or more word-characters.

See the online demo.

I can't think of any reason why the parameter would be the first thing in the input string, but if that were possible, changing it to (?<=^|\W):(?=\w+) would take care of that situation too.

Sign up to request clarification or add additional context in comments.

2 Comments

I found that replacement for this UPDATE B_BSET SET VALUE_DATE = '14.07.2011' WHERE BATCH_NAME = ':BWAG_ALM2' not working correctly. :BWAG_ALM2 is changed to @BWAG_ALM2. Any idea?
What about this :(?=(?:'[^']*'|[^'\n])*$) ? I think that this is working correctly.
0

Correct regex:

 statement = Regex.Replace(statement, @":(?=(?:'[^']*'|[^'\n])*$)", "@");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.