I'm trying to split a string in MSSQL by only the first whitespace Considering here can have 2 spaces in their full name, I have no idea how to do this.
Example:
Henk de Vries
I would like to split it into:
Firstname: Henk
Lastname: de Vries
I'm trying to split a string in MSSQL by only the first whitespace Considering here can have 2 spaces in their full name, I have no idea how to do this.
Example:
Henk de Vries
I would like to split it into:
Firstname: Henk
Lastname: de Vries
 
    
     
    
    try using Patindex
create table #t(name varchar(20))
insert into #t values('Henk de Vries')
select substring(name,1,PATINDEX('% %',name)) as First_name,SUBSTRING(name,patindex('% %',name),LEN(name)) as Last_Name from #t
This is done to fix as said in comments by t-clausen.dk
select left(name,charindex(' ',name+' ')) as First_Name,substring(name,charindex(' ',name+' '),len(name)) as Last_Name from #t
 
    
    Here is an example that will compare this answer to the chosen answer, note that the chosen answer has bugs when there is just 1 name (I know most people have combined names, but this is not a perfect world):
SELECT 
  LEFT(name, charindex(char(32), name + char(32))) Firstname,
  STUFF(name, 1, charindex(char(32), name), '') LastName,
  -- included example from accepted answer
  substring(name,1,PATINDEX('% %',name)) as First_name,
  SUBSTRING(name,patindex('% %',name),LEN(name)) as Last_Name
FROM (values('Henk de Vries'), ('Thomas')) x(name)
Result
Firstname LastName       First_name Last_Name
Henk      de Vries       Henk       de Vries
Thomas                              Thoma
 
    
    DECLARE @Name    VARCHAR(50) = 'Henk de Vries'
SELECT SUBSTRING(@Name, 1, CHARINDEX(' ', @Name) - 1) AS [First Name],
       SUBSTRING(@Name, CHARINDEX(' ', @Name) + 1, LEN(@Name)) AS [Last Name]
 
    
    Use CHARINDEX to find the first space, then LEFT and RIGHT to extract the user's first and last name
left(username, charindex(' ', username)-1) firstname, 
right(username, (len(username)-charindex(' ', username))) lastname
 
    
    