+ Reply to Thread
Results 1 to 4 of 4

SELECT query to deal with "A " and "The " prefixes

  1. #1
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    SELECT query to deal with "A " and "The " prefixes

    Say I am using a SELECT query that returns a list of Titles. I want to alter the query so that it will reformat each Title string that starts with "A " or "The " so that this is moved to the end of the string. To give an example:

    BEFORE
    "The Three Little Pigs"
    "A Tale of Two Cities"


    AFTER
    "Three Little Pigs, The"
    "Tale of Two Cities, A"


    I want this so that the ORDER BY will be more logical and thus makes it easier to find the Title in a dropdown control.

    How do I alter my SELECT query to achieve this outcome?
    *******************************************************

    HELP WANTED! (Links to Forum threads)
    Trying to create reusable code for Custom Events at Workbook (not Application) level

    *******************************************************

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2404 Win 11 Home 64 Bit
    Posts
    23,859

    Re: SELECT query to deal with "A " and "The " prefixes

    Try this"

    SELECT Table1.title, IIf(Left([title],3)="The",Mid([title],5,Len([title])-4) & ", The",IIf(Left([title],1)="A",Right([title],Len([title])-2) & ", A",[title])) AS Expr1
    FROM Table1;
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: SELECT query to deal with "A " and "The " prefixes

    I think I solved it a few minutes ago. Query below (I am using MS SQL Server hence the use of CHARINDEX)

    PHP Code: 
    SELECT CASE UPPER(LEFT(TitleCHARINDEX(' ',Title)))
    WHEN 'THE' THEN 
        RIGHT
    (TitleLEN(Title) - CHARINDEX(' ',Title)+1) + ', The'
    WHEN 'A'THEN 
        RIGHT
    (TitleLEN(Title) - CHARINDEX(' ',Title)+1) + ', A'
    ELSE
        
    Title
    END
    FROM Table1 
    ORDER BY Title 

  4. #4
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: SELECT query to deal with "A " and "The " prefixes

    Quote Originally Posted by alansidman View Post
    Try this"

    SELECT Table1.title, IIf(Left([title],3)="The",Mid([title],5,Len([title])-4) & ", The",IIf(Left([title],1)="A",Right([title],Len([title])-2) & ", A",[title])) AS Expr1
    FROM Table1;
    Thank you very much Alan. +1

    (My version works in MS SQL Server but not when I query an Excel workbook. I need your version for that!)

    I needed to make a slight change to your code to check for a space after "A" or "The". Detecting "The" instead of "The " causes "There" to be converted!
    Last edited by mc84excel; 12-10-2014 at 11:25 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 4
    Last Post: 11-17-2013, 12:05 PM
  2. [SOLVED] How to USE """"" cells count """"" change font color
    By austin123456 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-09-2013, 06:14 AM
  3. Replies: 5
    Last Post: 10-12-2010, 06:46 AM
  4. Replies: 5
    Last Post: 06-26-2006, 09:23 PM
  5. Replies: 7
    Last Post: 05-13-2006, 05:02 PM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1