0

How to display the details of employee whose name contains the same characters at the start and end position of their name?

2
  • Smells like homework ... Commented Jun 21, 2010 at 8:12
  • homework hint: lookup the instr function and see if you can use that somehow in your query or maybe do something clever with regexp. Commented Jun 21, 2010 at 8:45

1 Answer 1

3

There are two ways to do this using SUBSTR() to identify a portion of the ENAME. The more orthodox approach works on the basis that passing a negative value as the offset counts from the end of the string:

SQL> select ename
  2  from emp
  3  where substr(ename,1,1) = substr(ename,-1,1)
  4  /

ENAME
----------
TROMBONIST

SQL>

Just for grins, I include the second approach which uses the undocumented REVERSE() function:

SQL> select ename, reverse(ename)
  2  from emp
  3  where substr(ename,1,1) = substr(reverse(ename),1,1)
  4  /

ENAME      REVERSE(EN
---------- ----------
TROMBONIST TSINOBMORT

SQL>

In 10g and higher we can also be solve this with regular expressions:

SQL> select ename
  2  from emp
  3  where regexp_substr(ename,'^.') = regexp_substr(ename,'.$')
  4  /

ENAME
----------
TROMBONIST

SQL>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.