0

How to return first letter of first name and last name. I already know the substring and the left but if it has 2 names with different number of characters, it gives me wrong values.

8
  • Single column with delimited values?
    – Srini V
    Mar 9, 2018 at 12:19
  • 2
    some sample data will be appreciated
    – Ven
    Mar 9, 2018 at 12:21
  • Could you please tell the scenario if it has 2 names with different characters? Mar 9, 2018 at 12:21
  • Which SQL Server version would help too. Could you have someone with 3 names, what about double barrelled? I.e. George Walker Bush and David Lloyd-George.
    – Thom A
    Mar 9, 2018 at 12:22
  • 1
    Add Malcolm McDowell, Conan O'Brien, Jonathan Rhys-Davies and Carl van der Walt to the sample data!
    – jarlh
    Mar 9, 2018 at 12:23

1 Answer 1

5

Something like this:

declare @a varchar(20)

set @a = 'John Smith'

select LEFT(@a,1) + substring(@a, charindex(' ', @a)+1 , 1)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.