Updating Character String with sql

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Gmuchan
    New Member
    • Nov 2007
    • 2

    Updating Character String with sql

    Hi,


    Help needed please.

    Basically I am wanting to update a 16 digit character string which is in a table called say accounts.

    If the account no. field is say 050256238956874 5 I want to amend every record by taking away the leading zero of the account no. and replace it with a 9.

    So the result would be 950256238956874 5 and I want to run this so that it will update every account no. in the accounts table.

    I know its an update statement but wasn't sure how to pull the first character in the string out and whether I used the replace statement as well?!

    I am new to sql and have been trawling the net and books for answers but come up with nothing as yet so any help would be amazing!!!

    Thanks

    Graeme
  • iburyak
    Recognized Expert Top Contributor
    • Nov 2006
    • 1016

    #2
    Try this:
    Code:
    update accounts
    set [account no] = '9' + substring([account no],2,100)
    where substring([account no],1,1) = '0'
    Good Luck

    Comment

    • ck9663
      Recognized Expert Specialist
      • Jun 2007
      • 2878

      #3
      Originally posted by iburyak
      Try this:
      Code:
      update accounts
      set [account no] = '9' + substring([account no],2,100)
      where substring([account no],1,1) = '0'
      Good Luck

      note:
      this will only replace if the first digit is '0' and will only replace the first digit. if you're expecting 2 or more '0'-prefix, you're going to do some coding for that one...

      Comment

      • Gmuchan
        New Member
        • Nov 2007
        • 2

        #4
        Thanks for the help guys.


        That was hugely useful!!!!


        G

        Comment

        Working...