2012-04-23 13 views
6

私はカスタムソート順

1.19.3 
1.19.x 
1.2.3 
1.20.3 
1.4.168 
1.4.18 
1.x.x 
20MyTest 
DEC09 
dec09 
MyTest20 
N/A 
Plutonium 

が、私は希望を出力し、この

declare @testtable table (test nvarchar(max)) 


insert into @testtable (test) values ('1.2.3') 
insert into @testtable (test) values ('1.20.3') 
insert into @testtable (test) values ('1.19.x') 
insert into @testtable (test) values ('1.x.x') 
insert into @testtable (test) values ('1.19.3') 
insert into @testtable (test) values ('DEC09') 
insert into @testtable (test) values ('Plutonium') 
insert into @testtable (test) values ('dec09') 
insert into @testtable (test) values ('N/A') 
insert into @testtable (test) values ('MyTest20') 
insert into @testtable (test) values ('20MyTest') 
insert into @testtable (test) values ('1.4.18') 
insert into @testtable (test) values ('1.4.168') 

select * from @testtable 
order by test asc; 

は、出力順序がある必要はあり

1.2.3 
1.4.18 
1.4.168 
1.19.3 
1.19.x 
1.20.3 
1.x.x 
20MyTest 
DEC09 
dec09 
MyTest20 
Plutonium 
N/A 

(N/Aは "魔法"で、常に最大ですが、 "バージョン"(例1.2.3)は常に3桁ですが、1つ以上の桁はchar x可能な限り最大の数字と考えられる「任意の数字」を指定してください)

これをSQL Serverでどのように達成できますか?

+2

+ 1 DDLおよびINSERTの場合。どのSQL Serverのバージョンですか? –

+0

ソリューションは異なるサーバーに配置されるため、このバージョンは異なります – Millerbean

+0

*どのように違いますか?これを実行したい最初のバージョンは何ですか?適切なタグを追加してください。 –

答えて

3
select TT.* 
from @testtable as TT 
order by case when TT.test = 'N/A' then 1 else 0 end, 
     case when isnumeric(parsename(test, 3)+'E+00') = 1 then cast(parsename(test, 3) as int) else 99999 end, 
     case when isnumeric(parsename(test, 2)+'E+00') = 1 then cast(parsename(test, 2) as int) else 99999 end, 
     case when isnumeric(parsename(test, 1)+'E+00') = 1 then cast(parsename(test, 1) as int) else 99999 end, 
     test 
+1

しかし、解決策の1つは、列がvarcharであり、varcharとしてソートされることです。この解決策を使用してこれを実行することを検討することができます:http://stackoverflow.com/a/119842/326923 –

+0

これは私自身と同じ結果を返します – Millerbean

+0

1.4.168は1.4と交換する必要があります。18、まだ100%ではない:-) – Millerbean

0

これは簡単です。

2列を持つ新しいテーブルを作成します。

OrderValue列

のDescr文字列

1.2.3

B A-ために値を使用して、

をしたい場所を - 1.4.18

c-1.4.168

D- 1.19.3

E- 1.19.x

F- 1.20.3

G- 1.xxの

H- 20MyTest

I- DEC09

j-dec09

K-MyTest20プルトニウム1-

M- N/

今、それをある "OrderValue"

することにより、この新しいテーブルと注文をTESTTABLEに参加

関連する問題