1

当前输出:

PM_DIG_OUTPUT_1_CLOSED  
PM_DIG_OUTPUT_10_CLOSED  
PM_DIG_OUTPUT_14_CLOSED  
PM_DIG_OUTPUT_15_CLOSED  
PM_DIG_OUTPUT_16_CLOSED  
PM_DIG_OUTPUT_2_CLOSED  
PM_DIG_OUTPUT_3_CLOSED  

预期输出:

PM_DIG_OUTPUT_1_CLOSED  
PM_DIG_OUTPUT_2_CLOSED  
PM_DIG_OUTPUT_3_CLOSED  
PM_DIG_OUTPUT_10_CLOSED  
PM_DIG_OUTPUT_14_CLOSED  
PM_DIG_OUTPUT_15_CLOSED  
PM_DIG_OUTPUT_16_CLOSED 

Index of Number is not fixed
实现此命令的最佳方法是什么?

编辑:一些记录还包含以下数据
PRM_CODE_MIO_DIGITALOUT_WRITE_LOGIC_CARD2_DO1
PRM_CODE_MIO_DIGITALOUT_WRITE_LOGIC_CARD2_DO2
PRM_CODE_MIO_DIGITALOUT_WRITE_LOGIC_CARD2_DO3
PRM_CODE_MIO_DIGITALOUT_WRITE_LOGIC_CARD2_DO4

4

2 回答 2

5

询问:

SQLFIDDLE示例

SELECT *
FROM Table1 t1
ORDER BY CAST(REPLACE(REPLACE(col, 'PM_DIG_OUTPUT_', ''),'_CLOSED', '') AS int)

结果:

|                     COL |
|-------------------------|
|  PM_DIG_OUTPUT_1_CLOSED |
|  PM_DIG_OUTPUT_2_CLOSED |
|  PM_DIG_OUTPUT_3_CLOSED |
| PM_DIG_OUTPUT_10_CLOSED |
| PM_DIG_OUTPUT_14_CLOSED |
| PM_DIG_OUTPUT_15_CLOSED |
| PM_DIG_OUTPUT_16_CLOSED |

编辑的答案

您可以使用查询: SQLFIDDLEExample

SELECT *
FROM Table1 t1
ORDER BY CASE WHEN LEFT(col, 2) = 'PM' 
              THEN CAST(REPLACE(REPLACE(col, 'PM_DIG_OUTPUT_', ''),'_CLOSED', '') AS int)
              ELSE RIGHT(col,1) END

结果:

|                                           COL |
|-----------------------------------------------|
|                        PM_DIG_OUTPUT_1_CLOSED |
| PRM_CODE_MIO_DIGITALOUT_WRITE_LOGIC_CARD2_DO1 |
| PRM_CODE_MIO_DIGITALOUT_WRITE_LOGIC_CARD2_DO2 |
|                        PM_DIG_OUTPUT_2_CLOSED |
|                        PM_DIG_OUTPUT_3_CLOSED |
| PRM_CODE_MIO_DIGITALOUT_WRITE_LOGIC_CARD2_DO3 |
| PRM_CODE_MIO_DIGITALOUT_WRITE_LOGIC_CARD2_DO4 |
|                       PM_DIG_OUTPUT_10_CLOSED |
|                       PM_DIG_OUTPUT_14_CLOSED |
|                       PM_DIG_OUTPUT_15_CLOSED |
|                       PM_DIG_OUTPUT_16_CLOSED |
于 2013-10-24T11:37:33.177 回答
2

假设数字后面只有一个下划线和一些文字(那么无论数字前面是什么,如果它至少是一个下划线和一些文字)。

编辑新值。如果可以找到它们,它将按数字排序,如果通过匹配模式找不到数字,它将不会中断:

select 
    *,
    substring(y,len(y)-charindex('_',reverse(y))+2,100) as num
from (
    select
        *,
        substring(x,1,len(x)-charindex('_',reverse(x))) as y
    from (
        select 'PM_DIG_OUTPUT_1_CLOSED' as x union all
        select 'PM_DIG_OUTPUT_10_CLOSED' union all
        select 'PM_DIG_OUTPUT_14_CLOSED' union all
        select 'PM_DIG_OUTPUT_15_CLOSED' union all
        select 'PM_DIG_OUTPUT_16_CLOSED' union all
        select 'PM_DIG_OUTPUT_2_CLOSED' union all
        select 'PM_DIG_OUTPUT_3_CLOSED' union all 
        select 'PRM_CODE_MIO_DIGITALOUT_WRITE_LOGIC_CARD2_DO1' union all
        select 'PRM_CODE_MIO_DIGITALOUT_WRITE_LOGIC_CARD2_DO2' union all
        select 'PRM_CODE_MIO_DIGITALOUT_WRITE_LOGIC_CARD2_DO3' union all
        select 'PRM_CODE_MIO_DIGITALOUT_WRITE_LOGIC_CARD2_DO4'        
    ) x
) y
order by 
    case when isnumeric(substring(y,len(y)-charindex('_',reverse(y))+2,100))=1 then cast(substring(y,len(y)-charindex('_',reverse(y))+2,100) as int) end
于 2013-10-24T11:44:31.210 回答