sql1:select * from tablename order by username asc
解释:上面语句的意思是用过username 字段升序排序后输出 tablename表的结果。
sql2:select * from tablename order by username desc
解释:上面语句的意思是用过username 字段降序排序后输出 tablename表的结果。
--负责把字符串转换为Varbinary--思路,把字符串按.拆分,然后转换成int,再转换成varbinary拼接
Create Function f_Order(@SourceSql Varchar(8000),@StrSeprate Varchar(2))
Returns Varbinary(8000)
As
Begin
Declare @temp Varbinary(8000)=0x0
Declare @ch Varchar(100)
Set @SourceSql=@SourceSql+@StrSeprate
While(@SourceSql<>'')
Begin
Set @ch=left(@SourceSql,Charindex(@StrSeprate,@SourceSql,1)-1)
Set @temp=@temp+Convert(Varbinary, Convert(Int,@ch))
Set @SourceSql=Stuff(@SourceSql,1,Charindex(@StrSeprate,@SourceSql,1),'')
End
Return @temp
End
Go
--建表
Create table T
(
A Varchar(100)
)
--插入数据
Insert Into T Values('1.1')
Insert Into T Values('1.1.1')
Insert Into T Values('1.1.2')
Insert Into T Values('1.2')
Insert Into T Values('10.1')
Insert Into T Values('10.1.1')
Insert Into T Values('10.1.2')
Insert Into T Values('11.1')
Insert Into T Values('2.1')
Insert Into T Values('3.1')
Insert Into T Values('4.1')
--测试
Select * from T
order by dbo.f_Order(A,'.')
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)