Home » SQL & PL/SQL » SQL & PL/SQL » Finding positions of all 'X' in string?
Finding positions of all 'X' in string? [message #637116] Mon, 11 May 2015 01:30 Go to next message
Flyfishingninja
Messages: 3
Registered: May 2015
Junior Member
Hi!

I have a looong string containing "one character codes", Let's say I need to find the position of every X in below string.
In other Words, select the positions of all X and get the answer 3, 4, 19, 51 (...and so on).

[PSXXPPPPPPPPPSPSSSXPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPXXXXXXXXXXXXXXX]

How would I go about to do that? Som combination of instr and substr?
I can't get anything to work. Please help me...
Re: Finding positions of all 'X' in string? [message #637117 is a reply to message #637116] Mon, 11 May 2015 01:35 Go to previous messageGo to next message
John Watson
Messages: 8931
Registered: January 2010
Location: Global Village
Senior Member
Welcome to the forum. Please read our OraFAQ Forum Guide and How to use [code] tags and make your code easier to read

What SQL have you tried so far? I would approach the problem with instr and substr, as you have.
Re: Finding positions of all 'X' in string? [message #637119 is a reply to message #637116] Mon, 11 May 2015 01:37 Go to previous messageGo to next message
bugfox
Messages: 18
Registered: October 2010
Junior Member
with qry as (select '[PSXXPPPPPPPPPSPSSSXPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPXXXXXXXXXXXXXXX]' str from dual)
select listagg(lvl, ',') within group(order by lvl) answ
from (select substr(str, level, 1) val,
             level lvl
      from qry
      connect by level <= length(str))
where val = 'X'

[Updated on: Mon, 11 May 2015 01:38]

Report message to a moderator

Re: Finding positions of all 'X' in string? [message #637120 is a reply to message #637116] Mon, 11 May 2015 01:38 Go to previous messageGo to next message
Michel Cadot
Messages: 68647
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Welcome to the forum.
Please read OraFAQ Forum Guide and How to use [code] tags and make your code easier to read.

Always post your Oracle version, with 4 decimals ashe solution depends on this one.
Here's one for 11g+ and one line:
SQL> with
  2    data as (
  3      select '[PSXXPPPPPPPPPSPSSSXPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPXXXXXXXXXXXXXXX]' val
  4      from dual
  5    )
  6  select instr(val, 'X', 1, level) idx
  7  from data
  8  connect by level <= regexp_count(val, 'X')
  9  /
       IDX
----------
         4
         5
        20
        52
        53
        54
        55
        56
        57
        58
        59
        60
        61
        62
        63
        64
        65
        66

18 rows selected.

Re: Finding positions of all 'X' in string? [message #637122 is a reply to message #637116] Mon, 11 May 2015 02:18 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Flyfishingninja wrote on Mon, 11 May 2015 12:00
3, 4, 19, 51

[PSXXPPPPPPPPPSPSSSXPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPXXXXXXXXXXXXXXX]


@Flyfishingninja, If '[' is part of the string, then you need to trim it first or do a instr -1 to the solutions provided. Because, your desired output output doesn't count the occurrence of it.
Re: Finding positions of all 'X' in string? [message #637123 is a reply to message #637120] Mon, 11 May 2015 03:28 Go to previous messageGo to next message
Flyfishingninja
Messages: 3
Registered: May 2015
Junior Member
Michel Cadot wrote on Mon, 11 May 2015 01:38

Always post your Oracle version, with 4 decimals ashe solution depends on this one.


Understood Smile
Re: Finding positions of all 'X' in string? [message #637124 is a reply to message #637123] Mon, 11 May 2015 03:30 Go to previous messageGo to next message
Flyfishingninja
Messages: 3
Registered: May 2015
Junior Member
Both solution above works fine and I'm a happy camper Smile
Thankyou very much indeed!
Re: Finding positions of all 'X' in string? [message #637137 is a reply to message #637124] Mon, 11 May 2015 08:34 Go to previous messageGo to next message
Bill B
Messages: 1971
Registered: December 2004
Senior Member
Just curious, is this homework or did someone make the error of storing flags in a string?
Re: Finding positions of all 'X' in string? [message #637141 is a reply to message #637116] Mon, 11 May 2015 11:42 Go to previous message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
Just for fun, XQUERY & XMLTABLE solutions:

with qry as (select '[PSXXPPPPPPPPPSPSSSXPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPXXXXXXXXXXXXXXX]' str from dual)
select  xmlcast(
                xmlquery(
                         'fn:string-join(
                                         for $e at $i in string-to-codepoints(.)
                                           where codepoints-to-string($e) = "X"
                                           return xs:string($i),
                                         ","
                                        )'
                         passing str
                         returning content
                        )
                as varchar2(60)
               ) x_positions
  from  qry
/

X_POSITIONS
---------------------------------------------------
4,5,20,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66

SQL> 


with qry as (select '[PSXXPPPPPPPPPSPSSSXPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPXXXXXXXXXXXXXXX]' str from dual)
select  column_value x_positions
  from  qry,
        xmltable(
                 'fn:string-join(
                                 for $e at $i in string-to-codepoints(.)
                                   where codepoints-to-string($e) = "X"
                                   return xs:string($i),
                                 ","
                                )'
                         passing str
                )
/

X_POSITIONS
---------------------------------------------------
4,5,20,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66

SQL> 


with qry as (select '[PSXXPPPPPPPPPSPSSSXPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPXXXXXXXXXXXXXXX]' str from dual)
select  column_value x_positions
  from  qry,
        xmltable(
                 'for $e at $i in string-to-codepoints(.)
                    where codepoints-to-string($e) = "X"
                    return $i'
                 passing str
                )
/

X_POSITIONS
------------------
4
5
20
52
53
54
55
56
57
58
59

X_POSITIONS
------------------
60
61
62
63
64
65
66

18 rows selected.

SQL> 


SY.
Previous Topic: PL/SQL Tables
Next Topic: ORA-01847 problem
Goto Forum:
  


Current Time: Fri Apr 26 12:45:30 CDT 2024