Views with NULL values

  • Hi,

    I'm trying to create a view which will select a row from a table (that will always have only 1 record). Problem is that some fields will be NULL. The table is updated everyday and the NULL values can switch column.

    I want to select all the values as 1 field, so concatenate plus add a newline after every value (with CHAR(13) + CHAR(10)). When I have a NULL value it also adds the newlines giving me empty lines which is not good.

    I've tried with ISNULL(column, '') but an empty value is also not working properly.

    Any1 could help me with this ?

    Thanks in advance.

  • Try COALESCE(column, '')

    otherwise can you post the CREATE VIEW statement

  • Thanks for the reply. But works the same as ISNULL(), my newlines are still shown. My query in view is like this:

    SELECT COALESCE (dZ_Field1, '') + CHAR(13) + CHAR(10) + COALESCE (dZ_Field2_A, '') + CHAR(13) + CHAR(10)

    + COALESCE (dZ_Field3_A, '') + CHAR(13) + CHAR(10) + COALESCE (dZ_Field3_A, '') + CHAR(13) + CHAR(10)

    + COALESCE (dZ_Field4_A, '') + CHAR(13) + CHAR(10) + COALESCE (dZ_Field5_A, '') AS ConcatString

    FROM dbo.dZ_Data

    So when 1 field is NULL it still adds the newlines, I should find a way to only concat the newlines if the field is not null. If the field is null it should just skip and not concat the newlines...

  • and what does the data look like? sample?

  • Would this work?

    COALESCE(MyCol + CHAR(13),'') + COALESCE(MyNextCol + CHAR(13,'') + ....

    John

  • SELECT

    COALESCE (dZ_Field1 + CHAR(13) + CHAR(10), '') +

    COALESCE (dZ_Field2_A + CHAR(13) + CHAR(10), '') +

    COALESCE (dZ_Field3_A + CHAR(13) + CHAR(10), '') +

    COALESCE (dZ_Field3_A + CHAR(13) + CHAR(10), '') +

    COALESCE (dZ_Field4_A + CHAR(13) + CHAR(10), '') +

    COALESCE (dZ_Field5_A, '') AS ConcatString

    FROM dbo.dZ_Data

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • This worked like a charm! Thanks alot.

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply