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 2000 Forums
 Transact-SQL (2000)
 Trimming characters

Author  Topic 

hog
Constraint Violating Yak Guru

284 Posts

Posted - 2005-05-19 : 07:03:37
I cannot see why this does not work?

@Item_No = left(@Item_No, len(@Item_No)-2)

so if @Item_No = anytext%'

I would get anytext as the result, but all I get is incorrect parameter passed to substring function?

raclede
Posting Yak Master

180 Posts

Posted - 2005-05-19 : 07:07:33
forgot to include keyword SET or SELECT
set @Item_No = left(@Item_No, len(@Item_No)-2)
select @Item_No

select @Item_No = left(@Item_No, len(@Item_No)-2)
select @Item_No


Cursors are for those who doesn't know how to use SQL

raclede™
Go to Top of Page

hog
Constraint Violating Yak Guru

284 Posts

Posted - 2005-05-19 : 07:43:01
Sorry, typo on my part. In my code I do have set as you have it. Still get the error?
Go to Top of Page

hog
Constraint Violating Yak Guru

284 Posts

Posted - 2005-05-19 : 07:45:07
The exact error is 'invalid length parameter passed to substring function'
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2005-05-19 : 07:57:28
len of emty column is 0 so by subtracting 2 you get -2 which is invalid.

Go with the flow & have fun! Else fight the flow
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-05-19 : 08:13:22
yes, as spirit1 said, you need to check this

if len(@Item_No)>=2
Begin
select @Item_No = left(@Item_No, len(@Item_No)-2)
select @Item_No
end

Madhivanan

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

hog
Constraint Violating Yak Guru

284 Posts

Posted - 2005-05-19 : 09:58:25
Mmm it works so I can't grumble there, but am a bit confused as doesn't the line above assign a value to @item_no?
Go to Top of Page

hog
Constraint Violating Yak Guru

284 Posts

Posted - 2005-05-19 : 09:59:47
Can you also explain why you use select and not set and then select again after the assignment please...
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-05-19 : 10:15:54
select @Item_No = left(@Item_No, len(@Item_No)-2) is same as that of set
It will assign the value and you have to select it again


Madhivanan

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

hog
Constraint Violating Yak Guru

284 Posts

Posted - 2005-05-19 : 10:24:08
Gotcha.

I modified it to this;

if len(@Item_No)>=2
select @Item_No = left(@Item_No, len(@Item_No)-2)

as I have other code after it. as it was it stop everything else running, but now it works a treat. Thanks
Go to Top of Page
   

- Advertisement -