Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6233975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:23:59+00:00 2026-05-24T10:23:59+00:00

I need a MySQL stored function that sorts the words (separated by a space)

  • 0

I need a MySQL stored function that sorts the words (separated by a space) in a string and return the sorted string.

For example :
"The Quick Brown Fox" -> "Brown Fox Quick The"

In PHP this would be an easy task, but in MySQL stored functions I can’t use arrays, so I’m a bit stuck now. I could create a temp table, but this seems so overkill… (and slow).

Any idea’s ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T10:23:59+00:00Added an answer on May 24, 2026 at 10:23 am

    Many thanks to Bob Vale !

    I altered the function he suggested a little bit to specify a custom delimiter.

    UPDATE:

    This function is not perfect. It produces strangs results when the begin or end of the string is the same as the delimiter, and when there is more than 1 delimiter in a row in the string, so you’ll have to make sure this is not the case in your strings.

    CREATE FUNCTION `SPLIT_SORT`(inString TEXT, inSeparator TEXT)
     RETURNS text CHARSET utf8
    BEGIN
      DECLARE strings INT DEFAULT 0;     -- number of substrings
      DECLARE forward INT DEFAULT 1;     -- index for traverse forward thru substrings
      DECLARE backward INT;   -- index for traverse backward thru substrings, position in calc. substrings
      DECLARE remain TEXT;               -- work area for calc. no of substrings
    -- swap areas TEXT for string compare, INT for numeric compare
      DECLARE swap1 TEXT;                 -- left substring to swap
      DECLARE swap2 TEXT;                 -- right substring to swap
    
      SET remain = inString;
      SET backward = LOCATE(inSeparator, remain);
      WHILE backward != 0 DO
        SET strings = strings + 1;
        SET backward = LOCATE(inSeparator, remain);
        SET remain = SUBSTRING(remain, backward+1);
      END WHILE;
      IF strings < 2 THEN RETURN inString; END IF;
      REPEAT
        SET backward = strings;
        REPEAT
          SET swap1 = SUBSTRING_INDEX(SUBSTRING_INDEX(inString,inSeparator,backward-1),inSeparator,-1);
          SET swap2 = SUBSTRING_INDEX(SUBSTRING_INDEX(inString,inSeparator,backward),inSeparator,-1);
          IF  swap1 > swap2 THEN
            SET inString = TRIM(BOTH inSeparator FROM CONCAT_WS(inSeparator
            ,SUBSTRING_INDEX(inString,inSeparator,backward-2)
            ,swap2,swap1
            ,SUBSTRING_INDEX(inString,inSeparator,(backward-strings))));
          END IF;
          SET backward = backward - 1;
        UNTIL backward < 2 END REPEAT;
        SET forward = forward +1;
      UNTIL forward + 1 > strings
      END REPEAT;
    RETURN inString;
    END;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making one MySql script that break one string field in multiple words. I
I need a MySQL query (or function) to calculate the geographic midpoint of an
I need to provide 4 MySQL stored procedures for each table in a database.
I'm writing a function that I need to use either a TABLE variable for
I need to pass an array of strings as parameter to a MySQL stored
I am writing a PHP function that will need to loop over an array
I need to store a string in a MySQL database. The values will later
I'm quite new to subject of writting stored function for mySQL database, hence i'm
I need to have a routine/function/stored proc/whatever to archive particular accounts from our 'active'
I'm trying to learn and use SP(Stored Procedure) and UDF(User Defined Function) with MySQL

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.