Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 3 of 3 total
Thread How to create a computed expression
Tue, Dec 2 2008 4:39 PMPermanent Link

Dale Derix
I'm sure this is an embarrassingly simple question, but I'm stuck.

I want to create a computed field that is simply two other fields, for instance to create
a computed field called FullName that is made of of FirstName + LastName.

I'm trying to enter this into the "Computation" window in the ElevateDB Manager with no
success.

How to I do this?


PART II
Also, how do I make sure it still returns either the first name or the last name even of
one of them is NULL?


Thanks

Dale
Tue, Dec 2 2008 5:38 PMPermanent Link

Fernando Dias

Team Elevate Team Elevate

Dale,

> I want to create a computed field that is simply two other fields,

ALTER TABLE <YourTableName>
ADD COLUMN FullName VARCHAR(80) COMPUTED ALWAYS AS
TRIM(BOTH ' ' FROM COALESCE(FirstName,'')+' '+COALESCE(LastName,''))

> I'm trying to enter this into the "Computation" window in the ElevateDB Manager with no
> success.

Why? Do you get any error message?

> Also, how do I make sure it still returns either the first name or the last name even of
> one of them is NULL?

That's what COALESCE is for.
Note however that with this computation expression you will never get a
NULL value for the column. If you want it to be NULL when both FirstName
and LastName are NULL, then use the following expression instead:

IF(FirstName IS NULL AND LastName IS NULL,
  CAST(NULL AS VARCHAR),
  TRIM(BOTH ' ' FROM COALESCE(FirstName,'')+' '+COALESCE(LastName,''))
)


--
Fernando Dias
[Team Elevate]
Tue, Dec 2 2008 5:55 PMPermanent Link

Dale Derix
Awesome!  That did the trick!

Many thanks,

Dale
Image