locked
Trim string based on characters RRS feed

  • Question

  • User-1888080761 posted

    Hi,

    I have strings like 

    WebApp - 10148
    Smart - App - 1458

    Expected Result
    WebApp
    Smart - App

    I want to trim the characters from - to numbers from right.

    I have tried the below query and the result is this
    WebApp
    Smart

    select LEFT(app+' - ', CHARINDEX(' - ',app+' - ')-1) from repository

    Can anyone assist me to sort this?
     

    Monday, March 21, 2016 9:54 AM

Answers

  • User269881539 posted

    I do love a string extraction challenge.

    How about :

    select RTRIM(REVERSE(RIGHT(REVERSE(@app), LEN(@app) - CHARINDEX('-', REVERSE(@app)))))

    Seems to work for both your examples.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 21, 2016 10:07 AM