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

Whats wrong with this concatenation? Expand / Collapse
Author
Message
Posted Wednesday, October 17, 2012 8:09 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, December 30, 2013 7:28 AM
Points: 9, Visits: 48
I have this SSIS expression and it is evaluated as below with the default values replacing the SSIS variables. This is the raw expression:

"Select "+(DT_STR, 20, 1252)@[User::BatchID]+ " as BatchID, '" +@[User::BatchDateISO]+ "' as BatchDateISO, 'FH' as RecordType, 'AS400' as SourceSystem, CASE WHEN LEN("+(DT_STR, 10, 1252)@[User::FileLoadNumber]+") = CAST(1 as VARCHAR) THEN '0'+'"+ (DT_STR, 10, 1252) @[User::FileLoadNumber]+"'+'"+@[User::BatchDateISO]+"' ELSE '"+ (DT_STR, 10, 1252) @[User::FileLoadNumber]+"'+'"+@[User::BatchDateISO]+"' END as FileLoadNumber"

It is evaluated as:

Select 9 as BatchID, '20121017' as BatchDateISO, 'FH' as RecordType, 'AS400' as SourceSystem, CASE WHEN LEN(9) = CAST(1 as VARCHAR) THEN '0'+'9'+'20121017' ELSE '9'+'20121017' END as FileLoadNumber

This is returning the result without appending 0, which means always the ELSE part in CASE is chosen for FileLoadNumber column value.

If the BatchID value is of single digit it has to concatenate with BatchDateISO by appending 0 in front of BatchID

If BatchID is 9, then the result should be 0920121017 for FileLoadNumber column
If it is 19, then the result should be 1920121017 for FileLoadNumber column

Thanks a lot!
Post #1374114
Posted Thursday, October 18, 2012 12:21 PM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Today @ 8:25 AM
Points: 461, Visits: 1,686
Hi Learner,

A suggestion and a question. The suggestion. It's very helpful if you do not cross-post your questions. That keeps the responses in one place. Folks will find the question wherever you put it.

The question. I'm confused about what the issue is. Is the expression not giving the expected output or is the output not giving the expected query results?
Post #1374536
Posted Thursday, October 18, 2012 1:14 PM
UDP Broadcaster

UDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP Broadcaster

Group: General Forum Members
Last Login: Monday, April 14, 2014 1:52 PM
Points: 1,478, Visits: 1,020
Like this?

"Select "+(DT_STR, 20, 1252)@[User::BatchID] + " as BatchID, '" +@[User::BatchDateISO] + "' as BatchDateISO, 'FH' as RecordType, 'AS400' as SourceSystem, " +RIGHT("00" + (DT_STR, 10, 1252)@[User::BatchID],2) + @[User::BatchDateISO] + " as FileLoadNumber"
Your expression references FileLoadNumber variable, which you did not say what its value is or where it fits in, But why are you doing that crazy case?

Simple RIGHT("00" + (DT_STR, 10, 1252)@[User::BatchID],2) will Create a string 009, and then take the Right 2 characters, ie 09. if the batch id is 19 then it will create a string 0019 and then take the Right 2 chars getting 19.
Post #1374559
Posted Friday, October 19, 2012 12:37 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, April 09, 2014 2:46 PM
Points: 1,337, Visits: 449
I would dump the expression and use Execute SQL Task as it is easier to read and maintain.

Here is what I would do, you might need to tweak as I took 10 minutes just to put this together:
/* comment out declares and set in sql task
set-up ado.net connection
pass in variables that are declared here
Set-up result set to single row
For result set, set result name to 0, use user variable FileLoadNumber as variable name

DECLARE @BatchID INT
DECLARE @FileLoadNumber int
DECLARE @BatchDateISO datetime

SET @BatchID = 11
SET @BatchDateISO = GETDATE()
SET @FileLoadNumber = '9'
*/

Select CAST(@BatchID AS VARCHAR(20))
+ 'FHAS400'
+ CASE WHEN LEN(@FileLoadNumber) = 0 then '0' + convert(char(8),@BatchDateISO,112)
ELSE CAST(@FileLoadNumber AS VARCHAR(10)) + convert(char(8),@BatchDateISO,112) end

Post #1374992
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse