Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 split value based on space if no space available

Author  Topic 

cplusplus
Aged Yak Warrior

567 Posts

Posted - 2015-01-19 : 12:43:04
I want to split columns value with space, most part of the data i do have doctorname with space and their title. some cases where just the name available with no title. in those cases want to show null value in their title column.



Declare @Sample table (DrName_title varchar(100))
insert @Sample
select 'Perez-Pascual MD' union all
select 'Diaz-Monroig'

Select firstPart(DrName_title) as Doctorname, secondpart(DrName_Title) as Doctor_title from @Sample


Thanks a lot for the helpful info.

viggneshwar
Yak Posting Veteran

86 Posts

Posted - 2015-01-21 : 02:32:34
select DrName_title, substring(DrName_title, nullif(CHARINDEX(' ', DrName_title),0), LEN(DrName_title))
from @Sample

Regards
Viggneshwar A
Go to Top of Page
   

- Advertisement -