gpt4 book ai didi

SQL - 返回数字范围 1 到 1.99

转载 作者:行者123 更新时间:2023-12-02 03:45:06 24 4
gpt4 key购买 nike

例如:

700 表示 700.00 - 700.99

400.1 表示 400.10 - 400.19

目前我有这样的东西:

Select codes 
From code_table
where codes in ('700', '400.1')

这不起作用,因为它只是在寻找 700 和 400.1,所以我尝试了:

Select codes 
From code_table
where left (codes, 3) = '700'

这行得通,但问题是我正在寻找的初始数字集远不止这里的 2,有没有更简单的方法来做到这一点而不必为每个数字都这样做?我认为 between 子句也可以工作,但每个子句都需要代码?

最佳答案

如果我没有正确理解您的意见,那么如果您与 varchar() 值进行比较,或者将字符串与 BETWEEN 运算符一起使用,则需要显式构建正则表达式。

所以,这个 . . .

select * 
from code_table
where codes = '700'
or codes like '700.[1-9]'
or codes like '700.[1-9][0-9]';

或者这个。

select * 
from code_table
where codes between '700' and '700.99'

第二个更简单。这两个查询都排除了值“700.999”,我相信您想要排除它。

要使用数字而不是 varchar() 值,请转换为十进制。 ( float 。)

select * 
from code_table
where cast(codes as decimal(10, 3)) between 700 and 700.99;

无论如何,您的 SQL 必须了解代码的结构。如果我处于你的位置,我会明确地通过范围。这样一来,全部责任都由前端代码承担,所以应该不会有任何意外。

关于SQL - 返回数字范围 1 到 1.99,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17599015/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com