SQL Select Reformat Name

Posted on 2012-09-06
Last Modified: 2012-09-07
There's a table with a field or column that has a person's name in it.  It's in the format as follows:

lastname, firstname

Disregard that that's not normalized.  Have to put up with it.  Is there function in SQL that would me do a query on that field in the following format?:

firstname lastname

It is wthout the comma  Need a function embedded in the SQL select statement that formats the person's name in the latter format.  Using ORACLE.  Would prefer not to use any Oracle functions -- just standard SQL.
Question by:lcor
    LVL 76

    Accepted Solution

    Try this:
    select regexp_replace(name_column,'([^,]*)(,[ ]?)(.*)','\3 \1') from table_name;
    LVL 25

    Assisted Solution

    >> Would prefer not to use any Oracle functions -- just standard SQL.
    That makes it hard ... the number of 'standard' functions is limiting.  
    The substring is considered as standard (as part of SQL99) ... in Oracle it is SUBSTR(), in SQL Server, MySQL, DB2 it is SUBSTRING(), MS Access it is MID() - so even though it is standard, it is syntactically different between different dbms implementations.
    Whereas something like "locate in string" is not standard (I believe) ... however Oracle, MySQL, MS Access has INSTR(), SQL Server CHARINDEX() and LOCATE() in DB2.

    So this
      ltrim(substr(name_col,instr(name_col,',')+1))||' '||substr(name_col,1,instr(name_col,',')-1)
    is not 'standard' by my understanding of 'standard' ... but it should be portable to other dbms implementations, albeit with syntax changes.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
    From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
    Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now