split value into two columns

  • Hi All,

    I want to display full name column as FirstName and LastName.

    Eg if Full Name is Abhas Jadhav then i want to convert it into FirstName- Abhas and LastName - Jadhav

    i.e. Before Space is FirstName and After Space is LastName.

    Thanks,

    Abhas,

  • You need a splitter function: http://www.sqlservercentral.com/articles/Tally+Table/72993/

    -- Gianluca Sartori

  • What have you tried to do so far to make it work?

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • What happens if you have full names with more than one space in them?

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Luis, There will be only one space, 🙂

    Thanks.

  • I completely agree that the set-based splitter is the right approach, but what if someone has a name like 'Georges St. Pierre' and you want to split it based on a space? How many names would he have?

    I'm not trying to cause an argument here. I want to prevent trouble for you later.

  • For 2 columns, a splitter such as the Delimited8k would be too much.

    This thread might give you an idea on how to do it.

    http://www.sqlservercentral.com/Forums/Topic1670877-391-1.aspx

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Luis Cazares (3/30/2015)


    For 2 columns, a splitter such as the Delimited8k would be too much.

    This thread might give you an idea on how to do it.

    http://www.sqlservercentral.com/Forums/Topic1670877-391-1.aspx

    I like Scott's approach. Nice, simple and fast. As long as you leave enough space for the last name to be N characters, it's a great approach.

  • Hi Wanger, Thanks for suggestion.

    The format will be same.such case will not happen.

    SELECT LEFT(FullName, charindex(' ', FullName) - 1) i am able to find before space.

    Lokking now for after space

    Thanks

  • abhas (3/30/2015)


    Hi Wanger, Thanks for suggestion.

    The format will be same.such case will not happen.

    SELECT LEFT(FullName, charindex(' ', FullName) - 1) i am able to find before space.

    Lokking now for after space

    Thanks

    Thank Luis for finding the perfect post. Here's the query you're looking for.

    if OBJECT_ID('tempdb.dbo.#names', 'u') is not null drop table #names;

    create table #names (

    FullName varchar(100));

    insert into #names(FullName)

    values('Ed Wagner'),

    ('Georges St. Pierre'),

    ('John Smith');

    select FirstName = SUBSTRING(fullname, 1, charindex(' ', fullname) - 1),

    LastName = SUBSTRING(fullname, charindex(' ', fullname) + 1, 100)

    from #names;

    drop table #names;

    Only the first space is the delimiter, so if you have a case where the person has a space in their first name, it won't come out right.

  • I'm just adding a safety net.

    if OBJECT_ID('tempdb.dbo.#names', 'u') is not null drop table #names;

    create table #names (

    FullName varchar(100));

    insert into #names(FullName)

    values('Ed Wagner'),

    ('Georges St. Pierre'),

    ('John Smith'),

    ('Madonna');

    select FirstName = SUBSTRING(fullname, 1, charindex(' ', fullname + ' ') - 1), --Add a trailing space

    LastName = SUBSTRING(fullname, charindex(' ', fullname) + 1, 8000) --8000 will work with any length (except max)

    from #names;

    drop table #names;

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Luis Cazares (3/30/2015)


    I'm just adding a safety net.

    if OBJECT_ID('tempdb.dbo.#names', 'u') is not null drop table #names;

    create table #names (

    FullName varchar(100));

    insert into #names(FullName)

    values('Ed Wagner'),

    ('Georges St. Pierre'),

    ('Sarah Grace Smith'), -- Expected First: Sarah Grace Last: Smith

    ('John Smith'),

    ('Madonna');

    select FirstName = SUBSTRING(fullname, 1, charindex(' ', fullname + ' ') - 1), --Add a trailing space

    LastName = SUBSTRING(fullname, charindex(' ', fullname) + 1, 8000) --8000 will work with any length (except max)

    from #names;

    drop table #names;

    But what if the first name has a space in the middle? The way the code is written it will return the wrong result in that case. Off-hand, I can't think of any method that would solve both problems.



    The opinions expressed herein are strictly personal and do not necessarily reflect the views or policies of my employer.

  • LightVader (3/31/2015)


    But what if the first name has a space in the middle? The way the code is written it will return the wrong result in that case. Off-hand, I can't think of any method that would solve both problems.

    That was the whole point we brought up.

  • Ed Wagner (3/31/2015)


    LightVader (3/31/2015)


    But what if the first name has a space in the middle? The way the code is written it will return the wrong result in that case. Off-hand, I can't think of any method that would solve both problems.

    That was the whole point we brought up.

    I understood that. I guess I wanted to be a little more explicit that Luis's sample code works for one way but not the other. Or more specifically that it can only work for one interpretation at a time. 😀



    The opinions expressed herein are strictly personal and do not necessarily reflect the views or policies of my employer.

  • LightVader (3/31/2015)


    Ed Wagner (3/31/2015)


    LightVader (3/31/2015)


    But what if the first name has a space in the middle? The way the code is written it will return the wrong result in that case. Off-hand, I can't think of any method that would solve both problems.

    That was the whole point we brought up.

    I understood that. I guess I wanted to be a little more explicit that Luis's sample code works for one way but not the other. Or more specifically that it can only work for one interpretation at a time. 😀

    Okay. I don't know of a foolproof way to split out names either, where the delimiter is allowed within the individual parts of the string. That must be why people usually design tables with names in separate columns. 😉

Viewing 15 posts - 1 through 14 (of 14 total)

You must be logged in to reply to this topic. Login to reply