Tidy up data in field, need function ?

  • Hi,

    I have a SKU field that is TEXT

    In this field I can get data like this

    000000045654

    0000789

    0000000456

    KJGKG

    00HFJJ

    I need to remove all front ZEROs by a function, and return the data as TEXT, any ideas on how to..

    Like

    45654

    789

    456

    KJGKG

    HFJJ

     

  • Off the top of my head, this would probably work:

    CONVERT(TEXT,REPLACE(fieldname,'0',''))

    FROM tablename

  • I'm thinking you'd use something like PATINDEX to locate the first non-zero character and then SUBSTRING from there.

    DECLARE @MyVar varchar(30)
    SET @MyVar = '000000045654'
    SELECT SUBSTRING(@MyVar, PATINDEX('%[^0]%', @MyVar), LEN(@MyVar))
     

    --------------------
    Colt 45 - the original point and click interface

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

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