Asynchronous
Asynchronous

Reputation: 3977

TSQL how to remove all characters or text after the last tab space

I have some data that look like this:

Maple Leaf City

This is all in a single column called City.

I want to remove the word City so that the output looks like this:

Maple Leaf

My code below removes the everything after the first space instead of the last.

How do I take this: Maple Leaf City and turn it into Maple Leaf?

This is what I have got:

SELECT LEFT(City, CHARINDEX(' ', City) - 1)
FROM MyTable
WHERE CHARINDEX(' ', City) > 1

My code gives me this: Maple

Upvotes: 1

Views: 1743

Answers (1)

ventik
ventik

Reputation: 887

declare @str varchar(255)
set @str = 'Maple Leaf City'
select LEFT(@str, LEN(@str) - CHARINDEX(' ', REVERSE(@str)))

Upvotes: 2

Related Questions