分割字符串并返回mssql中最大的
我需要找到一种方法来获取具有最高versionNumber的数据。
这是我的数据库设计:
VERSIONNUMBER - varchar(15)
DOWNLOADPATH - varchar(100)
假设我有这样的记录:
VERSIONNUMBER -------- DOWNLOADPATH
1.1.2 a.com
1.1.3 b.com
2.1.4 c.com
2.1.5 d.com
2.2.1 e.com
我需要获取版本号为 2.2.1 的记录。不过需要一些关于 sql 的帮助:)
谢谢你的帮助
I need to find a way to get the data with the highest versionNumber.
Here is my database design:
VERSIONNUMBER - varchar(15)
DOWNLOADPATH - varchar(100)
Lets say I have records like:
VERSIONNUMBER -------- DOWNLOADPATH
1.1.2 a.com
1.1.3 b.com
2.1.4 c.com
2.1.5 d.com
2.2.1 e.com
I need to get the record with the versionnumber 2.2.1. Need some help with the sql though :)
Thank you for any help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
试试这个:
灵感来自: http://www.sql-server -helper.com/tips/sort-ip-address.aspx
在软件开发中,通常会发现有两位数字的次版本号,版本号与数字值没有任何关系,因此版本 1.12大于1.5;为了弥补这一点,您必须充分填充数字:
输出:
如果您不考虑这一点(如以下查询):
这两个查询将产生相同(不正确)的输出:
2.2.1 和 2.1.12 的值重叠。当您仅删除点并直接将结果字符串转换为 int 时,也会发生这种情况。 2.1.12变为2112,2.2.1变为2221。 2.2.1大于2.1.12,不小于
Try this:
Inspired from: http://www.sql-server-helper.com/tips/sort-ip-address.aspx
In software development, it is typical to find a minor version number that has two digits in it, the version's number don't have any bearing with number's value, thus version 1.12 is greater than 1.5; to compensate for that, you must pad the digits adequately:
Output:
If you don't take that into consideration(as with the following query):
Those two queries will yield the same (incorrect) output:
The 2.2.1 and 2.1.12's value overlapped. That also happens when you merely remove the dots and directly convert the resulting string to int. 2.1.12 become two thousand one hundred twelve, 2.2.1 become two hundred twenty one. 2.2.1 is greater than 2.1.12, not less than
或者,您可以使用排名,而不是将每个数字组相乘:
产量:
最终查询:
输出:
另一种简单的方法,降序排序,然后只获取第一行:
Alternatively, instead of multiplying each digit group, you can use ranking:
Yields:
Final query:
Output:
Another simple approach, sort descending then just get the first row:
如果您使用的是 SQL Server 2008,则可以利用
HIERARCHYID
数据类型。If you are on SQL Server 2008 you can lever the
HIERARCHYID
datatype.这会起作用,但它并不漂亮 - 我肯定会考虑更改存储版本号的方式。
基本上,它将
"1.24.937"
转换为"000010002400937"
,然后将其作为字符串正确排序。This will work, but it ain't pretty - I would definitely consider changing the way you store version numbers.
Basically it turns
"1.24.937"
into"000010002400937"
, which then sorts properly as a string.就我个人而言,我喜欢 @Mikael 的版本,但显然这对于其他 RDBMS 来说不太可移植......
这个怎么样?快速而肮脏,适用于三个数字的版本(如您的示例中所示)。
这里的技巧是要意识到“2.59”,例如,是一个有效的数字,只是不是一个有效的整数,所以您只需要拆分字符串在一处——您需要主版本号,以及其余的。这很可怕,但当它突然出现在我的脑海中时,我想我应该分享它,因为它至少短而且很丑陋。
当然,真正的答案是更改您的数据库设计,以拆分版本号或包含内部版本号或类似的内容......
Personally, I like @Mikael's version, but obviously that's not quite as portable to other RDBMSes...
How about this? Quick and dirty, and works for three-number versions (as given in your example.)
The hack here is to realise that "2.59", for example, is a valid number, just not a valid integer, so you only have to split the string in one place -- you want the major version number, and the rest. This is hideous, but when it sprang to mind, I figured I'd share it, as it is at least short and hideous.
Of course, the real answer is to change your database design to either split out the version numbers or include a build number or something like that...
可能还有一种更漂亮的方法来做到这一点。
There might be a prettier way to do it as well.