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 |
|
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 #637122 is a reply to message #637116] |
Mon, 11 May 2015 02:18 |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
Flyfishingninja wrote on Mon, 11 May 2015 12:003, 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 #637141 is a reply to message #637116] |
Mon, 11 May 2015 11:42 |
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.
|
|
|
Goto Forum:
Current Time: Fri Apr 26 12:45:30 CDT 2024
|