1

Possible Duplicate:
Split Function equivalent in tsql?

I have @FirstLastName = 'First Last'.

I need to split string in two variables @FirstName and @LastName.

In @FirstName goes 'First'.

In @LastName goes 'Last'.

Community
  • 1
  • 1
tanasi
  • 1,804
  • 6
  • 37
  • 53

1 Answers1

1

This should do the trick but it's only a quick hack (SQL Fiddle):

SET @FirstName = substring(@FirstLastName
                         , 1
                         , charindex(' ', @FirstLastName)-1)

SET @LastName = substring(@FirstLastName
                        , charindex(' ', @FirstLastName)
                        , 999)

-- if you want to calculate the actual @LastName length,
-- replace 999 with len(@FirstLastName)-charindex(' ', @FirstLastName)+1

For a more elegant and robust way see Split function equivalent in T-SQL?.

Community
  • 1
  • 1
Marek Grzenkowicz
  • 17,024
  • 9
  • 81
  • 111