Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
I'm trying to use this statement to find the last word in a string. SELECT RIGHT(Ad1, CHARINDEX(' ', REVERSE(Ad1))-1)FROM AddressIt works fine but only on 15 rows and then I get this error:Server: Msg 536, Level 16, State 1, Line 1Invalid length parameter passed to the substring function.If I try using a select top 5 and then run it it works fine. Anyone know why this is happening?There is 14.5 million records in the table I'm querying is that something to do with it?Thanks for any help
spirit1
Cybernetic Yak Master
11752 Posts
Posted - 2005-04-12 : 08:45:10
what's the value of the 16th row? null or empty sttring?because then CHARINDEX(' ', REVERSE(Ad1))-1 would return either null or 0Go with the flow & have fun! Else fight the flow
sezzy
Starting Member
3 Posts
Posted - 2005-04-12 : 08:51:13
I've just had a look and the 15th row has just one word in it with no space, so I think that is causing the problem! Dunno how to get round that problem as I want it to pick up those words aswell.Thanks
spirit1
Cybernetic Yak Master
11752 Posts
Posted - 2005-04-12 : 08:59:55
will this do?
declare @table table (ad1 varchar(100))insert into @tableselect 'gwegew 543252' union allselect 'twgese' union allselect 'g4wga gfw4t4w 4325wetey' union allselect '' SELECT RIGHT(Ad1, case when CHARINDEX(' ', REVERSE(Ad1)) > 1 then CHARINDEX(' ', REVERSE(Ad1))-1 else len(Ad1) end)FROM @table