Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3944230
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:52:32+00:00 2026-05-20T00:52:32+00:00

I have a bunch of SQL statements that were originally written to support Oracle

  • 0

I have a bunch of SQL statements that were originally written to support Oracle and use a lot of specific Oracle syntax. Most of this was pretty easy to address using simple regex style find and replace or through the use of JDBC functions. This case, however, is causing me a bit more trouble.

There are a bunch of instances where concatenation is used for values and I’m trying to replace col1 || col2 || col3 to be replaced with CONCAT(col1,col2,col3) style syntax in the case that the database engine is not Oracle.

I know it would be ideal to use some sort of ORM, but that isn’t practical in this case for various reasons.

I have a test app setup to test out some of the cases I am most likely to come into contact with but I am having trouble figuring out how to get it to replace all instance and also a variable number of matches after the ||. The solution as I have it now only matches the last occurence of col||col. Maybe it’s not possible, but I would like to try and exhaust this solution path.

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class regex {
    public static void main(String args[]){
        String sSource = "SELECT col1, col2||word||default_col||another field1, col3 || ' quote test ' || default_1 field2 FROM table order by 1";
        try{
            String pattern ="((\\b\\[a-zA-Z0-9_]+|'.*')\\s*(\\|\\|)\\s*(\\w+))";
            Pattern p = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE|Pattern.MULTILINE|Pattern.DOTALL);
            Matcher m = p.matcher(sSource);
            while(m.find()){
                sSource = m.replaceAll("CONCAT($2 , $4)");
                System.out.println("found match");
            }
            System.out.println(sSource);
        } catch( Exception e) {
            System.out.println("Bad things:" + e.getMessage());
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T00:52:33+00:00Added an answer on May 20, 2026 at 12:52 am

    You cannot really do concat replacement for all || with regular expressions as they aren’t strong enough to handle the SQL-grammar. You can concatenate anything and everything using || including character literals containing ||.

    However, regular expression might be enough in some special cases. It might be easier to divide and conquer the problem e.g.

    1. convert (SELECT|,) <content> || to (SELECT|,) concat(<content> ||

    2. convert || <content> <field> (FROM|,) to , <content>) <field> (FROM|,

    3. convert all remaining || to ,

    Store the result after each phase and apply the rules to that intermediate conversion.

    Something like

    private static String identifierOrString = "[a-zA-Z0-9_\\.\\(\\),]+";
    
    public static void main(String[] args) {
    
      String testCase = "SELECT col1, col2||word||default_col||another field1, col3 || ' quote test ' || default_1 field2 FROM table where 'abc' = col4 || col5 || col6 GROUP BY col7 || col8";
    
      testCase = convertBeginnings(testCase);
      System.out.println("Phase 1: " + testCase);
    
      testCase = convertEndings(testCase);
      System.out.println("Phase 2: " + testCase);
    
      testCase = convertRemainingOperators(testCase);
      System.out.println("Finished: " + testCase);    
    }
    
    private static String convertBeginnings(String testCase) {
      return replace("(SELECT|WHERE|=|<>|like|GROUP BY|\\,)(\\s+)(%s|'[^']*')\\s*\\|\\|", testCase, "%s %s concat(%s ||");
    }
    
    private static String convertEndings(String testCase) { 
      return replace("\\|\\|\\s*(%1$s|'[^']*')\\s*(\\s%1$s)?\\s*((\\,|FROM|GROUP BY|ORDER BY|=|<>|like|$))", testCase,",%s) %s %s");
    }
    
    private static String replace(String regexp, String source , String target) {
      Matcher m = match(regexp, source);
      while(m.find()) {
        source = source.replace(m.group(0), String.format(target, nvl(m.group(1)), nvl(m.group(2)), nvl(m.group(3))));
      }
      return source;
    }
    
    private static String nvl(String value) {
      return null == value ? "" : value;
    }
    
    private static String convertRemainingOperators(String testCase) {
      return testCase.replaceAll("\\|\\|", ",");
    }
    
    private static Matcher match(String regexp, String target ) {
      Pattern p = Pattern.compile(String.format(regexp, identifierOrString), Pattern.CASE_INSENSITIVE|Pattern.MULTILINE|Pattern.DOTALL);
      return p.matcher(target); 
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a great bunch of sql server statements that run on sql server
What I have is a C# windows app that reads a bunch of SQL
I have a simple SQL query in PostgreSQL 8.3 that grabs a bunch of
I've got an IronPython script that executes a bunch of SQL statements against a
I have a SQL statement that is something like this SELECT t1.* CASE WHEN
I have a bunch of SQL statements to execute on a database. (I'm doing
I have a SQL script that inserts data (via INSERT statements currently numbering in
I have a bunch of SQL servers which I periodically performs maintainance on (Windows
I have a bunch of entity class (generated by Linq to SQL) with a
We have an old SQL Server 2000 machine where a bunch of jobs run

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.