Solved

to concatenate with Null

Posted on 2003-12-01
10
764 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
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
Industry Leaders: 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!

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
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.
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

685 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