What is the MS SQL Server capability similar to the MySQL FIELD() function?

前端 未结 4 1638
不知归路
不知归路 2020-12-09 17:44

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 remai

4条回答
  •  猫巷女王i
    2020-12-09 18:39

    I recommend a CTE (SQL server 2005+). No need to repeat the status codes or create the separate table.

    WITH cte(status, RN) AS (  -- CTE to create ordered list and define where clause
          SELECT 'active', 1
    UNION SELECT 'approved', 2
    UNION SELECT 'rejected', 3
    UNION SELECT 'submitted', 4
    )
    SELECT , 
    FROM  tbl
    INNER JOIN cte ON cte.status = tbl.status  -- do the join
    ORDER BY cte.RN  -- use the ordering defined in the cte
    

    Good luck,

    Jason

    提交回复
    热议问题