Solved

Extract part of data in a field

Posted on 2013-06-05
3
626 Views
Last Modified: 2013-12-25
Hi Experts,

i am interested in extracting part of data in a field. the field data as below

Charles.David Charles David
Tom.Thomas Tom Thomas
Mannasseh.Abdul Mannasseh Abdul

i do not require the data after the space. The desired data is as below

Charles.David
Tom.Thomas
Mannasseh.Abdul

A simple solution is desired
Thanks in advance
0
Comment
Question by:Peter Kiprop
3 Comments
 
LVL 16

Accepted Solution

by:
Surendra Nath earned 500 total points
ID: 39221298
a charindex in combination with the substring will solve this for you

declare @t varchar(100) = 'Charles.David Charles David'
select substring(@t,1,CHARINDEX(' ',@t)-1)

Open in new window

0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39221323
a tiny addition to the above which will avoid a possible problem if no space is found in the string... deliberately add a space, then look for a space. Only the position of the first space is returned by charindex so the result is the same as the above - except it won't error if the actual string has no space in it.

declare @t varchar(100) = 'Charles.DavidCharlesDavid'
select substring(@t,1,CHARINDEX(' ',@t+' ')-1)
0
 
LVL 6

Author Closing Comment

by:Peter Kiprop
ID: 39221361
Work great. though i did some modifications to suit what i wanted. Many thanks
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question