Get the last word of a part of a varchar (LEFT/RIGHT)

后端 未结 1 344
孤独总比滥情好
孤独总比滥情好 2021-01-13 15:55

What is the correct way to get the last word of a part of a varchar?

DECLARE @desc varchar(100)
SET @desc=\'EXCHANGEUNIT P1i / SILVERBLACK/ CYRILLIC\'

SELEC         


        
相关标签:
1条回答
  • 2021-01-13 16:36

    Use a combination of REVERSE, LEFT and CHARINDEX - like so:

    DECLARE @desc varchar(100)
    SET @desc='EXCHANGEUNIT P1i / SILVERBLACK/ CYRILLIC'
    SET @subdesc=RTRIM(LEFT(@desc, CHARINDEX('/', @desc) - 1))
    
    
    SELECT REVERSE( LEFT( REVERSE(@subdesc), CHARINDEX(' ', REVERSE(@subdesc))-1 ) )
    
    0 讨论(0)
提交回复
热议问题