Difference between revisions of "String over columns"

From SQLZOO
Jump to: navigation, search
 
(One intermediate revision by one user not shown)
Line 1: Line 1:
 
Normally a string can be searched for across many columns using the <code>OR</code> function but this is not efficient as this could lead to errors while using
 
Normally a string can be searched for across many columns using the <code>OR</code> function but this is not efficient as this could lead to errors while using
a <code>CONCAT</code> function ( || for oracle, + for sqlserver) could do the same task but could get rid of any risk of errors.
+
a <code>CONCAT</code> function ( || for oracle, + for sqlserver) could do the same task but can get rid of the risk of errors
 
<div class='ht'>
 
<div class='ht'>
 
<div class=params>schema:scott</div>
 
<div class=params>schema:scott</div>
Line 16: Line 16:
 
<div>
 
<div>
 
  SELECT name FROM bedrooms
 
  SELECT name FROM bedrooms
WHERE floorcolor = 'YELLOW'
+
  WHERE floorcolor = 'YELLOW'
OR ceilingcolor = 'YELLOW'
+
    OR ceilingcolor = 'YELLOW'
OR wallcolor = 'YELLOW'
+
    OR wallcolor = 'YELLOW'
 
<p>Using <code>OR</code> increases the chances for careless mistakes instead
 
<p>Using <code>OR</code> increases the chances for careless mistakes instead
 
<code>CONCAT</code> could be used to make the query more efficient.</p>
 
<code>CONCAT</code> could be used to make the query more efficient.</p>
Line 25: Line 25:
 
COALESCE(floorcolor,' ').
 
COALESCE(floorcolor,' ').
 
</div>
 
</div>
<source lang='sql' class='def e-oracle'>SELECT name FROM bedrooms
+
<source lang='sql' class='def e-oracle'>
WHERE ':' || floorcolor || ':' || ceilingcolor || ':' || wallcolor || ':'
+
SELECT name FROM bedrooms
LIKE '%:YELLOW:%'</source>
+
WHERE ':' || floorcolor || ':' || ceilingcolor || ':' || wallcolor || ':'
<source lang='sql' class='def e-sqlserver'>SELECT name FROM bedrooms
+
  LIKE '%:YELLOW:%'</source>
WHERE ':' + floorcolor + ':' + ceilingcolor + ':' + wallcolor + ':'
+
<source lang='sql' class='def e-sqlserver'>
LIKE '%:YELLOW:%'</source>
+
SELECT name FROM bedrooms
<source lang='sql' class='def'>SELECT name FROM bedrooms
+
WHERE ':' + floorcolor + ':' + ceilingcolor + ':' + wallcolor + ':'
WHERE CONCAT (':',floorcolor,':',ceilingcolor,':',wallcolor,':')
+
  LIKE '%:YELLOW:%'</source>
LIKE '%:YELLOW:%'</source>
+
<source lang='sql' class='def'>
 +
SELECT name FROM bedrooms
 +
WHERE CONCAT (':',floorcolor,':',ceilingcolor,':',wallcolor,':')
 +
  LIKE '%:YELLOW:%'</source>
 
<div class="ecomm e-mysql" style="display: none">Instead of <code>COALESCE</code> or <code>NVL</code>, <code>IFNULL</code> can be used.</div>
 
<div class="ecomm e-mysql" style="display: none">Instead of <code>COALESCE</code> or <code>NVL</code>, <code>IFNULL</code> can be used.</div>
 
<div class="ecomm e-sqlserver" style="display: none">Instead of <code>COALESCE</code> or <code>NVL</code>, <code>IFNULL</code> can be used.</div>
 
<div class="ecomm e-sqlserver" style="display: none">Instead of <code>COALESCE</code> or <code>NVL</code>, <code>IFNULL</code> can be used.</div>
 
</div>
 
</div>
 
{{Hacks Ref}}
 
{{Hacks Ref}}

Latest revision as of 16:01, 2 August 2012

Normally a string can be searched for across many columns using the OR function but this is not efficient as this could lead to errors while using a CONCAT function ( || for oracle, + for sqlserver) could do the same task but can get rid of the risk of errors

schema:scott
DROP TABLE bedrooms
 CREATE TABLE bedrooms(
  name VARCHAR(20),
  floorcolor VARCHAR(20),
  ceilingcolor VARCHAR(20),
  wallcolor VARCHAR(20));
INSERT INTO bedrooms VALUES ('Jim','RED','GREEN','YELLOW');
INSERT INTO bedrooms VALUES ('Bob','YELLOW','BLUE','BLACK');
INSERT INTO bedrooms VALUES ('Allan','BLUE','PINK','BLACK');
INSERT INTO bedrooms VALUES ('George','BLUE','GREEN','OAK');
SELECT name FROM bedrooms
 WHERE floorcolor = 'YELLOW'
    OR ceilingcolor = 'YELLOW'
    OR wallcolor = 'YELLOW'

Using OR increases the chances for careless mistakes instead CONCAT could be used to make the query more efficient.

To avoid problems make sure to add in separators and if a value can be null make sure to use it is wrapped in COALESCE or NVL for example:

COALESCE(floorcolor,' ').

SELECT name FROM bedrooms
 WHERE ':' || floorcolor || ':' || ceilingcolor || ':' || wallcolor || ':'
  LIKE '%:YELLOW:%'
SELECT name FROM bedrooms
 WHERE ':' + floorcolor + ':' + ceilingcolor + ':' + wallcolor + ':'
  LIKE '%:YELLOW:%'
SELECT name FROM bedrooms
 WHERE CONCAT (':',floorcolor,':',ceilingcolor,':',wallcolor,':')
  LIKE '%:YELLOW:%'
Personal tools
Namespaces

Variants
Actions
Reference
Toolbox
Google AdSense