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 876369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:25:15+00:00 2026-05-15T11:25:15+00:00

I am attempting to split the ORDER BY statement of a SQL query into

  • 0

I am attempting to split the ORDER BY statement of a SQL query into an array. First inclination is:

order_by.split(',')

but that does not work for order by statements like the following:

SUBSTRING('test',1,3) ASC, SUBSTRING('test2', 2,2 ) DESC

The desired output for the above statement would be:

["SUBSTRING('test',1,3) ASC", "SUBSTRING('test2', 2,2 ) DESC"]

I am fairly certain that it would work if I could match any comma that is not enclosed in parethesis, but I cannot find a way to do that in ruby regex because lookbehind is not supported.

  • 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-15T11:25:16+00:00Added an answer on May 15, 2026 at 11:25 am

    While I suspect there probably is still a way of doing it with regex, you can do it with simple string parsing as well.

    Find all commas in the string, then go forwards or backwards from that point (it won’t matter which if the brackets are balanced correctly), adding one for an open brace and subtracting one for a closed brace. If you don’t have 0 at the end, you’re inside a brace, so it’s not a comma you want.

    Split on all other commas.

    EDIT

    Although the comment about this methodology failing in the case of parentheses enclosed in commas is valid, it may be the case that you’re dealing with queries simple enough not to worry about that. If that is the case, this should work:

    def in_brackets(str,pos)
      cnt = 0
      str[pos,str.length].each_char do |c|
        if c == '('
          cnt += 1
        elsif c == ')'
          cnt -= 1
        end
      end
    
      return cnt != 0
    end
    
    def split_on_some_commas(str) 
      offset = -1
      split_pts = []
    
      while (offset = str.index(",",offset+1))
        if !in_brackets(str,offset)
          split_pts << offset
        end
      end
    
      split_pts << str.length
    
      pos = 0
    
      ret = []
      split_pts.each do |pt|
        ret << str[pos..(pt-1)].strip
        pos = pt+1
      end
    
      return ret
    end
    
    puts split_on_some_commas("SUBSTRING('test',1,3) ASC, SUBSTRING('test2', 2,2 ) DESC, SUBSTRING('test2', 2,2 ) DESC").inspect
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been attempting to split a div into two columns using CSS, but
I am attempting to split a string of integers into an array in JavaScript.
I'm attempting to split up my WCF web services into a few services instead
I'm attempting to split a string into many strings (List) with each one having
I am attempting to push updates into a process via a named pipe, but
I am attempting to query ActiveDirectory via LDAP, but the query may contain spaces
I am attempting to split an image into pieces, lets say for example 16
I'm attempting to create a 2D array of integers that load in data from
I am splitting a line of text into an array then attempting to put
I am attempting to split a list into a series of smaller lists. My

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.