x

How To Return Only 1 Entry Per Row When A Column Can Contain Multiples

I have a FORENAME column in my CUSTOMER table which can contain more than one name, for example 'Michael James' below:

select FORENAME from CUSTOMER

FORENAME

Martyn
Hugh
Michael James
John
Frank

How is it possible to query this to return only 1 name per customer (i.e. How do I return in the results 'Michael' INSTEAD OF 'Michael James')?

TIA

more ▼

asked Dec 16, 2009 at 09:37 AM in Default

avatar image

David 2 1
1.4k 57 62 72

(comments are locked)
10|1200 characters needed characters left

2 answers: sort voted first

or

select substring(FORENAME, 1, charindex(char(32), FORENAME+' ')-1)            
from CUSTOMER            
more ▼

answered Dec 17, 2009 at 05:42 AM

avatar image

Madhivanan
1.1k 3 5 9

(comments are locked)
10|1200 characters needed characters left

Got a solution to truncating anything after the space:

select substring(FORENAME, 1, case
when charindex(char(32), FORENAME) > 0 then
charindex(char(32), FORENAME) else len(FORENAME)
end)
from CUSTOMER

more ▼

answered Dec 16, 2009 at 02:21 PM

avatar image

David 2 1
1.4k 57 62 72

(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x1095
x511

asked: Dec 16, 2009 at 09:37 AM

Seen: 1279 times

Last Updated: Dec 16, 2009 at 09:37 AM

Copyright 2018 Redgate Software. Privacy Policy