Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id D50921F07123 for ; Thu, 26 Jan 2012 20:07:01 -0400 (AST) Received: from cerberus.pinpointresearch.com ([66.7.238.130] helo=polaris.pinpointresearch.com) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1RqZLE-000488-GC for pgsql-sql@postgresql.org; Fri, 27 Jan 2012 00:07:01 +0000 Received: from betelgeuse.pinpointresearch.com (betelgeuse.pinpointresearch.com [192.168.1.179]) by polaris.pinpointresearch.com (Postfix) with ESMTP id 5317AE00EC84; Thu, 26 Jan 2012 16:06:43 -0800 (PST) Message-ID: <4F21EA93.9090202@pinpointresearch.com> Date: Thu, 26 Jan 2012 16:06:43 -0800 From: Steve Crawford User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.24) Gecko/20111109 Lightning/1.0b2 Thunderbird/3.1.16 MIME-Version: 1.0 To: Carlos Mennens CC: "PostgreSQL (SQL)" Subject: Re: Update Mass Data in Field? References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201201/105 X-Sequence-Number: 36276 On 01/26/2012 03:59 PM, Carlos Mennens wrote: > I'm new to SQL so I'm looking for a way to change several email > addresses with one command. For example everyone has a 'holyghost.org' > domain and I need to change a few 100 email addresses in the field > 'emp_email'. I need to UPDATE employees table which has a COLUMN > 'emp_email' and change %holyghost.org to %ghostsoftware.com. > > I tried: > > UPDATE employees SET emp_email = '%ghostsoftware.com' WHERE emp_email > LIKE '%holyghost.org'; > > It didn't update anything when I ran the command. Does anyone know how > I need to correct my SQL statement above to change everyone's email > address? > ...set emp_email = regexp_replace(emp_email, '@holyghost.org$', '@ghostingsoftware.org') where emp_email ~ '@holyghost.org$'... This is using the regular expression match and regexp_replace to ensure that the pattern is anchored at the end of the field and includes the "@" sign in the expression to avoid accidentally matching something like ...@theholyghost.org. You can always do a select of the emp_email alongside the replacement expression to be sure it will do what you want before actually updating your database. Cheers, Steve