Solved

Extract part of data in a field

Posted on 2013-06-05
3
621 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
Comment Utility
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
Comment Utility
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
Comment Utility
Work great. though i did some modifications to suit what i wanted. Many thanks
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Suggested Solutions

This article describes some very basic things about SQL Server filegroups.
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…
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now