Results 1 to 6 of 6

Thread: Simple Question

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Posts
    133

    Simple Question

    I have three fields in SQL..

    Full Name, Last Name, First Name

    Full Name = Doe, John
    Last Name = NULL
    First Name = NULL

    What SQL statement would be best to parse the name and have it so I can get:

    Full Name = Doe, John
    Last Name = Doe
    First Name = John


    Any advice would be appreciated.

    Thanks!

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Simple Question

    And what will you be running this code from?

    VB6? VB.NET? Query Analyser? Something else?

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Posts
    133

    Re: Simple Question

    SQL preferably

  4. #4
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Simple Question

    Code:
    SELECT Full Name, Last Name, First Name FROM Tablename
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  5. #5
    Hyperactive Member
    Join Date
    Jan 2008
    Location
    Merseyside
    Posts
    456

    Re: Simple Question

    (based on SQL Server T-SQL)

    Quick test to split them up.
    Code:
    -- Here is a quick example you can run yourself in T-SQL.
    DECLARE @FullName AS VARCHAR(20)
    SET @FullName = 'John, Doe'
    
    SELECT SUBSTRING(@FullName, 1, CHARINDEX(',', @FullName) - 1) AS 'First Name'
    SELECT LTRIM(SUBSTRING(@FullName, CHARINDEX(',', @FullName)+1, LEN(@FullName))) AS 'Last Name'
    And how you could code it in an update...
    Code:
    -- Used in a update
    UPDATE (table)
    SET first_name = SUBSTRING(FullName, 1, CHARINDEX(',', FullName) - 1),
    SET surname = LTRIM(SUBSTRING(FullName, CHARINDEX(',', FullName)+1, LEN(FullName)))

  6. #6
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: Simple Question

    If you have the luxury I'd recommend populating the Last Name and First Name columns correctly then ditching the full name altogether. Having both introduces redundancy into your database and it's easier to concatenate two strings (to get the full name) than it is to split a string.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width