How to avoid technically the null values while concatenating columns

  • HI,

    In SSIS by using dervied column i manipulate fields and end up in my desired results.

    But for an record if the particular column what i am using has NULL value then the particular record where i have given space got truncated and my file layout becomes a mess.

    So to avoid that i check whether a column is not null or not if its null means i would feed spaces.So my layout doesn't change.

    But this will be hectic if i am using multiple columns for a single ouptut column.

    Do we have some other way other to avoid NULL values?

  • Your message is a little confusing: are you importing or (I think) exporting?

    What do you mean when you say that your file layout "becomes a mess"? Can you provide some sample data highlighting the problem?

    Instead of 'passing spaces', what's wrong with an empty string?

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • If, as Phil has assumed/you've implied, you are exporting data from multiple source columns and concatenating them into a single export/destination column, you still need to account for the NULL fields in that concatenated string. Whether your final "export column" is in a fixed-width, or otherwise delimited format, you'd still want to pad those NULLed fields with some acceptable value (spaces, in a fixed-width file, for example). If you don't account for those NULL values/fields, then your export format/layout will definitely be "off."

    If this is not what you're after, please detail your situation further.

  • There may be an easier way. If the data source allows an SQL Query, and supports the ISNULL function, then your SELECT statement could look something like this:

    SELECT Field1, Field2,

    ISNULL(Field3, '') + ISNULL(Field4, '') AS CombinedFields3and4

    FROM dbo.SomeTable

    One other possiblity is if it also supports the NULLIF function:

    SELECT Field1, Field2,

    NULLIF(ISNULL(Field3, '') + ISNULL(Field4, ''), '') AS CombinedFields3and4

    FROM dbo.SomeTable

    ... which would only null the field if both sourc fields were null.

    Alternatively, there is a "Conditional" operator for SSIS that you can use in a Derived Column transformation, with a syntax something like this:

    ISNULL(Field3) ? ValueExpressionWhenTrue : ValueExpressionWhenFalse

    ... that can be used to detect a NULL value.

    Steve (aka sgmunson) 🙂 🙂 🙂
    Rent Servers for Income (picks and shovels strategy)

  • dg227 (4/24/2013)


    If, as Phil has assumed/you've implied, you are exporting data from multiple source columns and concatenating them into a single export/destination column, you still need to account for the NULL fields in that concatenated string. Whether your final "export column" is in a fixed-width, or otherwise delimited format, you'd still want to pad those NULLed fields with some acceptable value (spaces, in a fixed-width file, for example). If you don't account for those NULL values/fields, then your export format/layout will definitely be "off."

    If this is not what you're after, please detail your situation further.

    I am actually after this only.how it can be done ???

  • sgmunson (4/25/2013)


    There may be an easier way. If the data source allows an SQL Query, and supports the ISNULL function, then your SELECT statement could look something like this:

    SELECT Field1, Field2,

    ISNULL(Field3, '') + ISNULL(Field4, '') AS CombinedFields3and4

    FROM dbo.SomeTable

    One other possiblity is if it also supports the NULLIF function:

    SELECT Field1, Field2,

    NULLIF(ISNULL(Field3, '') + ISNULL(Field4, ''), '') AS CombinedFields3and4

    FROM dbo.SomeTable

    ... which would only null the field if both sourc fields were null.

    Alternatively, there is a "Conditional" operator for SSIS that you can use in a Derived Column transformation, with a syntax something like this:

    ISNULL(Field3) ? ValueExpressionWhenTrue : ValueExpressionWhenFalse

    ... that can be used to detect a NULL value.

    ya ya your are right..By using Conditional operator only i am able to achieve my goal.but it is easily possible if i am using 2 columns but if i am used to manipulate with 4-6 columns and each column are independent ones then its become difficult to achieve it.

  • The number of fields that can be included in the query's use of ISNULL and NULLIF is not limited to two. You can just keep concatenating an ISNULL function, and then NULLIF the entire concatenation, and thus have as many fields as you need. They just all need to be strings, or alternately, CAST-ed or CONVERT-ed to strings.

    Steve (aka sgmunson) 🙂 🙂 🙂
    Rent Servers for Income (picks and shovels strategy)

  • sgmunson (5/6/2013)


    The number of fields that can be included in the query's use of ISNULL and NULLIF is not limited to two. You can just keep concatenating an ISNULL function, and then NULLIF the entire concatenation, and thus have as many fields as you need. They just all need to be strings, or alternately, CAST-ed or CONVERT-ed to strings.

    Steve - you sound like you understand the problem - can you explain it, because I still cannot comprehend properly what the issue is? 🙂

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • I'm at the point of making an assumption about what the poster meant by their most recent post. Given the technique of concatenating a series of ISNULL functions whose parameters are all strings or casted or converted into strings, it's not that difficult to eliminate a null value from causing a problem in concatenation, and you just encapsulate the entire concatenation of ISNULLs within a NULLIF and you can preserve a NULL value for the case when all the fields are NULL as opposed to just one or more but not all. The assumption I make is that when they mention the idea of having 4 to 6 fields to deal with, that they mean that they need to concatenate more than just two fields. Whether that assumption holds true or not is something the poster would have to resolve for us by posting again. Does that explain?

    Phil Parkin (5/6/2013)


    sgmunson (5/6/2013)


    The number of fields that can be included in the query's use of ISNULL and NULLIF is not limited to two. You can just keep concatenating an ISNULL function, and then NULLIF the entire concatenation, and thus have as many fields as you need. They just all need to be strings, or alternately, CAST-ed or CONVERT-ed to strings.

    Steve - you sound like you understand the problem - can you explain it, because I still cannot comprehend properly what the issue is? 🙂

    Steve (aka sgmunson) 🙂 🙂 🙂
    Rent Servers for Income (picks and shovels strategy)

  • OK, I see what you are getting at, thanks. I still cannot see why that causes a formatting problem when outputting though.

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • The problem usually shows up when you are trying to export to a text file, and the presence of any one NULL value throws the entire output record into NULL status, and depending on exactly how poorly the process was designed, it could be a problem such as missing records in the output file, or data showing up in the wrong location within the output data record because they account for everything except the empty string. The original poster has yet to come back with feedback beyond the apparent need to deal with 4 to 6 independent fields. Why that makes a difference is thus still unknown.

    Hey manibad - do you have a solution now, or do you need more assistance? Let us know...

    Phil Parkin (5/6/2013)


    OK, I see what you are getting at, thanks. I still cannot see why that causes a formatting problem when outputting though.

    Steve (aka sgmunson) 🙂 🙂 🙂
    Rent Servers for Income (picks and shovels strategy)

  • sgmunson (5/6/2013)


    I'm at the point of making an assumption about what the poster meant by their most recent post. Given the technique of concatenating a series of ISNULL functions whose parameters are all strings or casted or converted into strings, it's not that difficult to eliminate a null value from causing a problem in concatenation, and you just encapsulate the entire concatenation of ISNULLs within a NULLIF and you can preserve a NULL value for the case when all the fields are NULL as opposed to just one or more but not all. The assumption I make is that when they mention the idea of having 4 to 6 fields to deal with, that they mean that they need to concatenate more than just two fields. Whether that assumption holds true or not is something the poster would have to resolve for us by posting again. Does that explain?

    Phil Parkin (5/6/2013)


    sgmunson (5/6/2013)


    The number of fields that can be included in the query's use of ISNULL and NULLIF is not limited to two. You can just keep concatenating an ISNULL function, and then NULLIF the entire concatenation, and thus have as many fields as you need. They just all need to be strings, or alternately, CAST-ed or CONVERT-ed to strings.

    Steve - you sound like you understand the problem - can you explain it, because I still cannot comprehend properly what the issue is? 🙂

    "The assumption I make is that when they mention the idea of having 4 to 6 fields to deal with, that they mean that they need to concatenate more than just two fields. "the assumption what you has is perfect.i have the same situation to be dealt.Moreover i am rookie to SSIS and so i just came to know about the NULLIF function..and i will work on it...to fix my issues.

  • manibad (5/6/2013)


    sgmunson (5/6/2013)


    I'm at the point of making an assumption about what the poster meant by their most recent post. Given the technique of concatenating a series of ISNULL functions whose parameters are all strings or casted or converted into strings, it's not that difficult to eliminate a null value from causing a problem in concatenation, and you just encapsulate the entire concatenation of ISNULLs within a NULLIF and you can preserve a NULL value for the case when all the fields are NULL as opposed to just one or more but not all. The assumption I make is that when they mention the idea of having 4 to 6 fields to deal with, that they mean that they need to concatenate more than just two fields. Whether that assumption holds true or not is something the poster would have to resolve for us by posting again. Does that explain?

    Phil Parkin (5/6/2013)


    sgmunson (5/6/2013)


    The number of fields that can be included in the query's use of ISNULL and NULLIF is not limited to two. You can just keep concatenating an ISNULL function, and then NULLIF the entire concatenation, and thus have as many fields as you need. They just all need to be strings, or alternately, CAST-ed or CONVERT-ed to strings.

    Steve - you sound like you understand the problem - can you explain it, because I still cannot comprehend properly what the issue is? 🙂

    "The assumption I make is that when they mention the idea of having 4 to 6 fields to deal with, that they mean that they need to concatenate more than just two fields. "the assumption what you has is perfect.i have the same situation to be dealt.Moreover i am rookie to SSIS and so i just came to know about the NULLIF function..and i will work on it...to fix my issues.

    I now know why I was confused - this problem has nothing to do with SSIS, it's merely a source/selection problem.

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

Viewing 13 posts - 1 through 12 (of 12 total)

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