Logo

SQL Server MAPPEND Function

Updated 2023-10-19 13:43:46.793000

Description

Use the scalar function MAPPEND to add columns or rows to an existing matrix variable. A matrix variable is a string representation of a matrix with columns separated by commas and rows separated by semi-colons. A new string representation is returned.

Syntax

SELECT [westclintech].[wct].[MAPPEND](
  <@A, nvarchar(max),>
 ,<@start_row_A, int,>
 ,<@end_row_A, int,>
 ,<@start_col_A, int,>
 ,<@end_col_A, int,>
 ,<@Connector, nvarchar(4000),>
 ,<@B, nvarchar(max),>
 ,<@start_row_B, int,>
 ,<@end_row_B, int,>
 ,<@start_col_B, int,>
 ,<@end_col_B, int,>)

Arguments

@A

A string representation of the A matrix.

@start_row_A

The first row of @A to be included in the result. @start_row_A must be of a type int or of a type that implicitly converts to int.

@end_row_A

The last row of @A to be included in the result. @end_row_A must be of a type int or of a type that implicitly converts to int.

@start_col_A

The first column of @A to be included in the result. @start_col_A must be of a type int or of a type that implicitly converts to int.

@end_col_A

The last column of @A to be included in the result. @end_col_A must be of a type int or of a type that implicitly converts to int.

@Connector

Identifies the method of connection. Use ';' to append the rows of @B to the rows of @A. Use ',' to append the columns of @B to the columns of @A.

@B

A string representation of the B matrix.

@start_row_B

The first row of @B to be included in the result. @start_row_B must be of a type int or of a type that implicitly converts to int.

@end_row_B

The last row of @B to be included in the result. @end_row_B must be of a type int or of a type that implicitly converts to int.

@start_col_B

The first column of @B to be included in the result. @start_col_B must be of a type int or of a type that implicitly converts to int.

@end_col_B

The last column of @B to be included in the result. @end_col_B must be of a type int or of a type that implicitly converts to int.

Return Type

nvarchar(max)

Remarks

The string representations of @A and/or @B must only contain numbers, commas (to separate the columns), and semi-colons to separate the rows.

Consecutive commas will generate an error.

Consecutive semi-colons will generate an error.

Non-numeric data between commas will generate an error.

Non-numeric data between semi-colons will generate an error.

To convert non-normalized data to a string format, use the MATRIX2STRING or the MATRIX2STRING_q function.

To convert normalized data to a string format, us the NMATRIX2STRING or the NMATRIX2STRING_q function.

To convert the string result to a table, us the table-valued function MATRIX.

If @B is NULL then the returned matrix is @A or a subset of @A.

If @A is NULL then the returned matrix is @B or a subset of @B.

If @start_row_A is NULL then @start_row_A equals 1.

If @start_col_A is NULL then @start_col_A equals 1.

If @start_row_B is NULL then @start_row_B equals 1.

If @start_col_B is NULL then @start_col_B equals 1.

If @end_row_A is NULL then @end_row_A equals the last row in in @A.

If @end_col_A is NULL then @end_col_A equals the last row in in @A.

If @end_row_B is NULL then @end_row_B equals the last row in in @B.

If @end_col_B is NULL then @end_col_B equals the last row in in @B.

Examples

Example #1

In this example we append all the columns of B to all the columns of A.

--A,B
DECLARE @A as varchar(max) = '1,1,1,1;1,2,4,8;1,3,9,27;1,4,16,64;1,5,25,125';
DECLARE @B as varchar(max) = '1,0,0,0,0;0,1,0,0,0;0,0,1,0,0;0,0,0,1,0;0,0,0,0,1';
DECLARE @C as varchar(max) = wct.MAPPEND(@A,NULL,NULL,NULL,NULL,',',@B,NULL,NULL,NULL,NULL);
SELECT @C as [A,B];
 
--Only run this SQL to automatically PIVOT the results into the
--traditional row/column matrix presentation
--DECLARE @M as nvarchar(max) = N'SELECT @cols FROM wct.Matrix(@C) d PIVOT(MAX(ItemValue) FOR ColNum in (@cols))p ORDER BY RowNum';
--DECLARE @cols as nvarchar(max);
 
