Regular Expression required

Hi folks!

I have spent some days looking for a regular expression which let me get order fields in a SQL SELECT statement.

Examples:

SELECT * FROM TABLE
ORDER BY A, B DESC, C

Open in new window


SELECT * FROM TABLE
ORDER 
BY A, 
B DESC, C

Open in new window


SELECT * FROM TABLE
ORDER 
BY A, 
B DESC, C LIMIT 100 OFFSET 4000

Open in new window


SELECT * FROM TABLE
ORDER 
BY A, 
B DESC, C 
ROWS 4000 TO 4100

Open in new window


For all these SQL sentences I wanna extract this:

A, B DESC, C

Open in new window

Marco PineroEngineerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Yuval_ShohatCommented:
Dear Marco Pinero,
Please supply some further details like:
what results do you receive from the database.
What do you expect to have after applying the regexp?

It may be just me, but it seems that something is missing here in your question and possibly in the way you have things going.
As I see it once you have a query to SQL server, you get the results ordered as you set them in the query. Thus, in your case: order by A then desc B and then by C. Once the query is executed and the SQL has responded you have some kind of data structure according to the fields (columns) that were fetched from the TABLE.
You don't have to have a regexp to get the fields.
Usually I hate using * in queries as using it results in a potential unknown number of fetched fields.
The only case you actually need to use a Regexp is if you receive the results in a string instead of some kind of array \ list \ directory \ dataset, a regexp is irrelevant.
if you do have the results of each row as a string, then I suppose you have some sign of delimitation like  , - tab and so on. In this case, a simple split which all languages has,  will do most of the job.

Since its night I wish you good night. I'm off to sleep.
-=Yuval=-
0
NerdsOfTechTechnology ScientistCommented:
try:
/(?:ORDER\s+BY\s+)(.*?)(?:\s+LIMIT.*|\s+ROWS.*|\s*$)/gm

Open in new window


https://www.regextester.com/?fam=99808
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
NerdsOfTechTechnology ScientistCommented:
Provided RegEx to extract desired text from all three test inputs, presuming that the inputs are always valid SQL.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Regular Expressions

From novice to tech pro — start learning today.