November 29, 2013 at 4:25 am
i have a column called: CustomerName in SQL Server:
I am trying to remove the *NA in any customer field. However some customer fields dont have the star.
For example:
West Middlesex Hospital *LN
Norfolk & Norwich University Hospital*EA
West Suffolk NHS Trust *EA
I have used this code:
Select RTRIM(LEFT('CustomerName',CHARINDEX('*','CustomerName')-1))
SELECT CASE WHEN CHARINDEX('*','CustomerName') < 1 THEN
RTRIM('CustomerName') ELSE
RTRIM(LEFT('CustomerName',CHARINDEX('*','CustomerName')-1))END
I appear to be getting the message:
Invalid length parameter passed to the left function.
November 29, 2013 at 4:53 am
You're passing a string as an argument rather than the column name. Remove the Apostophes i.e. CustomerName instead of 'CustomerName'
December 6, 2013 at 10:22 am
Personally I would flip the string, use substring to remove the last 3 characters, then flip it back. eg select reverse(substring(reverse(CustomerName),4,200))
December 6, 2013 at 1:00 pm
How about this:
SELECT REPLACE(CustomerName,'*NA','')
That will remove all instances of '*NA' within CustomerName.
Would that work for you, or is there a chance that the string '*NA' will show up within the string somewhere were you don't want to remove it?
December 9, 2013 at 4:16 pm
SELECT RTRIM(LEFT(CustomerName,CHARINDEX('*',CustomerName + '*')-1))
SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply