SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: Change all filename extensions in table?

  1. #1
    SitePoint Zealot
    Join Date
    Oct 2003
    Location
    Tulsa, OK
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Change all filename extensions in table?

    I have a table with a field containing image file names. Some end in .jpg and some in .gif. Is there a SQL-only way that I can change all of the extensions to .gif, or will I have to write a program to loop through and handle each one?

    I guess basically what I need is to search and replace all instances of .jpg with .gif. Any ideas?

  2. #2
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Örebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    A simple UPDATE query should do the trick.

    Code:
    UPDATE table
    SET extension_field = ".gif"
    WHERE extension_field = ".jpg"
    Yours, Erik.

  3. #3
    SitePoint Zealot
    Join Date
    Oct 2003
    Location
    Tulsa, OK
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That would work if the extensions were in separate fields from the rest of the filename, but they're not. The filename and extension are all lumped together as one string. i.e.:

    file1.gif
    file2.gif
    file3.jpg
    file4.gif
    file5.jpg

  4. #4
    SitePoint Wizard Lats's Avatar
    Join Date
    Jun 2003
    Location
    Melbourne, AU
    Posts
    1,142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this...
    Code:
    UPDATE
    	table
    SET
    	filename = replace(filename, '.jpg', '.gif') 
    WHERE
    	filename LIKE '%.jpg%'
    Lats...

  5. #5
    SitePoint Zealot
    Join Date
    Oct 2003
    Location
    Tulsa, OK
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Perfect. I didn't nkow that mySQL had a replace function. Thanks a lot!

  6. #6
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Örebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    MySQL have a lot of built-in functions. Take a look at the manual.

    Yours, Erik.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •