Solved

case expression problem

Posted on 2013-01-16
5
445 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 74

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 74

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video shows how to recover a database from a user managed backup

705 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