--SET @cols = (SELECT '[' + cast(ColNum as varchar(max)) + ']' FROM wct.MATRIX(@C) WHERE RowNum = 0 ORDER BY colnum FOR XML PATH(''));
--SET @cols = REPLACE(@cols,'][','],[');
--SET @M = REPLACE(REPLACE(@M,'@cols',@cols),'@C','''' + @C + '''');
--EXECUTE(@M);

This produces the following result.

A,B
1,1,1,1,1,0,0,0,0;1,2,4,8,0,1,0,0,0;1,3,9,27,0,0,1,0,0;1,4,16,64,0,0,0,1,0;1,5,25,125,0,0,0,0,1

Here are the results formatted as a matrix.

012345678
111110000
124801000
1392700100
14166400010
152512500001

Example #2

In this example we append all the rows of B to all the rows of A.

--A;B
DECLARE @A as varchar(max) = '1,1,1,1,1;1,2,4,8,16;1,3,9,27,81;1,4,16,64,256;1,5,25,125,625';
DECLARE @B as varchar(max) = '1,0,0,0,0;0,1,0,0,0;0,0,1,0,0;0,0,0,1,0;0,0,0,0,1';
DECLARE @C as varchar(max) = wct.MAPPEND(@A,NULL,NULL,NULL,NULL,';',@B,NULL,NULL,NULL,NULL);
SELECT @C as [A;B];
 
--Only run this SQL to automatically PIVOT the results into the
--traditional row/column matrix presentation
--DECLARE @M as nvarchar(max) = N'SELECT @cols FROM wct.Matrix(@C) d PIVOT(MAX(ItemValue) FOR ColNum in (@cols))p ORDER BY RowNum';
--DECLARE @cols as nvarchar(max);
 
--SET @cols = (SELECT '[' + cast(ColNum as varchar(max)) + ']' FROM wct.MATRIX(@C) WHERE RowNum = 0 ORDER BY colnum FOR XML PATH(''));
--SET @cols = REPLACE(@cols,'][','],[');
--SET @M = REPLACE(REPLACE(@M,'@cols',@cols),'@C','''' + @C + '''');
--EXECUTE(@M);

This produces the following result.

A;B
1,1,1,1,1;1,2,4,8,16;1,3,9,27,81;1,4,16,64,256;1,5,25,125,625;1,0,0,0,0;0,1,0,0,0;0,0,1,0,0;0,0,0,1,0;0,0,0,0,1

Here are the results formatted as a matrix.

01234
11111
124816
1392781
141664256
1525125625
10000
01000
00100
00010
00001

Example #3

In this example we return a sub-matrix of A.

--A(i:m,j:n)
DECLARE @A as varchar(max) = '1,1,1,1,1;1,2,4,8,16;1,3,9,27,81;1,4,16,64,256;1,5,25,125,625';
DECLARE @C as varchar(max) = wct.MAPPEND(@A,3,NULL,3,NULL,'',NULL,NULL,NULL,NULL,NULL);
SELECT @C as [A(i:m,j:n)];
 
--Only run this SQL to automatically PIVOT the results into the
--traditional row/column matrix presentation
--DECLARE @M as nvarchar(max) = N'SELECT @cols FROM wct.Matrix(@C) d PIVOT(MAX(ItemValue) FOR ColNum in (@cols))p ORDER BY RowNum';
--DECLARE @cols as nvarchar(max);
 
--SET @cols = (SELECT '[' + cast(ColNum as varchar(max)) + ']' FROM wct.MATRIX(@C) WHERE RowNum = 0 ORDER BY colnum FOR XML PATH(''));
--SET @cols = REPLACE(@cols,'][','],[');
--SET @M = REPLACE(REPLACE(@M,'@cols',@cols),'@C','''' + @C + '''');
--EXECUTE(@M);

This produces the following result.

A(i:m,j:n)
9,27,81;16,64,256;25,125,625

Here are the results formatted as a matrix.

012
92781
1664256
25125625

Example #4

In this example we return a sub-matrix of B.

--B(i:m,j:n)
DECLARE @B as varchar(max) = '1,0,0,0,0;0,1,0,0,0;0,0,1,0,0;0,0,0,1,0;0,0,0,0,1';
DECLARE @C as varchar(max) = wct.MAPPEND(NULL,NULL,NULL,NULL,NULL,'',@B,3,NULL,3,NULL);
SELECT @C as [B(i:m,j:n)];
 
--Only run this SQL to automatically PIVOT the results into the
--traditional row/column matrix presentation
--DECLARE @M as nvarchar(max) = N'SELECT @cols FROM wct.Matrix(@C) d PIVOT(MAX(ItemValue) FOR ColNum in (@cols))p ORDER BY RowNum';
--DECLARE @cols as nvarchar(max);
 
--SET @cols = (SELECT '[' + cast(ColNum as varchar(max)) + ']' FROM wct.MATRIX(@C) WHERE RowNum = 0 ORDER BY colnum FOR XML PATH(''));
--SET @cols = REPLACE(@cols,'][','],[');
--SET @M = REPLACE(REPLACE(@M,'@cols',@cols),'@C','''' + @C + '''');
--EXECUTE(@M);

This produces the following result.

B(i:m,j:n)
1,0,0;0,1,0;0,0,1

Here are the results formatted as a matrix.

012
100
010
001

Example #5

In this example we append all of the rows but just some of the columns of A with all of the rows and just some of the columns of B. A & B must have the same number of rows.

--A(:,j:n),B(:,j:n)
DECLARE @A as varchar(max) = '1,1,1,1,1;1,2,4,8,16;1,3,9,27,81;1,4,16,64,256;1,5,25,125,625';
DECLARE @B as varchar(max) = '1,0,0,0,0;0,1,0,0,0;0,0,1,0,0;0,0,0,1,0;0,0,0,0,1';
DECLARE @C as varchar(max) = wct.MAPPEND(@A,NULL,NULL,3,NULL,';',@B,NULL,NULL,3,NULL);
SELECT @C as [A(:,j:n),B(:,j:n)];
 
--Only run this SQL to automatically PIVOT the results into the
--traditional row/column matrix presentation
--DECLARE @M as nvarchar(max) = N'SELECT @cols FROM wct.Matrix(@C) d PIVOT(MAX(ItemValue) FOR ColNum in (@cols))p ORDER BY RowNum'
--DECLARE @cols as nvarchar(max)
 
--SET @cols = (SELECT '[' + cast(ColNum as varchar(max)) + ']' FROM wct.MATRIX(@C) WHERE RowNum = 0 ORDER BY colnum FOR XML PATH(''))
--SET @cols = REPLACE(@cols,'][','],[')
--SET @M = REPLACE(REPLACE(@M,'@cols',@cols),'@C','''' + @C + '''')
--EXECUTE(@M)

This produces the following result.

A(:,j:n),B(:,j:n)
1,1,1;4,8,16;9,27,81;16,64,256;25,125,625;0,0,0;0,0,0;1,0,0;0,1,0;0,0,1

Here are the results formatted as a matrix.

012
111
4816
92781
1664256
25125625
000
000
100
010
001

Example #6

In this example we append all of the columns but just some of the rows of A with all of the columns and just some of the rows of B. A & B must have the same number of columns.

--A(i:m,:);B(i:m,:)
DECLARE @A as varchar(max) = '1,1,1,1,1;1,2,4,8,16;1,3,9,27,81;1,4,16,64,256;1,5,25,125,625';
DECLARE @B as varchar(max) = '1,0,0,0,0;0,1,0,0,0;0,0,1,0,0;0,0,0,1,0;0,0,0,0,1';
DECLARE @C as varchar(max) = wct.MAPPEND(@A,3,NULL,NULL,NULL,';',@B,3,NULL,NULL,NULL);
SELECT @C as [A(i:m,:);B(i:m,:)];
 
--Only run this SQL to automatically PIVOT the results into the
--traditional row/column matrix presentation
--DECLARE @M as nvarchar(max) = N'SELECT @cols FROM wct.Matrix(@C) d PIVOT(MAX(ItemValue) FOR ColNum in (@cols))p ORDER BY RowNum';
--DECLARE @cols as nvarchar(max);
 
--SET @cols = (SELECT '[' + cast(ColNum as varchar(max)) + ']' FROM wct.MATRIX(@C) WHERE RowNum = 0 ORDER BY colnum FOR XML PATH(''));
--SET @cols = REPLACE(@cols,'][','],[');
--SET @M = REPLACE(REPLACE(@M,'@cols',@cols),'@C','''' + @C + '''');
--EXECUTE(@M);

This produces the following result.

A(i:m,:);B(i:m,:)
1,3,9,27,81;1,4,16,64,256;1,5,25,125,625;0,0,1,0,0;0,0,0,1,0;0,0,0,0,1

Here are the results formatted as a matrix.

01234
1392781
141664256
1525125625
00100
00010
00001

Example #7

In this example the selected rows and columns of B are append to the right of the selected rows and columns of A. The sub-matrices of A and B must have the same number of rows.

--A(i:m,j:n),B(k:p,l:q)
DECLARE @A as varchar(max) = '1,1,1,1,1;1,2,4,8,16;1,3,9,27,81;1,4,16,64,256;1,5,25,125,625';
DECLARE @B as varchar(max) = '1,0,0,0,0;0,1,0,0,0;0,0,1,0,0;0,0,0,1,0;0,0,0,0,1';
DECLARE @C as varchar(max) = wct.MAPPEND(@A,2,5,1,4,',',@B,1,4,4,4);
SELECT @C as [A(i:m,j:n),B(k:p,l:q)];
 
--Only run this SQL to automatically PIVOT the results into the
--traditional row/column matrix presentation
--DECLARE @M as nvarchar(max) = N'SELECT @cols FROM wct.Matrix(@C) d PIVOT(MAX(ItemValue) FOR ColNum in (@cols))p ORDER BY RowNum';
--DECLARE @cols as nvarchar(max);
 
--SET @cols = (SELECT '[' + cast(ColNum as varchar(max)) + ']' FROM wct.MATRIX(@C) WHERE RowNum = 0 ORDER BY colnum FOR XML PATH(''));
--SET @cols = REPLACE(@cols,'][','],[');
--SET @M = REPLACE(REPLACE(@M,'@cols',@cols),'@C','''' + @C + '''');
--EXECUTE(@M);

This produces the following result.

A(i:m,j:n),B(k:p,l:q)
1,2,4,8,0;1,3,9,27,0;1,4,16,64,0;1,5,25,125,1

Here are the results formatted as a table.

01234
12480
139270
1416640
15251251

Example #8

In this example the selected rows and columns of B are appended to the bottom of the selected rows and columns of A. The sub-matrices of A and B must have the same number of columns.

--A(i:m,j:n);B(k:p,l:q)
DECLARE @A as varchar(max) = '1,1,1,1,1;1,2,4,8,16;1,3,9,27,81;1,4,16,64,256;1,5,25,125,625';
DECLARE @B as varchar(max) = '1,0,0,0,0;0,1,0,0,0;0,0,1,0,0;0,0,0,1,0;0,0,0,0,1';
DECLARE @C as varchar(max) = wct.MAPPEND(@A,3,5,1,4,';',@B,1,1,1,4);
SELECT @C as [A(i:m,j:n);B(k:p,l:q)];
 
--Only run this SQL to automatically PIVOT the results into the
--traditional row/column matrix presentation
--DECLARE @M as nvarchar(max) = N'SELECT @cols FROM wct.Matrix(@C) d PIVOT(MAX(ItemValue) FOR ColNum in (@cols))p ORDER BY RowNum';
--DECLARE @cols as nvarchar(max);
 
--SET @cols = (SELECT '[' + cast(ColNum as varchar(max)) + ']' FROM wct.MATRIX(@C) WHERE RowNum = 0 ORDER BY colnum FOR XML PATH(''));
--SET @cols = REPLACE(@cols,'][','],[');
--SET @M = REPLACE(REPLACE(@M,'@cols',@cols),'@C','''' + @C + '''');
--EXECUTE(@M);

This produces the following result.

A(i:m,j:n);B(k:p,l:q)
1,3,9,27;1,4,16,64;1,5,25,125;1,0,0,0

Here are the results formatted as a table.

0123
13927
141664
1525125
1000

See Also

MUPDATE - perform elementwise operations on a matrix