Select max int from varchar column(从 varchar 列中选择最大整数)
问题描述
我正在尝试从包含数字和字符串的 varchar 列中检索最大数字.我正在处理的数据示例:
I am trying to retrieve the largest number from a varchar column that includes both numbers and strings. An example of the data I'm working with:
箱号
123
A5
789
B1
BoxNumber
123
A5
789
B1
我需要从列中返回最大的数字(在本例中为 789),同时忽略 A5 和 B1 的非数字值.
I need to return the largest number (789 in this case) from the column while ignoring the non-numeric values of A5 and B1.
我找到了使用自定义函数来解决问题的解决方案,但我需要一些可以在不依赖自定义函数或过程的情况下执行即席查询的东西.
I have found solutions that use custom functions to solve the problem, but I need something that can be executed ad-hoc query without relying on custom functions or procs.
推荐答案
你需要一个组合,因为下面的事情 isnumeric 返回 1
you need a combination because of the fact that isnumeric returns 1 for the following things
select isnumeric('+'),isnumeric('5d2')
你的 where 子句应该是这样的
your where clause would be like this
WHERE VALUE NOT LIKE '%[a-z]%'
AND ISNUMERIC(VALUE) = 1
create table #bla (value varchar(50))
insert #bla values('123')
insert #bla values('a5')
insert #bla values('789')
insert #bla values('b1')
SELECT MAX(CAST(value AS Int)) FROM #bla
WHERE VALUE NOT LIKE '%[a-z]%'
AND ISNUMERIC(VALUE) = 1
我在这里写了这个ISNUMERIC Trouble
这篇关于从 varchar 列中选择最大整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从 varchar 列中选择最大整数
基础教程推荐
- 带更新的 sqlite CTE 2022-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- 带有WHERE子句的LAG()函数 2022-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
- 从字符串 TSQL 中获取数字 2021-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
