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)
 Why LEN function does not work in SUBSTRING?

Author  Topic 

danyeung
Posting Yak Master

102 Posts

Posted - 2007-10-31 : 12:39:49
I tried this and worked. It printed out 'somewhere'

declare @str as char(20)
set @str = 'region - somewhere'
print substring(@str, 10, len(@str) - 9)


Why I used the same syntax in a where clause and it didn't work? I got an error "Invalid length parameter passed to the SUBSTRING function."

WHERE substring(nvchrName, 10, LEN(nvchrName) - 9) = nvchrRegion

Thanks.
DanYeung

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-10-31 : 12:44:14
your string in nvchrName is less than 10


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

danyeung
Posting Yak Master

102 Posts

Posted - 2007-10-31 : 13:40:04
You are right. Thanks.

DanYeung
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2007-10-31 : 13:42:03
len excludes the space charaters at the end of the string. Substring errors if there is a length less than zeo.
The error you are gettnig means there is at least one row in the table with that column too short for the substring.
why not
WHERE substring(nvchrName, 10, 1000) = nvchrRegion

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-11-01 : 02:34:35
or

nvchrName like '%'+nvchrRegion

??

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -