• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

How to select max row of mixed string/int column with a variable number of leading characters?

 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a column that contains (Abseiling - Level 9, Rock Climbing - Level 3, ...). I want to return the highest level (e.g., Abseiling - Level 9). I have found:



However, this requires the same number of leading characters (I have variable) and only returns the integer (I want "Abseiling - Level 9").

Kind regards,

Glyn
 
Glyndwr Bartlett
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is:


 
Saloon Keeper
Posts: 23515
161
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The real answer is to have a separate numeric column just to hold the level number. It's good that you've mastered the string expression, but converting each candidate from string to number is a lot of overhead for a query.

Commonly, in fact, if you have a limited number of codes, one would create a separate table to hold the category name and level and have the primary table index into it. That can save quite a bit of space in a large database.
 
Marshal
Posts: 26523
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:The real answer is to have a separate numeric column just to hold the level number. It's good that you've mastered the string expression, but converting each candidate from string to number is a lot of overhead for a query.



Not to mention that the expression gets broken when the climbers decide to have "Level 10"...
 
Tim Holloway
Saloon Keeper
Posts: 23515
161
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Crank it up to 11!

Seriously, though, what DBMS supports a SUBSTRING function with these semantics? All I can find are versions that work with character positions, not matches.
 
Willie Smits increased rainfall 25% in three years by planting trees. Tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic