?
Solved

to concatenate with Null

Posted on 2003-12-01
10
Medium Priority
?
769 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
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 
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 200 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

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.

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 I was talking with Tim Sharp, one of my colleagues from our Technical Account Manager team about MongoDB’s scalability. While doing some quick training with some of the Percona team, Tim brought something to my attention...
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

719 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