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?
 
NerdsOfTechTechnology ScientistCommented:
try:
/(?:ORDER\s+BY\s+)(.*?)(?:\s+LIMIT.*|\s+ROWS.*|\s*$)/gm

Open in new window


https://www.regextester.com/?fam=99808
0
 
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:
Provided RegEx to extract desired text from all three test inputs, presuming that the inputs are always valid SQL.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.