SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Whats wrong with this concatenation?


Whats wrong with this concatenation?

Author
Message
Learner093
Learner093
Grasshopper
Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)

Group: General Forum Members
Points: 17 Visits: 49
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!
kl25
kl25
Say Hey Kid
Say Hey Kid (684 reputation)Say Hey Kid (684 reputation)Say Hey Kid (684 reputation)Say Hey Kid (684 reputation)Say Hey Kid (684 reputation)Say Hey Kid (684 reputation)Say Hey Kid (684 reputation)Say Hey Kid (684 reputation)

Group: General Forum Members
Points: 684 Visits: 1875
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?
Ray M
Ray M
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2129 Visits: 1076
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.
Chris Hurlbut
Chris Hurlbut
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1848 Visits: 540
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


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search