【问题标题】:CHARINDEX function to fetch string of digits until there is '.'CHARINDEX 函数获取数字字符串,直到出现 '.'
【发布时间】:2021-02-23 10:56:22
【问题描述】:

如何使用charindex获取.之前最多一位数字的字母字符串,例如:

ID
1022786.12
1203384
1226757.23
22343445
23434533

我只想使用charindex 函数获取没有. 的那些。

我们可以使用like 条件来做到这一点:

where ID not like '%.%'

但我想使用charindex函数

使用charindex后,结果应该是:

Result
1203384
22343445
23434533

我该怎么做?

【问题讨论】:

  • 这是什么rdbms
  • 我投票结束这个问题,因为如果您已经有解决方案但坚持使用一个特定功能,这不是一个需要解决的实际问题。
  • “但是我想用charindex函数” 为什么? LIKE 似乎是这里更好的解决方案(尽管两者都不是 SARGable)。但是如果你“必须”那么你的尝试没有奏效呢?似乎您想要检查包含int 的列,那么为什么不将TRY_CONVERT 作为选项也扔在那里。

标签: sql-server azure data-warehouse charindex


【解决方案1】:

使用 charindex('.', number)

declare @t table(number varchar(20))
insert @t select '1022786.12'
insert @t select '1203384'
insert @t select '1226757.23'
insert @t select '22343445'
insert @t select '23434533'

select * 
from @t
where charindex('.', number) < 1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-05
    • 2012-09-04
    • 2011-04-03
    相关资源
    最近更新 更多