Monday, March 12, 2012

How to Concatonate with Fixed Starting Positions

I want to concatonate the following into ColumnReference
ColumnA + ColumnB + ColumnC + ColumnD
Each of these columns A-D may be variable in length, (but would never
exceed the position in the string I have provided). I would like to store
the values in ColumnReference in the following manner.
ColumnA always begins at position1
ColumnB always begins at position 9
ColumnC always begins at position 13
ColumnD always begins at position 20
Thanks !Rob,
Essentially you need to pad with spaces (unless you wanted other
characters), truncate to length required and concatenate, as in
select ColumnReference =
left((ColumnA + replicate (' ', 8)), 8) +
left((ColumnB + replicate (' ', 4)), 4) +
left((ColumnC + replicate (' ', 7)), 7) +
left((ColumnD + replicate (' ', x)), x)
Replace x with appropriate value (you did not supply the ending position of
ColumnD). Note that the first parameter of the replicate() fn is a space
char (might go unnoticed). I am also assuming all four columns are of the
char family; if not convert as appropriate.
Hope this helps,
Raj
"Rob" <rwc1960@.bellsouth.net> wrote in message
news:yT3ke.18258$J25.12328@.bignews6.bellsouth.net...
>I want to concatonate the following into ColumnReference
> ColumnA + ColumnB + ColumnC + ColumnD
> Each of these columns A-D may be variable in length, (but would never
> exceed the position in the string I have provided). I would like to store
> the values in ColumnReference in the following manner.
> ColumnA always begins at position1
> ColumnB always begins at position 9
> ColumnC always begins at position 13
> ColumnD always begins at position 20
> Thanks !
>|||You can pad and concatenate strings in SQL, but a better question is
why are you destroying and doing formatting in the database?

No comments:

Post a Comment