Topic: String concatenation - as is better provide that one of items can to be NULL?
I want to add the elementary calculated column.
All calculation consists in blinding through a gap of value from several columns of the same table, and last in brackets.
ALTER TABLE test ADD user_title AS f4 + ' ' +f2 + ' ' +f7 + ' (' +comment + ') ';
With columns f * problems are not present, they NOT NULL so . it is not required. And here the column comment can be (and often enough and happens) equal NULL.
If in the last it is empty, it is necessary that it first of all did not spoil result, and " a gap and empty brackets " too it is not necessary to us at all.
The first thought which went at once in :
ALTER TABLE test ADD user_title AS f4 + ' ' +f2 + ' ' +f7+ISNULL (' (' +comment + ') ', ");
As though... The Line concatenated with NULL gives NULL, and ISNULL () in that case catches and substitutes for blank line. All ok?
But I here reflected - and how it will be for the server? Can eat other decision which or is simple faster or in something the friend it is better for request scheduling?
It is possible and here so:
ALTER TABLE test ADD user_title AS f4 + ' ' +f2 + ' ' +f7+COALESCE (' (' +comment + ') ', ");
And here so too it is possible:
ALTER TABLE test ADD user_title AS f4 + ' ' +f2 + ' ' +f7+CASE WHEN comment IS NULL THEN "ELSE ' (' +comment + ') ' END;
And figs knows how many still methods it is possible to concoct at desire...
Prompt, how it will be better and why?