SQL Server: Trimming the final word from all entries in a table

Hey folks, I'm stuck with a database issue. I've got a table called CustomerFeedback with a column named Feedback. When I run:

SELECT Feedback FROM CustomerFeedback

I get results like:

2023/05/15 10:30:45 AM - Smith, John Great service, fast delivery Satisfied
2023/05/15 10:29:30 AM - Doe, Jane Product was damaged Unhappy
2023/05/15 10:28:15 AM - Brown, Mike Exactly what I ordered Perfect

I need to chop off that last word from each row. So the output should look like:

2023/05/15 10:30:45 AM - Smith, John Great service, fast delivery
2023/05/15 10:29:30 AM - Doe, Jane Product was damaged
2023/05/15 10:28:15 AM - Brown, Mike Exactly what I ordered

Any SQL wizards out there who can help me craft a query to do this? Thanks in advance!

I’ve encountered a similar situation before, and I found that using the REVERSE function in combination with CHARINDEX and LEFT can effectively solve this problem. Here’s a query that should work for you:

SELECT LEFT(Feedback, LEN(Feedback) - CHARINDEX(’ ', REVERSE(Feedback)))
FROM CustomerFeedback

This query reverses the string, finds the position of the first space (which is actually the last space in the original string), and then uses that to determine how many characters to keep from the left side of the original string.

Keep in mind that this assumes there’s always at least one space in your Feedback entries. If that’s not guaranteed, you might need to add some additional error handling logic.

hey, u could try something like this:

SELECT SUBSTRING(Feedback, 1, LEN(Feedback) - CHARINDEX(’ ', REVERSE(Feedback)))
FROM CustomerFeedback

it basically finds the last space and cuts off everything after it. hope this helps!

ooh, interesting problem! have u considered using STUFF? like this:

SELECT STUFF(Feedback, LEN(Feedback) - CHARINDEX(’ ', REVERSE(Feedback)) + 1, LEN(Feedback), ‘’)
FROM CustomerFeedback

what do you think? it might be a bit more efficient for larger datasets. curious to hear ur thoughts!