SQLTeam.com | Weblogs | Forums

How to slect first name only?

sql2008

#1

Hi,
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.


#2

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


#3

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


#4

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:


#5

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


#6

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