Skip to content
Advertisement

SQL: ORDER BY using a substring within a specific column… possible?

I have a database whose columns are npID, title, URL, and issue.

Here is an example of two years’ entries:

What I would like to do is be able to sort the results based on substrings within the “issue” column. However, until 2010, the client used seasons as the header and in 2010, they started using quarters. Is there a way in the “ORDER BY” I can provide a list of words to sort by if/when they’re found anywhere in the “issue” value?

I would like the end result to be something like this:

Advertisement

Answer

You can put a CASE statement in the ORDER BY to accomplish this. A better route would be to change the application and table to actually store this relevant data in columns where it belongs when you have the development time to do that.

Order the seasons however you want. You could also order them in a specific way (Q1 followed by Spring, followed by Q2, etc.) by adjusting the CASE statement.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement