简体   繁体   English

SQL Server将字符串转换为数字并进行排序

[英]SQL Server converting string to numeric and sorting

I am attempting to sort a column of strings, which contains one alphanumeric character and a numbers. 我正在尝试对一列字符串进行排序,其中包含一个字母数字字符和一个数字。 If I run this: 如果我运行此命令:

select 
    column_name 
from 
    table_name 
where 
    item_type = 'ABC' 
    and item_sub_type = 'DEF' 
order by 
    cast(replace([column_name], 'A', '') as Numeric(10, 0)) desc

I get the correct sorted output: 我得到正确的排序输出:

A218
A217
A216

but if I try to grab the top row 但是如果我尝试抢占第一排

select top 1 
    column_name 
from 
    table_name 
where 
    item_type = 'ABC' 
    and item_sub_type = 'DEF' 
order by 
    cast(replace([column_name], 'A', '') as numeric(10, 0)) desc

it fails with the following error: 它失败,并显示以下错误:

Error converting data type varchar to numeric 将数据类型varchar转换为数值时出错

Any ideas on how I can select the top row? 关于如何选择第一行的任何想法?

Thanks! 谢谢!

I think this is the problem with your data - not all are match your pattern. 我认为这是您的数据存在的问题-并非所有人都符合您的模式。 You can check what is not valid using: 您可以使用以下方法检查无效的内容:

select column_name from table_name where ISNUMERIC(replace([column_name],'A','')) = 0

I think this is the optimizer not executing your query how you would hope. 我认为这是优化程序未按您希望的那样执行查询。 What I mean is SQL is a declarative language--your query merely states what you are trying to accomplish, not how you are trying to accomplish it (in most cases). 我的意思是SQL是一个说明性语言-查询只不过是说你想什么来完成的,你是不是如何努力完成它(在大多数情况下)。 Thus, the optimizer determines the best way and in cases as yours, does things in certain order that causes errors. 因此,优化器确定最佳方法,并且在您遇到的情况下,以一定顺序执行会导致错误的事情。 Try and force your logic with a CTE. 尝试通过CTE强制执行您的逻辑。

with cte as(
select column_name, [item_value] 
from table_name 
where item_type='ABC' and item_sub_type='DEF')

select top 1 column_name
from cte 
ORDER BY CAST(replace([item_value],'A','') AS Numeric(10,0)) desc

SQL Server 2012/2016 SQL Server 2012/2016年

select top 1 column_name 
from table_name 
where item_type='ABC' and item_sub_type='DEF' 
order by TRY_CONVERT(Numeric(10,0),replace([column_name],'A','')) desc

Order by dropping the prefix and casting to int, provided that the prefix is the only non-numeric the rest should be pure numbers. 通过删除前缀并强制转换为int进行排序,前提是该前缀是唯一的非数字,其余应为纯数字。

select 
    column_name 
from 
    table_name 
where 
    item_type = 'ABC' 
    and item_sub_type = 'DEF' 
order by 
    cast(replace([column_name], 'A', '') as Int) desc

the script above should just work, i reckon there is something wrong with your data could multiple values... 上面的脚本应该可以正常工作,我认为您的数据有问题,可能有多个值...

see example below 见下面的例子

declare @mytable table
(
code varchar(10) 
)

insert into @mytable
values 
('A323'),
('A223'),
('A123'),
('A553'),
('A923'),
('A23'),
('A235')



select 
    code
from 
    @mytable 
order by 
    cast(replace(code, 'A', '') as Int) desc


code
----------
A923
A553
A323
A235
A223
A123
A23

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM