SQL Server-是否存在提供数字自然顺序的排序规则? [英] SQL Server - Is there a collation that provides natural order for numbers?

查看:129
本文介绍了SQL Server-是否存在提供数字自然顺序的排序规则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SQL Server 2008。

I'm using SQL Server 2008.

我有一个表,其列的类型为 VarChar 。它当前填充有实际代表数字的字符串。不幸的是,该列必须保留 VarChar('1','2'...,'1000')

I have a table with a column of type VarChar. It is currently filled with strings which actually represent numbers. Unfortunately, the column must remain VarChar ('1', '2' ... , '1000' ).

我希望在该字段中查询最大数量值,但是由于这是 VarChar ,因此我得到的是词汇词典的最大值,而不是自然顺序的最大值。

I wish to query that field for the max number value but since this is a VarChar I get the Lexicographical max and not the natural order max.

我想我将尝试通过使用 COLLATE 子句来解决该查询,并更改为提供数字自然顺序的排序规则,例如在此链接

I thought I'll try and solve this by using the COLLATE clause for that query and change to a collation that provides numerical natural order like in this link


  1. SQL Server 2008是否有这样的排序规则,如果是,查询应该是什么?

  2. 如果存在这样的排序规则,我应该使用这种方法还是应该使用强制转换?

谢谢。

推荐答案

没有排序规则选项可以对数字进行自然排序。

There is no collation option that will perform a natural sort on numbers.

但是,如果所有值应为int

You can however, cast the value as an integer if all the values are expected to be integers.

尝试类似的东西

SELECT MAX(CAST (Value as int))
FROM MyTable

这篇关于SQL Server-是否存在提供数字自然顺序的排序规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