Solved

case expression problem

Posted on 2013-01-16
5
443 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 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

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

Title # Comments Views Activity
Oracle SQL 6 73
oracle 11g 23 107
oracle- set role and grant privileges 6 40
PL/SQl Expanding the WHERE statement in query 3 20
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

829 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