Sql issue

  • 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.

  • You're passing a string as an argument rather than the column name. Remove the Apostophes i.e. CustomerName instead of 'CustomerName'

  • 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))

  • 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?

  • SELECT RTRIM(LEFT(CustomerName,CHARINDEX('*',CustomerName + '*')-1))

    SQL DBA,SQL Server MVP(07, 08, 09) A socialist is someone who will give you the shirt off *someone else's* back.

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply