Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3467
  • Last Modified:

Oracle SQL Remove all text after chr(32)

Hello all
This is my first question.

Im pulling data out of an oracle database with asp.

Im trying to construct an email address on the fly from payroll data. Unfortunately the forename field also contains middle names, so i want to remove all text after the space (chr(32)).

Im using this code which does the job if the field contains a middlename (ie a space), but leaves the field blank if only the first name is entered.
I need it to return the fistname irrespective of whether it contains a middle name.

select substr(per_forename,1,(instr(per_forename,chr(32),1,1)-1))as email from mytable

"julian gregson" returns "julian"
"julian" returns ""

Hope you can help
Thanks in advance.

Julian


0
JGregson
Asked:
JGregson
1 Solution
 
TimCotteeCommented:
Hi JGregson,

> select substr(per_forename,1,(instr(per_forename,chr(32),1,1)-1))as email from mytable
How about

select substr(per_forename,1,(instr(per_forename + Chr(32),chr(32),1,1)-1))as email from mytable

As this always ensures that there is a space at the end so your statement will always return everything up to the first space.


Tim Cottee MCSD, MCDBA, CPIM
http://www.timcottee.tk 

Brainbench MVP for Visual Basic
http://www.brainbench.com
0
 
JGregsonAuthor Commented:
Thanks
Had to slightly alter the code to:

select substr(per_forename,1,(instr(concat(per_forename,chr(32)),chr(32),1,1)-1)) as email

Many Thanks
Julian
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now