need help on whitespace

  • I want to remove leading whitespace from a string only if the string has one leading whitespace , if two leading whitespaces then do not ltrim. Can this be done in tsql I'm using 2008

  • Hi)

    You can use wild cards to define if there is one whitespace or more:

    declare @a nvarchar(50)

    set @a=' with one leading whitespace'

    select @a=case when @a like ' [^ ]%' then LTRIM(@a) end

    print @a

    ' [^ ]%' - means that the first character of string should be the whitespace

    second character of string shouldn't be the whitespace

    other characters are arbitrary.

  • Hi,

    sorry if I was not clear in my request above,

    Basically what I'm looking for is-

    I have string with value "HPJP00000013"

    if this field value has one LEADING SPACE /WHITE SPACE i.e. " HPJP00000013" then TRIM it out BUT if there are 2 LEADING SPACES/WHOTE SPACES i.e. " HPJP00000013" then do not do any trimming.

    if I use LTRIM then it trims out all LEADING SPACES/WHITE SPACES which is not what i need.

    If you can please help me with this would be much obliged since I'm very new to TSQL world .

    Kind Regards

    Dhananjay

  • Hi,

    seems the code you gave is workign just tried-

    //

    declare @a nvarchar(50)

    set @a= 'HPJP00000013'

    select @a=case when @a like ' [^ ]%' then LTRIM(@a) end

    print @a

    //

    When I put one leading space then it lTRIMS it out if I out 2 or more leading spaces then it does LTRIM it out.

    Thanks so much foer the help.

    thanks

    DJ

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

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