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

  • SEARCH
  • Home
  • 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 3228140
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:39:25+00:00 2026-05-17T16:39:25+00:00

MySQL provides a string function named FIELD() which accepts a variable number of arguments.

  • 0

MySQL provides a string function named FIELD() which accepts a variable number of arguments. The return value is the location of the first argument in the list of the remaining ones. In other words:

FIELD('d', 'a', 'b', 'c', 'd', 'e', 'f')

would return 4 since ‘d’ is the fourth argument following the first.

This function provides the capability to sort a query’s results based on a very specific ordering. For my current application there are four statuses that I need to manager: active, approved, rejected, and submitted. However, if I simply order by the status column, I feel the usability of the resulting list is lessened since rejected and active status items are more important than submitted and approved ones.

In MySQL I could do this:

SELECT <stuff> FROM <table> WHERE <conditions> ORDER BY FIELD(status, 'rejected', 'active','submitted', 'approved')

and the results would be ordered such that rejected items were first, followed by active ones, and so on. Thus, the results were ordered in decreasing levels of importance to the visitor.

I could create a separate table which enumerates this importance level for the statuses and then order the query by that in descending order, but this has come up for me a few times since switching to MS SQL Server so I thought I’d inquire as to whether or not I could avoid the extra table and the somewhat more complex queries using a built-in function similar to MySQL’s FIELD().

Thank you,
David Kees

  • 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-17T16:39:26+00:00Added an answer on May 17, 2026 at 4:39 pm

    Use a CASE expression (SQL Server 2005+):

    ORDER BY CASE status
               WHEN 'active' THEN 1
               WHEN 'approved' THEN 2
               WHEN 'rejected' THEN 3
               WHEN 'submitted' THEN 4
               ELSE 5
             END
    

    You can use this syntax for more complex evaluation (including combinations, or if you need to use LIKE)

    ORDER BY CASE 
               WHEN status LIKE 'active' THEN 1
               WHEN status LIKE 'approved' THEN 2
               WHEN status LIKE 'rejected' THEN 3
               WHEN status LIKE 'submitted' THEN 4
               ELSE 5
             END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was wondering if it is possible in a mysql stored-function or stored-procedure compose
I've been doing PHP/MySQL websites with shared hosting providers for the last couple years.
I'm inserting a DateTime into MsSQL using the GetUTCDate() function provided by MsSQL. I
MySQL has this incredibly useful yet proprietary REPLACE INTO SQL Command. Can this easily
MySQL specifies the row format of a table as either fixed or dynamic, depending
MySQL 5.0.45 What is the syntax to alter a table to allow a column
MySQL ResultSets are by default retrieved completely from the server before any work can
MySQL column > sdate, edate ( its 2 column). sdate is start date for
In Mysql Administrator, when doing backups, what exactly is Compatibility Mode? I'm trying to
What mysql functions are there (if any) to trim leading zeros from an alphanumeric

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.