SQLTeam.com | Weblogs | Forums

How to slect first name only?



There is a column for Name with data type varchar 50 the input data is like Alok Kumar, Sunita kuamri, Rohit seth Like that. The column contains the data as Combination of First name and second name.
I would like to write a select query for selecting first part of name like Alok, Sunit, Rohit and so on only, ignoring the second part of name. Please help me.


select left(Name, charindex(Name, ' ')-1)


Keep following string part function in database and easily get any part of string.


Parameters in charindex are switched. Also if you want to handle situations where only firstname exists, do this:

select left(Name,charindex(' ',Name+' ')-1)

@gbritton as always, nice to see short and effective solutions :thumbsup:


@lionofdezert I think you are missing "ml" at the end of your link


@bitsmed good catch! Clearly I needed more coffee!!