Author |
Topic |
andreas.zaras
Starting Member
2 Posts |
Posted - 2011-03-09 : 04:10:44
|
Hello,I am trying to create a function that counts the characters between two slashes in a string. I am writing the follwings lines:create function priceobserver.fourth_node(@variable nvarchar(100))returns int(50)WITH EXECUTE AS CALLERASBEGINdeclare @length int(50)select @length=(charindex('/',@variable,charindex('/',@variable,charindex('/',@variable,charindex('/',@variable)+1)+1)+1)-1),(charindex('/',@variable,charindex('/',@variable,charindex('/',@variable,charindex('/',@variable)+1)+1)+1)-1)RETURN(ISNULL(@length,0))end GOWhat is worng woth that?I get the following error message: Msg 141, Level 15, State 1, Procedure fourth_node, Line 8A SELECT statement that assigns a value to a variable must not be combined with data-retrieval operations.Thanks in advance |
|
Lumbago
Norsk Yak Master
3271 Posts |
Posted - 2011-03-09 : 04:16:01
|
Replace the SELECT with SET- LumbagoMy blog-> http://thefirstsql.com/2011/02/07/regular-expressions-advanced-string-matching-and-new-split-function-sql-server-2008-r2/ |
 |
|
andreas.zaras
Starting Member
2 Posts |
Posted - 2011-03-09 : 04:23:46
|
Hello,Thanks for your answer. The previous error is gone but i get the following:Msg 102, Level 15, State 1, Procedure fourth_node, Line 8Incorrect syntax near ','.Though if i run seperately the following (which is the same) i get correct result.select (charindex('/',node_name,charindex('/',node_name,charindex('/',node_name,charindex('/',node_name)+1)+1)+1)-1)-(charindex('/',node_name,charindex('/',node_name,charindex('/',node_name)+1)+1)+2)from priceobserver.getproductstructurenameshierarchy()ANy ideas?Thnaks again |
 |
|
Lumbago
Norsk Yak Master
3271 Posts |
Posted - 2011-03-09 : 04:40:10
|
You have a comma in your function but a minus in your working query:create function priceobserver.fourth_node (@variable nvarchar(100))returns int(50)WITH EXECUTE AS CALLERASBEGINdeclare @length int(50)set @length = (charindex('/',@variable,charindex('/',@variable,charindex('/',@variable,charindex('/',@variable)+1)+1)+1)-1), <-- See here (charindex('/',@variable,charindex('/',@variable,charindex('/',@variable,charindex('/',@variable)+1)+1)+1)-1)RETURN(ISNULL(@length,0))end - LumbagoMy blog-> http://thefirstsql.com/2011/02/07/regular-expressions-advanced-string-matching-and-new-split-function-sql-server-2008-r2/ |
 |
|
Lumbago
Norsk Yak Master
3271 Posts |
Posted - 2011-03-09 : 04:43:21
|
And it seems you need to brush up on your copy/paste skills.This:(charindex('/',@variable,charindex('/',@variable,charindex('/',@variable,charindex('/',@variable)+1)+1)+1)-1)-(charindex('/',@variable,charindex('/',@variable,charindex('/',@variable,charindex('/',@variable)+1)+1)+1)-1)is still pretty different from this(charindex('/',node_name,charindex('/',node_name,charindex('/',node_name,charindex('/',node_name)+1)+1)+1)-1)-(charindex('/',node_name,charindex('/',node_name,charindex('/',node_name)+1)+1)+2)- LumbagoMy blog-> http://thefirstsql.com/2011/02/07/regular-expressions-advanced-string-matching-and-new-split-function-sql-server-2008-r2/ |
 |
|
|
|
|