Solved

to concatenate with Null

Posted on 2003-12-01
10
760 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article describes some very basic things about SQL Server filegroups.
CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
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…

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now