Timothy Bolton
Timothy Bolton

Reputation: 75

Trying to return only part of a string in SQL

I currently have the following query:

SELECT SPEAKERNAME, 
       SUBSTR(SPEAKERADDRESS, CHARINDEX('-', SPEAKERADDRESS), 4)
  FROM SPEAKER;

I am tyrying to only return the postcode from SPEAKERADDRESS and the postcode is always after '-' but i am getting an invalid identifier (ORA-00904) error for CHARINDEX

Upvotes: 0

Views: 3945

Answers (1)

Mr. Bhosale
Mr. Bhosale

Reputation: 3106

You can use SUBSTR and INSTR:

SELECT SPEAKERNAME, 
       SUBSTR(SPEAKERADDRESS, instr(SPEAKERADDRESS, '-')+1 ,4)
  FROM SPEAKER;

Check Demo.

Upvotes: 1

Related Questions