Solved

case expression problem

Posted on 2013-01-16
5
442 Views
Last Modified: 2013-01-17
/* this code works,  similar version below does not */
/* some issue with the substr in the case statement*/

select a.bl_id, a.max_bl_id, substr(max_bl_id,5,4), a.title, a.name, m.location, m.description, m.bc_buildcode,
case when a.title = m.bc_buildcode then 'same'
    else 'diff'
    end as "fields same or different"
from bl A
join locations m
on a.title = m.bc_buildcode
order by name;


/*this code does not.  code is the same exception for the 1st line of the case */

select a.bl_id, a.max_bl_id, substr(max_bl_id,5,4), a.title, a.name, m.location, m.description, m.bc_buildcode,
case when substr(max_bl_id,5,4) like substr(m.bc_buildcode,1,4) then 'same'
    else 'diff'
    end as "fields same or different"
from bl A
join locations m
on a.title = m.bc_buildcode
order by name;
0
Comment
Question by:stirus
5 Comments
 
LVL 73

Accepted Solution

by:
sdstuber earned 100 total points
ID: 38784788
what doesn't work?

wrong results? no results? error? keyboard catches fire?


please provide sample data and expected results
0
 
LVL 73

Assisted Solution

by:sdstuber
sdstuber earned 100 total points
ID: 38784793
one question comes to mind immediately though.

Why do you use LIKE in the second query?  Are their wildcards in your strings? If not, it's effectively the same = and should be written that way to be self-documenting
0
 
LVL 23

Assisted Solution

by:paquicuba
paquicuba earned 50 total points
ID: 38784811
sdstuber is right, since you're extracting 4 characters from each string, you don't need to use the LIKE operator. However, if you're planning to extract 4 characters from the first string and 3 characters from the second one, then you can do something like:


select a.bl_id, a.max_bl_id, substr(max_bl_id,5,4), a.title, a.name, m.location, m.description, m.bc_buildcode,
case when substr(max_bl_id,5,4) like substr(m.bc_buildcode,1,3)||'%' then 'same'
    else 'diff'
    end as "fields same or different"
from bl A
join locations m
on a.title = m.bc_buildcode
order by name;
0
 
LVL 37

Assisted Solution

by:Geert Gruwez
Geert Gruwez earned 50 total points
ID: 38786066
if you want case insensitive check, then add upper() too

select a.bl_id, a.max_bl_id, substr(max_bl_id,5,4), a.title, a.name, m.location, m.description, m.bc_buildcode,
  decode(upper(substr(max_bl_id,5,4))=upper(substr(m.bc_buildcode,1,4)),'same','diff') 
  "fields same or different"
from bl A
join locations m
on a.title = m.bc_buildcode
order by name; 

Open in new window

0
 

Author Closing Comment

by:stirus
ID: 38787546
My error.  comparing two unlike fields.  thanks for your response.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: Swadhin
From the Oracle SQL Reference (http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/queries006.htm) we are told that a join is a query that combines rows from two or more tables, views, or materialized views. This article provides a glimps…
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question