Quantcast
Viewing all articles
Browse latest Browse all 3160

Obtain a truncated substring in SQL Server

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?

Reverse the string, then find the position of the first space, then you know how far form the end the last space is.

SELECT SUBSTRING( BrokerName, 1, LEN(BrokerName) - CHARINDEX(' ', REVERSE(BrokerName)) ) FROM #BrokerNameT


Viewing all articles
Browse latest Browse all 3160

Trending Articles