34,587
社区成员
发帖
与我相关
我的任务
分享
select case when len(col)=3 then LEFT(col,3) else LEFT(col,6) end as a from tb
select left(字段名,6) from tb
union
select left(字段名,3) from tb
--> 测试数据:#t
if object_id('tempdb.dbo.#t') is not null drop table #t
create table #t([a] varchar(9))
insert #t
select '002006009' union all
select '002006' union all
select '002008002' union all
select '002001' union all
select '002' union all
select '001003001'
select a from
(
select distinct left(a,3) as a,0 as rn from #t
union
select distinct left(a,6) as a,1 as rn from #t
)t
group by a
order by max(rn)
/*
(所影响的行数为 6 行)
a
---------
001
001003
002
002001
002006
002008
(所影响的行数为 6 行)
(所影响的行数为 6 行)
a
---------
001
001003
002
002001
002006
002008
(所影响的行数为 6 行)
select top 6 * from tb
order by 字段名
select left(col,6) from tb order by left(col,6)
select left(字段名,6) from tb