如何使用Order获取名称? [英] How to get the Name using Order by?
问题描述
亲爱的所有人,
我正在使用Asp.net和Sql。我的数据如下:
名称
LD_B_10
LD_B_101
LD_B_2
LD_B_1
LD_B_200
LD_B_100
我正在使用Select Query with按名称使用顺序。我得到以下类型的结果。
LD_B_1
LD_B_10
LD_B_100
LD_B_101
LD_B_2
LD_B_200
但我需要以下类型的结果。
LD_B_1
LD_B_2
LD_B_10
LD_B_100
LD_B_101
LD_B_200
任何一个给我参考如何做。
谢谢提前。
使用SUBSTRING
和CAST
来提取字符串的数字部分和对该值进行排序。
SQL - 子串函数1Keydata [ ^ ]
CAST和CONVERT(Transact-SQL) [ ^ ]
声明@选项卡表(
名称nvarchar(最大)
)
插入@tab
值('LD_B_10')
,('LD_B_101')
,('LD_B_2')
,('LD_B_1' )
,('LD_B_200')
,('LD_B_100')
select * from @tab按try_convert排序(int,replace(名称,'ld_b_',''))asc
您的查询将是
选择名称来自 myTable 订单 by
CAST( RIGHT (名称,len(名称)-5) AS int )
那么你的结果将是:
LD_B_1
LD_B_2
LD_B_10
LD_B_100
LD_B_101
LD_B_200
快乐编码! :)
Dear All,
I am Working with Asp.net and Sql. I have the data following like this
Names
LD_B_10
LD_B_101
LD_B_2
LD_B_1
LD_B_200
LD_B_100
I am using Select Query with using order by "Names". I get the Following type of result.
LD_B_1
LD_B_10
LD_B_100
LD_B_101
LD_B_2
LD_B_200
but I need Following type of result.
LD_B_1
LD_B_2
LD_B_10
LD_B_100
LD_B_101
LD_B_200
any one give me the reference how to do it.
Thanks Advance.
UseSUBSTRING
andCAST
to extract the number part of your string and sort on that value.
SQL - Substring Function | 1Keydata[^]
CAST and CONVERT (Transact-SQL)[^]
declare @tab table(
names nvarchar(max)
)
insert into @tab
values ('LD_B_10')
,('LD_B_101')
,('LD_B_2')
,('LD_B_1')
,('LD_B_200')
,('LD_B_100')
select * from @tab order by try_convert(int,replace(Names,'ld_b_','')) asc
Your query will be
select Names from myTable order by CAST(RIGHT(Names,len(Names)-5) AS int)
Then your Result will be:
LD_B_1
LD_B_2
LD_B_10
LD_B_100
LD_B_101
LD_B_200
Happy Coding! :)
这篇关于如何使用Order获取名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!