0

I have a table with a field in with data such as:

id | path
1 | /2/9/1 
2 | /2/4/11 
3 | /1/3/11 
4 | /5/3/2 
5 | /2/1/4

I'm wanting to do a select query to show only the first number between the slashes. Can you use a Regex in the select part of a query? If so, what is the syntax to find what I need?

PhilTM
32k10 gold badges86 silver badges108 bronze badges
asked Nov 9, 2016 at 12:48
2
  • 1
    Is the number always 0-9? If so, there's easy ways of doing this Commented Nov 9, 2016 at 12:50
  • Its always a number, but more than likely it will be something like /143/23/153 Commented Nov 14, 2016 at 12:29

1 Answer 1

2

You can use SUBSTRING_INDEX() function for that, no need for regexes:

 SUBSTRING_INDEX( SUBSTRING_INDEX(path, '/', 2), '/', -1) 
answered Nov 9, 2016 at 12:57
5
  • I get the error message 1582: Incorrect parameter count in the call to native function 'SUBSTRING_INDEX' Its fine when I put an actual string in there but trying to do it on a field errors Commented Nov 14, 2016 at 12:18
  • Then you are doing something wrong. Show us the exact query you run. Commented Nov 14, 2016 at 12:30
  • SELECT substring_index( substring_index(cc.path),'/',2),'/',-1) as path FROM mdl_course_categories as cc; Commented Nov 15, 2016 at 10:00
  • The "incorrect parameter count" indicates that you missed a comma or you misplaced parentheses and that's what you did. There is an extra parenthesis after cc.path. Remove it. Commented Nov 15, 2016 at 10:05
  • Thats sorted it. Cheers Commented Nov 15, 2016 at 14:47

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.