Google
Information Storage and Retrieval: Palindromes (SQL Puzzle 9)

Pages

Wednesday, July 9, 2008

Palindromes (SQL Puzzle 9)

Problem: Given a string, find whether its a palindrome or not by SQL query.
Solution:
select decode(count(distinct ascii(straight.ch)-ascii(revers.ch)),1,'YES','NO') is_palindrome
from
(select ch,rownum rownumber
from
(
select substr(name,level,1) ch,rownum rn from
(
select &palindrome name from dual
)
connect by prior name=name
and level<=length(name)
and prior dbms_random.string('p',10) is not null
)) straight,
(select ch,rownum rownumber
from
(
select ch
from
(
select substr(name,level,1) ch,rownum rn from
(
select &palindrome name from dual
)
connect by prior name=name
and level<=length(name)
and prior dbms_random.string('p',10) is not null
)
order by rn desc
)
) revers
where straight.rownumber=revers.rownumber

1 comment:

Anonymous said...

This can also be done using
SELECT DECODE('&enter',REVERSE('&enter'),'Y','N') FROM DUAL

Cheers
Kartik