Solved

Extract part of data in a field

Posted on 2013-06-05
3
632 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
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…

679 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