update first 3 chars

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ddtpmyra
    Contributor
    • Jun 2008
    • 333

    update first 3 chars

    I have records like:

    abc -books
    acc -papers

    now what I wanted to to is to update the first three (3) characters into:

    xxx -books
    xxx -papers

    without doing anything after the dash (-) or leave the 'book' and 'paper'
    I tried to do this way but I got error message:

    MYSQL
    update table1 set LEFT(name,3)='x xx-';
    ERROR:
    Code:
    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LEFT(name,3)='xxx-' where name in ('books','pens')'' at line 1
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hey.

    Try something like:
    [code=sql]UPDATE `stuff`
    SET `field` = CONCAT('xxx', SUBSTRING(`fiel d`, 4));[/code]

    Comment

    • nbiswas
      New Member
      • May 2009
      • 149

      #3
      Solution to update first 3 chars

      Try this

      Query1:
      Code:
      update table1 set data = 'xxx' + SUBSTRING(data,4,LEN(data));
      Query2:
      Code:
      update table1  set data = 'xxx' + RIGHT(data,LEN(data) - 3)

      Output:

      Code:
      data
      xxx -books
      xxx -papers
      Last edited by nbiswas; Jan 6 '10, 02:59 AM. Reason: Added 1 more solution

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        That's a bit dangerous though, nbiswas.
        The REAPLCE function replaces all occurrences of the input string, so if the first three letters are repeated anywhere else in the string, those would get replaced as well.

        Comment

        • ddtpmyra
          Contributor
          • Jun 2008
          • 333

          #5
          thanks for your help Atli!

          Comment

          Working...