大家好,这是我第一次问什么。
我的数据库上有多个编号和“/”的代码,例如:
510325205
510325205/000/01
510565025-01
510565025-01/090/03
...
我需要修剪/
-我需要这些结果:
510325205
510325205
510565025-01
510565025-01
...
我已经搜过并试过了
left(code, charindex('/', code))
它适用于含有/
的码,但不含/
的码被排除在结果之外。
谢谢你的帮忙!
发布于 2022-05-03 08:59:46
你的尝试非常接近。只需添加-1.
如您所见,左()函数的解释如下:
函数从字符串(从左开始)提取若干字符:左( string,number_of_chars)
使用charindex()函数,您已经告诉左侧()函数要接受多少个字符。通过添加-1,您已经删除了'/‘符号,因为您告诉左侧()函数接受10个字符,而不是11个(例如),因为第11个字符是'/’。
select left('510325205/000/01',charindex('/','510325205/000/01')-1)
或者因为您有名为代码的列
select left(code,charindex('/',code)-1)
如果您的值没有/您可以使用以下内容:
select case when charindex('/',code_c)-1 = -1
then
code_c
else
left(code_c,charindex('/',code_c)-1)
end RESULT
from test
或
select left(code_c,iif(charindex('/',code_c)-1 = -1, len(code_c), charindex('/',code_c)-1))
from test
https://stackoverflow.com/questions/72096973
复制相似问题