1

次の行を持つ SQL Server テーブルに列があります。

マイカラム:

1 Month
2 Week
10 Minutes
1 week
12 hours
1 days
2 month
2 day
5 minutes
1 hours

Priority 文字列を含むテキスト列です。

次のように並べ替えられたこの列を選択して返す方法はありますか:

10 Minutes
5 minutes
1 hours
10 Hours
1 day
2 days
1 week
2 weeks
1 month
2 months

等..

ありがとうございました

4

3 に答える 3

1

この解決策を試してください:

SELECT mycolumn
FROM tbl
ORDER BY
    SUBSTRING(mycolumn, PATINDEX('%[^0-9]%', mycolumn)+1, 999),
    CAST(LEFT(mycolumn, PATINDEX('%[^0-9]%', mycolumn)-1) AS INT)

SQL フィドルのデモ

于 2012-07-10T08:10:20.153 に答える
0
select T4.cnt +' '+T4.name from (         
select substring(name, 1, CHARINDEX(' ', name)) cnt,substring(name,CHARINDEX(' ', name)+1,LEN(name)) name from test4) T4      
left outer join (
select 1 as id,'Month' As name union all
select 2 as id,'Week' As name union all
select 3 as id,'Day' As name union all
select 4 as id,'Minutes' As name )T6
on t4.name=t6.name
order by t6.id,t4.cnt

You have to give all the distinct values (month,week etc..) in the order you want in the left table with "union all"

于 2012-07-10T08:34:46.057 に答える
0
order by case when patindex('%Month', MyColumn) > 0
              then 0
              when patindex('%week', MyColumn) > 0
              then 1
              when patindex('%days', MyColumn) > 0
              then 2
              when patindex('%Minutes', MyColumn) > 0
              then 3
         end, 
         cast(substring(MyColumn, 1, CHARINDEX(' ', MyColumn)) as int)
于 2012-07-10T07:52:29.747 に答える