排序字符串包含数字

时间:2013-10-24 11:32:21

标签: sql-server

Current OutPut:

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 

数字索引未固定
实现此订单的最佳方式是什么?

编辑: 有些记录还包含以下数据 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

2 个答案:

答案 0 :(得分:5)

查询:

<强> SQLFIDDLEExample

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 |

答案 1 :(得分: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