May 24, 2002 at 3:33 pm
Could someone please help me with this syntax?
update #temploadpac set city_state = left(city_state, len(city_state)-3)
basically I need to set the city_state field equal to itself minus the rightmost 3 characters. I get an error that says "Invalid length parameter passed to the substring function".
Thanks!!
May 24, 2002 at 3:43 pm
Actually I need to do the opposite. I need to set city_state = to its length minus 3 characters, as opposed to setting its length equal to 3 characters.
I believe it has to do with handling 0 length strings - but I can't remember how to do that in this context...
Thanks!
May 24, 2002 at 3:46 pm
You need to check the length of the city_state field before you take the leftmost chars. There is probably one record (or more) with a city_state length of 3 or less, causing the left function to fail.
Try this:
set city_state = case when len(ltrim(city_state)) > 3 then left(city_state, len(ltrim(city_state))-3) else ltrim(city_state) end
Steve Armistead,
Database Administration
Panther Systems Northwest, Inc.
Vancouver, WA
Steve
May 24, 2002 at 3:51 pm
Thanks, thats what I was trying to remember! I am getting an error now b/c I have a where clause after the end of the case statement - the error says incorrect syntax near the 'where' clause - does the case statement have to be "ended" somehow before using the where clause?
Thanks!
May 24, 2002 at 4:00 pm
What I have is ....
update #temploadpac set city_state = CASE WHEN Len(CityState) < 4 Then CityState
ELSE Left(CityState, Len(CityState)-3) where city_state is not null
I'm getting a syntax error on the "where" clause. Thanks
May 24, 2002 at 4:14 pm
I tried adding the "end, " to the code, but I am still getting the same error. Any other ideas? Thanks!
May 24, 2002 at 4:20 pm
I don't think I need a (from tablename) statement in an update clause...
May 24, 2002 at 4:22 pm
remove the final comma after END
Steve
May 28, 2002 at 8:16 am
having the comma in or out both give me errors, two different errors, but errors.
We are soo close!! Thank you for your help!!
May 28, 2002 at 9:14 am
Remove the comma after the end and have a consistent column name i.e. city_state not CityState see below:-
update #temploadpac
set city_state =
CASE WHEN Len(city_state) < 4
Then city_state
ELSE Left(city_state , Len(city_state)-3)
END
where city_state is not null
Regards,
Andy Jones
.
May 28, 2002 at 9:52 am
Thank you for pointing that out - I had actually not corrected the syntax on my field name. I will give that a try and see if it corrects the error I was getting.
May 28, 2002 at 1:59 pm
Thank you everyone. Once I re-wrote the fieldnames so they all matched again, everything works - no comma after 'end'.
Thanks to everyone for their help!
May 29, 2002 at 6:05 am
Update #temploadpack
SET city_state=
WHEN LEN(city_state) > 3 then
LEFT(city_state,(LEN(city_state)-3))
WHEN LEN(city_state) < 3 then
city_state
END
from #temploadpack
where city_state is not null
Shrinivas L.K.
Shrinivas L.K.
Viewing 13 posts - 1 through 12 (of 12 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy