6

私はこれを持っています

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.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

しかし、出力順序を

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 でこれを行うにはどうすればよいですか?

4

2 に答える 2

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
于 2012-04-23T09:43:42.903 に答える
0

これは簡単なはずです。

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

OrderValue文字列

Descr文字列

必要な順序で値を配置します

a-1.2.3

b- 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

l-プルトニウム

m-該当なし

次に、この新しいテーブルでテストテーブルに参加し、「OrderValue」で並べ替えます

それだ

于 2012-04-23T15:27:40.413 に答える