Replacing characters

  • Hi Guys,

    Im pretty new to SQL and I need to perform a task where I have to replace the leading 0 with 44.

    E.g 01329225558 would become 441328225558.

    I have tried many times but all of my results end up in me changing every 0 in the data.

    Any ideas how to just change this leading 0.

    Cheers

    matt

  • UPDATE [TableName]

    SET [ColumnName] = '44' + RIGHT([ColumnName], LEN([ColumnName]) - 1)

    Add...

    WHERE [ColumnName] LIKE '0%'

    ...if not all entries begin with 0 and so you need to restrict to the ones that do, otherwise don't.

  • Another way

    UPDATE [TableName]

    SET [ColumnName] = STUFF([ColumnName],1,1,'44')

    WHERE [ColumnName] LIKE '0%'

    ____________________________________________________

    Deja View - The strange feeling that somewhere, sometime you've optimised this query before

    How to get the best help on a forum

    http://www.sqlservercentral.com/articles/Best+Practices/61537
  • Thanks Guys,

    Works a treat!!

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

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