Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 String manipulation question

Author  Topic 

Abu-Dina
Posting Yak Master

206 Posts

Posted - 2008-03-31 : 11:23:51
Greetings all,

What's the neatest way to get the whole string but the last word?

e.g.

'THE CAT SAT ON THE MAT' would become 'THE CAT SAT ON THE'

My solution :


select left(@test, len(@test)-charindex(' ', reverse(@test)))


Thanks in advance.

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2008-03-31 : 12:15:38
Mine isn't any different than yours. I don't know if there are any inherent advantages or disadvantages of using SUBSTRING over LEFT.

SELECT SUBSTRING(@q,1,LEN(@q) - CHARINDEX(' ',REVERSE(@q)))

Jim
Go to Top of Page
   

- Advertisement -