Solved

to concatenate with Null

Posted on 2003-12-01
10
765 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Azure Functions is a solution for easily running small pieces of code, or "functions," in the cloud. This article shows how to create one of these functions to write directly to Azure Table Storage.
Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
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…

740 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