locked
Removing a character from data returned in some but not all instances RRS feed

  • Question

  • Hello

    I have a field that will in some instances return data 12 characters long:

    XX1234567891

    and in some instances 13 characters long:

    XXX1234567891

    What I need to be able to do is if the data returned is 13 characters long, remove the first character on the left; however I need to ensure that in the instances where the data returned is 12 characters long then those 12 characters remain as is - that the first character  the left is not removed.

    So in the two examples above the output needs to be:

    XX1234567891

    XX1234567891

    Please can you advise how I go about doing this?  Thank you.



    Monday, May 11, 2020 8:17 AM

Answers

  • Please try this,with len function we can do this

    declare @Name varchar(13)
    set @Name = 'XXX1234567891'

    select case when len(@Name) > 12 then right(@Name,12) else @Name end Name


    http://uk.linkedin.com/in/ramjaddu

    Monday, May 11, 2020 8:24 AM

All replies

  • Please try this,with len function we can do this

    declare @Name varchar(13)
    set @Name = 'XXX1234567891'

    select case when len(@Name) > 12 then right(@Name,12) else @Name end Name


    http://uk.linkedin.com/in/ramjaddu

    Monday, May 11, 2020 8:24 AM
  • Perfect: thank you for taking the trouble to get back so quickly.
    Monday, May 11, 2020 8:34 AM
  • You can use RIGHT function directly for your varchar column.

    select  right(@Name,12)

    Monday, May 11, 2020 3:18 PM