Results 1 to 14 of 14
  1. #1
    tigorin is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Apr 2014
    Posts
    7

    Swap words from a string.


    Hi. I have some data that I want to display from a table in the following manner: I have two columns, the first name and last name. Let's say I have a first name + last name John Martin Doe. What's a SQL script that can only display (not modify the name in the actual table) the name as Martin John Doe. Thanks.

  2. #2
    orange's Avatar
    orange is online now Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,742
    I suggest you start here.

  3. #3
    tigorin is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Apr 2014
    Posts
    7
    I did, that's where I come from. Maybe there's something I overlooked and I can't figure it out.

  4. #4
    orange's Avatar
    orange is online now Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,742
    What is your table name?
    Post your sql.

  5. #5
    tigorin is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Apr 2014
    Posts
    7
    I have found a workaround for individual names:
    SELECT REPLACE(Info.Name,"John Martin Doe", "Martin John Doe") AS Name
    FROM Info;

    This returns Martin John Doe, as I wanted. But I need a generic formula that I can use to automatically switch data like that.

  6. #6
    Xipooo's Avatar
    Xipooo is offline Sr. Database Developer
    Windows 8 Access 2013
    Join Date
    Jan 2014
    Location
    Arizona
    Posts
    332
    You just wrote it.

    SELECT Replace(Info.name, [Name], [OtherName])

    But I'm not sure why you aren't just doing a SELECT on the OtherName and renaming the field name.

    Code:
    SELECT [OtherName] As Name

  7. #7
    tigorin is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Apr 2014
    Posts
    7
    And what if I have 1000 names to modify like that? Do I need to take each and every one by hand?

  8. #8
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    how do you know which name is supposed to go first? In your example you don't say which set of text is stored in which field.

    So is the first name John Martin and the surname Doe
    or is the first name John and the surname Martin Doe?

    There are all kinds of things you can do to manipulate a string or strings but you haven't given enough information to help in determining what that should be.

    If your string is actually a single field (which seems to be the case based on your formula) what are the parameters of how that data is stored?

    I'm assuming this is supposed to be FIRST MIDDLE LAST based on your example so in the case of a person where there was no middle name you'd have FIRST LAST and you'd need to account for that but really your example isn't clear enough to determine that.

  9. #9
    tigorin is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Apr 2014
    Posts
    7
    Ok, leaving aside my above example formula(I wrote it like that only to simplify), I have two columns in a table. One is Name and the other is Surname. In Name I have the field John Martin and in Surname, Doe. The single result that I want to be displayed by using a formula in SQL is Martin John Doe. If there's any more info needed, please say.

  10. #10
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,956
    And you want this for every single record? Does every record have two parts in the Name field? You need to swap the parts for every record? Why is this needed?

    BTW, Name is a reserved word. Should not use reserved words as field names.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  11. #11
    tigorin is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Apr 2014
    Posts
    7
    Yes, I want for all records, not every record have 2 names and yes, I need to swap for every record.

  12. #12
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    Ok so your rule is:

    If the first name is two words you want the second word to appear first, otherwise just use the first name?

    Assuming for the sake of argument that your data entry is 100% reliable and that there is, at most, one space in the name appearing in the firstname field you could have something like


    Code:
    IIf(InStr([firstname]," ")=0,[firstname],Right([firstname],Len([firstname])-InStr([firstname]," ")) & " " & Trim(Left([firstname],InStr([firstname]," ")))) & " " & [lastname]

  13. #13
    tigorin is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Apr 2014
    Posts
    7
    Yes, that's the rule. That code doesn't work for me, and I don't know if that's VBA or not, but I don't need VBA, only SQL script.

  14. #14
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    that's not vba, that's a formula you can plug into a query, it assumes you have two fields on your table named FIRSTNAME and LASTNAME, if you don't then modify it to your table structure.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 01-01-2014, 11:21 PM
  2. Replies: 13
    Last Post: 06-05-2012, 10:46 AM
  3. Replies: 4
    Last Post: 12-02-2011, 11:20 AM
  4. Replies: 1
    Last Post: 05-30-2011, 09:38 AM
  5. tags and words in a row
    By bimfire in forum Access
    Replies: 0
    Last Post: 11-07-2007, 10:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums