当前位置: 代码迷 >> Sql Server >> 怎么删除一个字段里重复的数并按照从小到大的顺序排列
  详细解决方案

怎么删除一个字段里重复的数并按照从小到大的顺序排列

热度:113   发布时间:2016-04-24 20:04:44.0
如何删除一个字段里重复的数并按照从小到大的顺序排列
例如在表tb1里有17778行数据,数据在字段notext里,TTD字段代表数据的ID,请问如何用sql语句完成
TDD NOTEXT
1  13 07 04 00 12 10 12 03 02 01 13 13 10 09 15
2  09 04 13 14 11 10 12 01 05 05 05 05 08 07 16
3  11 07 07 12 10 10 12 02 04 06 13 13 04 13 03
4  10 07 02 14 13 14 00 00 12 02 11 11 07 06 07
5  06 04 10 13 13 15 01 04 01 02 04 04 03 12 03
6  05 04 11 08 09 12 14 11 01 02 16 16 09 08 10
7  08 08 05 12 14 02 04 00 03 08 10 10 08 07 11
8  07 08 01 13 00 05 07 15 06 03 01 01 05 04 12
9  02 04 05 01 05 11 13 01 08 04 11 11 02 11 13
10  04 07 03 11 00 07 09 01 07 01 08 02 06 05 16
11  01 05 05 11 01 09 11 07 02 01 12 06 04 13 02
12  06 11 01 10 01 10 12 01 03 10 02 12 02 11 16
13  00 06 04 08 00 10 12 08 09 05 04 14 04 13 16
14  00 07 05 04 13 08 10 01 10 02 11 05 03 12 06
15  06 14 06 04 14 10 12 10 06 07 08 02 04 13 16
16  15 08 08 08 03 00 02 00 02 06 02 12 05 14 10
一、出现00就删除掉;
二、删除重复的数值;
三、按照从小到大的顺序排列在字段notext 里?谢谢!


删除重复,排序

------解决方案--------------------
create table #tb(notext varchar(100))
insert into #tb
select'13 07 04 00 12 10 12 03 02 01 13 13 10 09 15'
union all select'09 04 13 14 11 10 12 01 05 05 05 05 08 07 16'
union all select'11 07 07 12 10 10 12 02 04 06 13 13 04 13 03'
union all select'10 07 02 14 13 14 00 00 12 02 11 11 07 06 07'
union all select'06 04 10 13 13 15 01 04 01 02 04 04 03 12 03'
union all select'05 04 11 08 09 12 14 11 01 02 16 16 09 08 10'
union all select'08 08 05 12 14 02 04 00 03 08 10 10 08 07 11'
union all select'07 08 01 13 00 05 07 15 06 03 01 01 05 04 12'
union all select'02 04 05 01 05 11 13 01 08 04 11 11 02 11 13'
union all select'04 07 03 11 00 07 09 01 07 01 08 02 06 05 16'
union all select'01 05 05 11 01 09 11 07 02 01 12 06 04 13 02'
union all select'06 11 01 10 01 10 12 01 03 10 02 12 02 11 16'
union all select'00 06 04 08 00 10 12 08 09 05 04 14 04 13 16'
union all select'00 07 05 04 13 08 10 01 10 02 11 05 03 12 06'
union all select'06 14 06 04 14 10 12 10 06 07 08 02 04 13 16'
union all select'15 08 08 08 03 00 02 00 02 06 02 12 05 14 10' 
go


create function dbo.fn_text (@notext varchar(100))
returns varchar(100)
as
begin
declare @t table(col char(2))
declare @s varchar(100),@v varchar(2)
set @s=@notext
while len(@s)>0
begin
set @v=left(@s,2)
if @v<>'00'
insert into @t(col) values(left(@s,2))
set @s=stuff(@s,1,3,'')
end

set @s=''
select @s=@s+' '+col from (select distinct col from @t)t order by col
set @s=stuff(@s,1,1,'')