Link to home
Start Free TrialLog in
Avatar of Marco Pinero
Marco PineroFlag for Dominican Republic

asked on

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

Avatar of Yuval_Shohat
Yuval_Shohat
Flag of Israel image

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=-
ASKER CERTIFIED SOLUTION
Avatar of NerdsOfTech
NerdsOfTech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Provided RegEx to extract desired text from all three test inputs, presuming that the inputs are always valid SQL.