<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.2.1" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: Conditional UPDATE And DELETE In PostgreSQL</title>
	<link>http://blog.charcoalphile.com/2008/04/04/conditional-update-and-delete-in-postgresql/</link>
	<description>On Databases, Recovery, Tech</description>
	<pubDate>Mon, 06 Sep 2010 10:23:38 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.2.1</generator>

	<item>
		<title>By: Ian Eure</title>
		<link>http://blog.charcoalphile.com/2008/04/04/conditional-update-and-delete-in-postgresql/#comment-322</link>
		<author>Ian Eure</author>
		<pubDate>Fri, 04 Apr 2008 22:03:53 +0000</pubDate>
		<guid>http://blog.charcoalphile.com/2008/04/04/conditional-update-and-delete-in-postgresql/#comment-322</guid>
		<description>CASE appears to be part of the SQL standard. It’s definitely implemented in MySQL, and certainly not something that’s specific to PostgreSQL.

It’s really much more useful in stored procedures. The UPDATE above could also be done as:
UPDATE `distances` SET `measurement` = 'kilometers' WHERE `name` LIKE '%km.' OR `name` LIKE '%kilometers' OR `name` LIKE '%K';
UPDATE `distances` SET `measurement` = 'miles' WHERE `name` LIKE '%mi.' OR `name` LIKE '%miles' OR `name` LIKE '%M';

Which is to say that it doesn’t get you anything except that you can do it in a single query. You may want to avoid that to improve the legibility of your code. You can further refine it, if you use 'k' and 'm' as your identifiers, rather than the full word. This works in PostgreSQL, but not MySQL (it doesn’t have REGEXP_REPLACE()):
UPDATE `distances` SET `measurement` = REGEXP_REPLACE('[^km]', `name`, '');

The delete query is even more straightforward, and you would certainly prefer WHERE over CASE for readability:
DELETE FROM `table` WHERE `col1` ~* '^foo&#124;bar$' OR `col2` ~* '^oof&#124;rab$';</description>
		<content:encoded><![CDATA[<p>CASE appears to be part of the SQL standard. It’s definitely implemented in MySQL, and certainly not something that’s specific to PostgreSQL.</p>
<p>It’s really much more useful in stored procedures. The UPDATE above could also be done as:<br />
UPDATE `distances` SET `measurement` = &#8216;kilometers&#8217; WHERE `name` LIKE &#8216;%km.&#8217; OR `name` LIKE &#8216;%kilometers&#8217; OR `name` LIKE &#8216;%K&#8217;;<br />
UPDATE `distances` SET `measurement` = &#8216;miles&#8217; WHERE `name` LIKE &#8216;%mi.&#8217; OR `name` LIKE &#8216;%miles&#8217; OR `name` LIKE &#8216;%M&#8217;;</p>
<p>Which is to say that it doesn’t get you anything except that you can do it in a single query. You may want to avoid that to improve the legibility of your code. You can further refine it, if you use &#8216;k&#8217; and &#8216;m&#8217; as your identifiers, rather than the full word. This works in PostgreSQL, but not MySQL (it doesn’t have REGEXP_REPLACE()):<br />
UPDATE `distances` SET `measurement` = REGEXP_REPLACE(&#8217;[^km]&#8217;, `name`, &#8221;);</p>
<p>The delete query is even more straightforward, and you would certainly prefer WHERE over CASE for readability:<br />
DELETE FROM `table` WHERE `col1` ~* &#8216;^foo|bar$&#8217; OR `col2` ~* &#8216;^oof|rab$&#8217;;</p>
]]></content:encoded>
	</item>
</channel>
</rss>
