简体   繁体   中英

Oracle sql function question

如何在姓名的开头和结尾位置显示姓名相同的员工的详细信息?

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM