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.
Author |
Topic |
Adstra
Starting Member
2 Posts |
Posted - 2013-03-27 : 17:45:16
|
If I run the following query, I get results without issue:select Firstname, right(Firstname,(CHARINDEX(' ', reverse(FirstName))-1)), len(right(Firstname,(CHARINDEX(' ', reverse(FirstName))-1))) from CustomersHowever if I run this query:select * from customers where len(right(Firstname,(CHARINDEX(' ', reverse(FirstName))-1))) > 15I get this error:Invalid length parameter passed to the RIGHT function.State: 42000, Native: 536, Source: Microsoft SQL Native ClientCould someone illuminate me on the problem and point me towards a solution.Thank you. |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2013-03-27 : 17:52:32
|
Most probably with a first name that does not have a space. Then CHARINDEX returns 0 and substraction 1 gives -1, and this value is invalid for substring/left/right function. N 56°04'39.26"E 12°55'05.63" |
|
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-03-27 : 18:05:49
|
To add to what SwePeso said, your first query is not showing an error probably because you have (some other) where clause that happens to eliminate all firstnames without spaces in them. |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2013-03-27 : 18:25:44
|
quote: Originally posted by Adstra Could someone illuminate me on the problem and point me towards a solution.
DECLARE @FirstName VARCHAR(100) = 'Test';-- Errorselect len(right(@FirstName,(CHARINDEX(' ', reverse(@FirstName))-1)))--One solution - check for a charindex value of 0.select len(right(@FirstName,(NULLIF(CHARINDEX(' ', reverse(@FirstName)), 0) -1))) |
|
|
Adstra
Starting Member
2 Posts |
Posted - 2013-03-28 : 10:36:57
|
Thank you, I found the problem I had 1 record return a zero. |
|
|
|
|
|
|
|