0

有人可以告诉我哪里出错了,我似乎无法提取第四个单词 CCC,尝试了所有方法,不想将其放入表中,只是在选择中返回子字符串?

DECLARE  @ProductCode VARCHAR(256) 

SET @ProductCode = 'CCCC-DDDDDDD-AAA-CCC-BBBBB' 

SELECT LEFT(@ProductCode,CHARINDEX('-',@ProductCode) - 1) AS CHAR1, 

    SUBSTRING(@ProductCode,CHARINDEX('-',@ProductCode) + 1, 
                CHARINDEX('-',@ProductCode,CHARINDEX('-', @ProductCode) + 1) - (CHARINDEX('-',@ProductCode) + 1)) AS CHAR2, 
        
    SUBSTRING(@ProductCode,CHARINDEX('-',@ProductCode,CHARINDEX('-',@ProductCode) + 1) + 1, 
                DATALENGTH(@ProductCode) - CHARINDEX('-', @ProductCode,CHARINDEX('-',@ProductCode) + 1) - CHARINDEX('-',REVERSE(@ProductCode))) AS CHAR3, 

    SUBSTRING(@ProductCode,CHARINDEX('-',@ProductCode,CHARINDEX('-',@ProductCode), CHARINDEX('-',@ProductCode) + 1)) + 1, 
DATALENGTH(@ProductCode) - CHARINDEX('-', @ProductCode,CHARINDEX('-',@ProductCode), CHARINDEX('-',@ProductCode) + 1) - CHARINDEX('-',REVERSE(@ProductCode))) AS CHAR4,
       RIGHT(@ProductCode,CHARINDEX('-',REVERSE(@ProductCode)) - 1) AS LASTCHAR

GO

非常感谢

4

5 回答 5

0

这种方法具有可读性的优点,但它很丑......

DECLARE  @ProductCode VARCHAR(256) 

SET @ProductCode = 'CCCC-DDDDDDD-AAA-CCC-BBBBB' 

select @ProductCode = substring(@ProductCode,charindex('-',@ProductCode)+1,99)
select @ProductCode = substring(@ProductCode,charindex('-',@ProductCode)+1,99)
select @ProductCode = substring(@ProductCode,charindex('-',@ProductCode)+1,99)
select @ProductCode = left(@ProductCode,charindex('-',@ProductCode)-1)

select @ProductCode
于 2020-11-18T23:11:49.000 回答
0

这种方法的优点是灵活,您可以更改搜索的第 N 个单词......

declare @ProductCode varchar(256) 
declare @WordSought int = 4
declare @Cntr int = 0

set @ProductCode = 'CCCC-DDDDDDD-AAA-CCC-BBBBB' 

while @cntr < @WordSought - 1
begin
    select @ProductCode = substring(@ProductCode,charindex('-',@ProductCode)+1,99)
    set @cntr = @cntr + 1
end

select left(@ProductCode,charindex('-',@ProductCode + '-') - 1)
于 2020-11-18T23:17:51.800 回答
0
--get 3rd and 4th words (the same way as for the 2nd word)

DECLARE  @ProductCode VARCHAR(256) 

SET @ProductCode = 'CC12CC-DDD34DD-A56A-C78C-BB910BB' 

SELECT LEFT(@ProductCode,CHARINDEX('-',@ProductCode) - 1) AS CHAR1, 

SUBSTRING(@ProductCode,CHARINDEX('-',@ProductCode) + 1, 
                CHARINDEX('-',@ProductCode,CHARINDEX('-', @ProductCode) + 1) - (CHARINDEX('-',@ProductCode) + 1)) AS CHAR2, 
        
SUBSTRING(
@ProductCode,
--.. from position of 2nd "-" plus one 
CHARINDEX('-',@ProductCode, CHARINDEX('-', @ProductCode) + 1) + 1,
--as many chars as there are between 2nd&3rd "-"
CHARINDEX('-', @ProductCode, CHARINDEX('-',@ProductCode, CHARINDEX('-', @ProductCode) + 1) + 1) -1 --3rd "-" minus 1
- --minus position 2nd "-"
CHARINDEX('-',@ProductCode, CHARINDEX('-', @ProductCode) + 1) --= chars between 2nd and 3rd "-"
) as xCHAR3,                 
                
SUBSTRING(
@ProductCode,
--.. from position of 3rd "-" plus one 
CHARINDEX('-', @ProductCode, CHARINDEX('-',@ProductCode, CHARINDEX('-', @ProductCode) + 1) + 1) + 1,
--get as many chars as there are between 3rd&4th "-"
CHARINDEX('-', @ProductCode, CHARINDEX('-', @ProductCode, CHARINDEX('-',@ProductCode, CHARINDEX('-', @ProductCode) + 1) + 1)+1) -1 --4th "-" minus 1
- --minus position 3rd "-"
CHARINDEX('-', @ProductCode, CHARINDEX('-',@ProductCode, CHARINDEX('-', @ProductCode) + 1) + 1) --= chars between 3rd and 4th "-"
) as xCHAR4, 

RIGHT(@ProductCode,CHARINDEX('-',REVERSE(@ProductCode)) - 1) AS LASTCHAR

GO

--... or ...
DECLARE  @ProductCode VARCHAR(256);

SET @ProductCode = 'CC12CC-DDD34DD-A56A-C78C-BB910BB'; 

select 
    p.value('comment()[1]', 'varchar(100)') as char1,
    p.value('comment()[2]', 'varchar(100)') as char2,
    p.value('comment()[3]', 'varchar(100)') as char3,
    p.value('comment()[4]', 'varchar(100)') as char4,
    p.value('comment()[5]', 'varchar(100)') as char5                
from
(
--splitting on '-', xml comment is rather safe
select cast('<!--'+replace(@ProductCode, '-', '--><!--')+'-->' as xml) p
) as t;
于 2020-11-18T22:56:04.000 回答
0

也许它会更简单:

select s.value
from string_split(@productcode, '-') s
where @productcode like '%-%-%-' + s.value + '-%' and
      @productcode not like '%-%-%-%-' + s.value + '-%' and

  
于 2020-11-18T20:55:59.433 回答
0

以下是完成任务的一种方法:

select substring(@productcode,charindex('-',@ProductCode,charindex('-',@ProductCode,charindex('-',@ProductCode,charindex('-',@ProductCode))+1)+1)+1,datalength(@productcode)-charindex('-',@ProductCode,charindex('-',@ProductCode,charindex('-',@ProductCode,charindex('-',@ProductCode,charindex('-',@ProductCode))+1)+1)+1)-2);

您因使用多种方法提取字符串而感到困惑。您不需要 REVERSE FUNTION 来获取字符串的结尾,您可以使用 charindex 函数继续提取“-”的位置并将其从字符串的总长度中减去。

于 2020-11-18T22:41:39.553 回答