Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

How to avoid technically the null values while concatenating columns Expand / Collapse
Author
Message
Posted Monday, April 22, 2013 12:44 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Thursday, July 17, 2014 12:46 PM
Points: 91, Visits: 143
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?
Post #1445113
Posted Tuesday, April 23, 2013 1:13 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:31 AM
Points: 5,162, Visits: 12,010
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?



Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

When you ask a question (and please do ask a question: "My T-SQL does not work" just doesn't cut it), please provide enough information for us to understand its context.
Post #1445294
Posted Wednesday, April 24, 2013 2:59 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, September 29, 2014 10:25 PM
Points: 254, Visits: 755
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.
Post #1446238
Posted Thursday, April 25, 2013 8:21 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Friday, October 17, 2014 6:53 AM
Points: 1,669, Visits: 2,219
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
[code]

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

[code]
SELECT Field1, Field2,
NULLIF(ISNULL(Field3, '') + ISNULL(Field4, ''), '') AS CombinedFields3and4
FROM dbo.SomeTable
[code]

... 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:

[code]
ISNULL(Field3) ? ValueExpressionWhenTrue : ValueExpressionWhenFalse

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


Steve
(aka sgmunson)

Internet ATM Machine
Post #1446534
Posted Monday, May 6, 2013 2:09 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Thursday, July 17, 2014 12:46 PM
Points: 91, Visits: 143
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 ???
Post #1449621
Posted Monday, May 6, 2013 2:12 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Thursday, July 17, 2014 12:46 PM
Points: 91, Visits: 143
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
[code]

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

[code]
SELECT Field1, Field2,
NULLIF(ISNULL(Field3, '') + ISNULL(Field4, ''), '') AS CombinedFields3and4
FROM dbo.SomeTable
[code]

... 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:

[code]
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.
Post #1449622
Posted Monday, May 6, 2013 7:16 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Friday, October 17, 2014 6:53 AM
Points: 1,669, Visits: 2,219
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)

Internet ATM Machine
Post #1449705
Posted Monday, May 6, 2013 7:20 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:31 AM
Points: 5,162, Visits: 12,010
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?



Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

When you ask a question (and please do ask a question: "My T-SQL does not work" just doesn't cut it), please provide enough information for us to understand its context.
Post #1449707
Posted Monday, May 6, 2013 9:04 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Friday, October 17, 2014 6:53 AM
Points: 1,669, Visits: 2,219
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)

Internet ATM Machine
Post #1449747
Posted Monday, May 6, 2013 9:23 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:31 AM
Points: 5,162, Visits: 12,010
OK, I see what you are getting at, thanks. I still cannot see why that causes a formatting problem when outputting though.


Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

When you ask a question (and please do ask a question: "My T-SQL does not work" just doesn't cut it), please provide enough information for us to understand its context.
Post #1449755
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse