Connecting Tech Pros Worldwide Help | Site Map

extracting info from LDAP column?

Newbie
 
Join Date: Sep 2008
Location: Washington DC
Posts: 25
#1: Mar 10 '09
I have a table with one of the columns being ldap info. Example value would be:
ou=FOO,ou=BAR,o=XYZ,dc=WORLD

From this I would like to extract the first string, in this case FOO.

My last attempt was:

Expand|Select|Wrap|Line Numbers
  1. SELECT 1+instr(T.ldap,"=") as startpos,
  2.               instr(T.ldap,",") as endpos,
  3.               mid(T.ldap,startpos,endpos-startpos)
  4. FROM myTable T where ldap like @likeStr;
which gives me error message:
ERROR 1054 (42S22): Unknown column 'startpos' in 'field list'


1+instr(T.ldap,"=") evaluates to the correct value -- 4 for the example ldap field value on the top
Same with instr(T.ldap,",") -- 7 in the example

And mid(T.ldap,4,7-4) would give me the correct value -- FOO in the example

However, I can not rely on the numbers 4 and 7.

Any suggestions?
Newbie
 
Join Date: Oct 2008
Posts: 27
#2: Mar 12 '09

re: extracting info from LDAP column?


Use substring() as shown below:

mysql> SELECT SUBSTRING('Quadratically',5);
-> 'ratically'
mwasif's Avatar
Moderator
 
Join Date: Jul 2006
Location: Pakistan
Posts: 718
#3: Mar 12 '09

re: extracting info from LDAP column?


You can not use column aliases in functions, change your query as follows
Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2. mid(T.ldap,1+instr(T.ldap,"="), (instr(T.ldap,",")-1) + instr(T.ldap,"=")) 
  3. FROM myTable T where ldap like @likeStr;
Newbie
 
Join Date: Sep 2008
Location: Washington DC
Posts: 25
#4: Mar 12 '09

re: extracting info from LDAP column?


Quote:

Originally Posted by mwasif View Post

You can not use column aliases in functions, change your query as follows

Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2. mid(T.ldap,1+instr(T.ldap,"="), (instr(T.ldap,",")-1) + instr(T.ldap,"=")) 
  3. FROM myTable T where ldap like @likeStr;

Thanks.
That's what I ended up doing. I just don't like the idea of the same expression being calculated twice.
Reply

Tags
instr, ldap, mid, mysql, select