1

Question for everyone... I have some values I've inserted into a table as I try to play around with string functions. What I'm trying to do is remove the end of the string and keep everything before it:

CREATE TABLE #BrokerNameT
  (
     BrokerName VARCHAR(100)
  )

INSERT INTO #BrokerNameT
            (BrokerName)
VALUES      ('Peter Pan Co Cash'),
            ('Batman Co Cash'),
            ('Spiderman Algo'),
            ('Spiderman Cash') 

Here what I'm looking to is simply return: 'Peter Pan Co', 'Batman Co', and 'Spiderman'

Is it possible to search for the first space from the end of a string? If i was able to do that, couldn't I just then keep everything before the first space?

Any idea how I'd go about doing this?

Could I use two reverses to achieve this?

4

2 回答 2

4

反转字符串,然后找到第一个空格的位置,然后您就知道最后一个空格距离末尾有多远。

SELECT
  SUBSTRING(
    BrokerName,
    1,
    LEN(BrokerName) - CHARINDEX(' ', REVERSE(BrokerName))
  )
FROM
  #BrokerNameT
于 2013-06-14T15:49:43.033 回答
0

这是一个查询:

select *,
       left(BrokerName, len(BrokerName) - charindex(' ', reverse(BrokerName))
           ) as AllButLast
from BrokerNameT

是一个 SQLFiddle 来演示它。

于 2013-06-14T15:52:10.600 回答