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
-
1Is the number always 0-9? If so, there's easy ways of doing thisPhilᵀᴹ– Philᵀᴹ2016年11月09日 12:50:12 +00:00Commented Nov 9, 2016 at 12:50
-
Its always a number, but more than likely it will be something like /143/23/153Wheelz– Wheelz2016年11月14日 12:29:42 +00:00Commented Nov 14, 2016 at 12:29
1 Answer 1
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
-
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 errorsWheelz– Wheelz2016年11月14日 12:18:45 +00:00Commented Nov 14, 2016 at 12:18
-
Then you are doing something wrong. Show us the exact query you run.ypercubeᵀᴹ– ypercubeᵀᴹ2016年11月14日 12:30:06 +00:00Commented Nov 14, 2016 at 12:30
-
SELECT substring_index( substring_index(cc.path),'/',2),'/',-1) as path FROM mdl_course_categories as cc;Wheelz– Wheelz2016年11月15日 10:00:53 +00:00Commented 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.ypercubeᵀᴹ– ypercubeᵀᴹ2016年11月15日 10:05:43 +00:00Commented Nov 15, 2016 at 10:05 -
lang-sql