Solved

to concatenate with Null

Posted on 2003-12-01
10
767 Views
Last Modified: 2013-12-09
Hi,
a question,

in a table I have two Fields, ID_PESSOA and ID_PESSOA_PAI.
in some cases, the field ID_PESSOA_PAI is null.

How can I make to concatenate the two fields, and case the field ID_PESSOA_PAI is null, return only ID_PESSOA ????

ex:
P.ID_PESSOA = 10;
P.ID_PESSOA = 20;
(P.ID_PESSOA || ' \ ' || P.ID_PESSOA_PAI) PATH
PATH = 10 \ 20;

P.ID_PESSOA = 10;
P.ID_PESSOA_PAI = null;
(P.ID_PESSOA || ' \ ' || P.ID_PESSOA_PAI) PATH
PATH = 10 \;


Thank's
Rafael Schardosin
Porto Alegre - Brazil


0
Comment
Question by:schardosin
[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
10 Comments
 
LVL 19

Expert Comment

by:Gary Benade
ID: 9853770
There may be other ways, but all I can think of is this. I hope I understood your question correctly....

select cast( P.ID_PESSOA || ' \ ' || P.ID_PESSOA_PAI as char(25)) result_str from test
where P.ID_PESSOA is not null and P.ID_PESSOA_PAI is not null
union all
select cast( P.ID_PESSOA as char(25)) result_str from test
where P.ID_PESSOA is not null and P.ID_PESSOA_PAI is null
union all
select cast( P.ID_PESSOA_PAI as char(25)) result_str from test
where P.ID_PESSOA is null and P.ID_PESSOA_PAI is not null

Regards
Gary
0
 
LVL 19

Expert Comment

by:Gary Benade
ID: 9853787
When I re-read your question, I saw that only P.ID_PESSOA_PAI could be NULL, so you can shorten the above to this.

select cast( P.ID_PESSOA || ' \ ' || P.ID_PESSOA_PAI as char(25)) result_str from test
where P.ID_PESSOA_PAI is not null
union all
select cast( P.ID_PESSOA as char(25)) result_str from test
where P.ID_PESSOA_PAI is null
0
 

Author Comment

by:schardosin
ID: 9857236
Thank´s,

but the solution more optimized is other,
reading the Firebird Help I  get the following solution:
select
CASE WHEN PP.ID_PESSOA is null then P.ID_PESSOA ELSE PP.ID_PESSOA || ' \ ' || P.ID_PESSOA END PATH
from
  V_PESSOA P left outer join
  V_PESSOA PP on PP.ID_PESSOA = P.ID_PESSOA_PAI


Thank´s
Rafael Schardosin
Porto Alegre - Brazil
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 19

Expert Comment

by:Gary Benade
ID: 9857616
I have never heard of the Select CASE statement for FB 1.0.x? Must be a new 1.5 feauture...

Surely all you need to do is

select
CASE WHEN P.ID_PESSOA_PAI is null then P.ID_PESSOA ELSE P.ID_PESSOA || ' \ ' || P.ID_PESSOA_PAI END PATH
from
  V_PESSOA P

Why the join??
0
 

Author Comment

by:schardosin
ID: 9857838
sorry,

in a view V_PESSOA for table PESSOA, the field ID_PESSOA_PAI is Daddy of the ID_PESSOA.
to bring the name(field NOME) of ID_PESSOA and ID_PESSOA_PAI, I make a bond between ID_PESSOA_PAI (V_PESSOA P) and ID_PESSOA (V_PESSOA PP) for bring in only one select the name of ID_PESSOA and your Daddy, ID_PESSOA_PAI.

translation.
PAI = DADDY
PESSOA = PEOPLE
NOME = NAME

Rafael Schardosin
Porto Alegre - Brazil
0
 
LVL 6

Accepted Solution

by:
BAlexandrov earned 50 total points
ID: 9866293
If you use firebird, the most correct is to use Coalesce - this is the Ansi standart function

(P.ID_PESSOA || ' \ ' || coalesce(P.ID_PESSOA_PAI,''))


Bojidar Alexandrov
0
 

Author Comment

by:schardosin
ID: 9866847
thank´s

your solution was of great help!!!


Rafael Schardosin
Porto Alegre - Brazil
0
 
LVL 2

Expert Comment

by:vowca
ID: 9910561
0
 
LVL 10

Expert Comment

by:kacor
ID: 10341113
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area for this question:
       to accept BAlexandrov's answer
Please leave any comments here within the next four (4) days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

If you will get a useful answer to your newer question next time, please accept it after evaluating. By this way the experts will get sooner his expert points for supporting you. If you have question about, please look at  http://www.experts-exchange.com/help.jsp.

kacor
EE Cleanup Volunteer
0

Featured Post

Do you have a plan for Continuity?

It's inevitable. People leave organizations creating a gap in your service. That's where Percona comes in.

See how Pepper.com relies on Percona to:
-Manage their database
-Guarantee data safety and protection
-Provide database expertise that is available for any situation

Question has a verified solution.

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

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

636 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