SQL Server 2017 - Extracting source and target column names within a string containing column mappingsSQL...

Why Third 'Reich'? Why is 'reich' not translated when 'third' is? What is the English synonym of reich?

In a world with multiracial creatures, what word can be used instead of mankind?

How do I handle a blinded enemy which wants to attack someone it's sure is there?

Ramanujan's radical and how we define an infinite nested radical

What does @ mean in a hostname in DNS configuration?

Rudeness by being polite

Cryptic cross... with words

Why write a book when there's a movie in my head?

Can you wish for more wishes from an Efreeti bound to service via an Efreeti Bottle?

Multiple null checks in Java 8

How can I make my enemies feel real and make combat more engaging?

How do I know my password or backup information is not being shared when creating a new wallet?

I hate taking lectures, can I still survive in academia?

Empty optional argument or Not giving optional argument at all?

Are encryption algorithms with fixed-point free permutations inherently flawed?

Coworker asking me to not bring cakes due to self control issue. What should I do?

How can I create unencrypted addresses?

How does the income of your target audience matter for logo design?

How do I add a strong "onion flavor" to the biryani (in restaurant style)?

Coworker is trying to get me to sign his petition to run for office. How to decline politely?

How many copper coins fit inside a cubic foot?

Does changing "sa" password require a SQL restart (in mixed mode)?

How bad is a Computer Science course that doesn't teach Design Patterns?

Do error bars on probabilities have any meaning?



SQL Server 2017 - Extracting source and target column names within a string containing column mappings


SQL Server - NTEXT columns and string manipulationnested if expressions for string in excel source file in derived column expressionFind rows with similar string valuesUse STUFF function to insert at multiple points in a string?SQL Server 2017 Enterprise vs Standard Edition?gMSAs and SQL Server 2017nText and String manipulation. SQL SERVERReinstalling SQL Server 2017Elastic queries available in SQL Server 2017Cannot Start SQL Server 2017?













0















I have the following format string stored in text (could be any number of columns):



col1_source|col1_target;col2_source|col2_target;col3_source|col3_target;...


I'm trying to come up with an elegant way of extracting and isolating all the xxx_source column names and all the xxx_target column names so I could store them in variables and get the following end result:



@Source_Columns = 'col1_source,col2_source,col3_source'
@Target_Columns = 'col1_target,col2_target,col3_target'



At the end of the day, I'd like to perform SELECTs on my source and target columns to perform data compares.



This is what I've achieved so far, but I find it's just too complex for nothing (with a table valued function):



CREATE FUNCTION [dbo].[UF_miscParseStringToTable]
(
@list nvarchar(MAX)
, @sep varchar(8)
)
RETURNS @ts table
(
[ID] int identity
, [value] nvarchar(MAX)
)
AS
BEGIN

-- Parameters check
if ((@sep is null) or (datalength(@sep) < 1)) return

if ((@list is null) or (@list = '') or (@list = @sep)) return

-- Add path wildcards directly with sep
-- ?worth it?
if (left(@sep, 1) <> '%') set @sep = '%' + @sep
if (right(@sep, 1) <> '%') set @sep = @sep + '%'

-- First first sep
declare @i int
set @i = patindex(@sep, @list)

-- Acc values
while (@i > 0) begin
insert into @ts ([value]) values (rtrim(left(@list, @i - 1)))
set @list = ltrim(right(RTRIM(@list), len(@list) + 3 - (@i + len(@sep) )))
set @i = patindex(@sep, @list)
end

set @list = rtrim(@list)
-- Insert last value, if any
if (@list <> '') insert into @ts (value) values (@list)

return

END


The function above basically takes my mapping string and converts it to a list of column names in a table (see query logic below):



DECLARE @Delim varchar(1) = '|'
DECLARE @Mapping varchar(max) = 'col1_source|col1_target;col2_source|col2_target;col3_source|col3_target'
DECLARE @String varchar(max) = REPLACE(@Mapping,';', @Delim)
SELECT * FROM dbo.UF_miscParseStringToTable(@String, @Delim)


The above resulting query yields the following table:



 ID| value
1 | col1_source
2 | col1_target
3 | col2_source
4 | col2_target
5 | col3_source
6 | col3_target


I could perhaps do a join on the column indexes but, I'm finding it difficult to isolate my source and target fields so that I could perform data comparisons between them. In addition, I'd like to avoid performing an extra join to a table if I don't have to.



Here are the results desired (to be able to perform the following):



SELECT col1_source, col2_source, col3_source FROM mytable;

SELECT col1_target, col2_target, col3_target FROM mytable;


Any help or ideas would be great!



Shawn










share|improve this question

























  • Your question told us what the source string was and also what your current query is returning. What results do you want returned? Also, please specify your version of SQL Server (2012, 2016, etc.)

    – Scott Hodgin
    3 hours ago











  • My apologies. It's SQL Server 2017.

    – shawnyshawny
    1 hour ago











  • Here are the results desired for the source columns: col1_source,col2_source,col3_source ; here are the results desired for the targets: col1_target,col2_target,col3_target

    – shawnyshawny
    1 hour ago


















0















I have the following format string stored in text (could be any number of columns):



col1_source|col1_target;col2_source|col2_target;col3_source|col3_target;...


I'm trying to come up with an elegant way of extracting and isolating all the xxx_source column names and all the xxx_target column names so I could store them in variables and get the following end result:



@Source_Columns = 'col1_source,col2_source,col3_source'
@Target_Columns = 'col1_target,col2_target,col3_target'



At the end of the day, I'd like to perform SELECTs on my source and target columns to perform data compares.



This is what I've achieved so far, but I find it's just too complex for nothing (with a table valued function):



CREATE FUNCTION [dbo].[UF_miscParseStringToTable]
(
@list nvarchar(MAX)
, @sep varchar(8)
)
RETURNS @ts table
(
[ID] int identity
, [value] nvarchar(MAX)
)
AS
BEGIN

-- Parameters check
if ((@sep is null) or (datalength(@sep) < 1)) return

if ((@list is null) or (@list = '') or (@list = @sep)) return

-- Add path wildcards directly with sep
-- ?worth it?
if (left(@sep, 1) <> '%') set @sep = '%' + @sep
if (right(@sep, 1) <> '%') set @sep = @sep + '%'

-- First first sep
declare @i int
set @i = patindex(@sep, @list)

-- Acc values
while (@i > 0) begin
insert into @ts ([value]) values (rtrim(left(@list, @i - 1)))
set @list = ltrim(right(RTRIM(@list), len(@list) + 3 - (@i + len(@sep) )))
set @i = patindex(@sep, @list)
end

set @list = rtrim(@list)
-- Insert last value, if any
if (@list <> '') insert into @ts (value) values (@list)

return

END


The function above basically takes my mapping string and converts it to a list of column names in a table (see query logic below):



DECLARE @Delim varchar(1) = '|'
DECLARE @Mapping varchar(max) = 'col1_source|col1_target;col2_source|col2_target;col3_source|col3_target'
DECLARE @String varchar(max) = REPLACE(@Mapping,';', @Delim)
SELECT * FROM dbo.UF_miscParseStringToTable(@String, @Delim)


The above resulting query yields the following table:



 ID| value
1 | col1_source
2 | col1_target
3 | col2_source
4 | col2_target
5 | col3_source
6 | col3_target


I could perhaps do a join on the column indexes but, I'm finding it difficult to isolate my source and target fields so that I could perform data comparisons between them. In addition, I'd like to avoid performing an extra join to a table if I don't have to.



Here are the results desired (to be able to perform the following):



SELECT col1_source, col2_source, col3_source FROM mytable;

SELECT col1_target, col2_target, col3_target FROM mytable;


Any help or ideas would be great!



Shawn










share|improve this question

























  • Your question told us what the source string was and also what your current query is returning. What results do you want returned? Also, please specify your version of SQL Server (2012, 2016, etc.)

    – Scott Hodgin
    3 hours ago











  • My apologies. It's SQL Server 2017.

    – shawnyshawny
    1 hour ago











  • Here are the results desired for the source columns: col1_source,col2_source,col3_source ; here are the results desired for the targets: col1_target,col2_target,col3_target

    – shawnyshawny
    1 hour ago
















0












0








0








I have the following format string stored in text (could be any number of columns):



col1_source|col1_target;col2_source|col2_target;col3_source|col3_target;...


I'm trying to come up with an elegant way of extracting and isolating all the xxx_source column names and all the xxx_target column names so I could store them in variables and get the following end result:



@Source_Columns = 'col1_source,col2_source,col3_source'
@Target_Columns = 'col1_target,col2_target,col3_target'



At the end of the day, I'd like to perform SELECTs on my source and target columns to perform data compares.



This is what I've achieved so far, but I find it's just too complex for nothing (with a table valued function):



CREATE FUNCTION [dbo].[UF_miscParseStringToTable]
(
@list nvarchar(MAX)
, @sep varchar(8)
)
RETURNS @ts table
(
[ID] int identity
, [value] nvarchar(MAX)
)
AS
BEGIN

-- Parameters check
if ((@sep is null) or (datalength(@sep) < 1)) return

if ((@list is null) or (@list = '') or (@list = @sep)) return

-- Add path wildcards directly with sep
-- ?worth it?
if (left(@sep, 1) <> '%') set @sep = '%' + @sep
if (right(@sep, 1) <> '%') set @sep = @sep + '%'

-- First first sep
declare @i int
set @i = patindex(@sep, @list)

-- Acc values
while (@i > 0) begin
insert into @ts ([value]) values (rtrim(left(@list, @i - 1)))
set @list = ltrim(right(RTRIM(@list), len(@list) + 3 - (@i + len(@sep) )))
set @i = patindex(@sep, @list)
end

set @list = rtrim(@list)
-- Insert last value, if any
if (@list <> '') insert into @ts (value) values (@list)

return

END


The function above basically takes my mapping string and converts it to a list of column names in a table (see query logic below):



DECLARE @Delim varchar(1) = '|'
DECLARE @Mapping varchar(max) = 'col1_source|col1_target;col2_source|col2_target;col3_source|col3_target'
DECLARE @String varchar(max) = REPLACE(@Mapping,';', @Delim)
SELECT * FROM dbo.UF_miscParseStringToTable(@String, @Delim)


The above resulting query yields the following table:



 ID| value
1 | col1_source
2 | col1_target
3 | col2_source
4 | col2_target
5 | col3_source
6 | col3_target


I could perhaps do a join on the column indexes but, I'm finding it difficult to isolate my source and target fields so that I could perform data comparisons between them. In addition, I'd like to avoid performing an extra join to a table if I don't have to.



Here are the results desired (to be able to perform the following):



SELECT col1_source, col2_source, col3_source FROM mytable;

SELECT col1_target, col2_target, col3_target FROM mytable;


Any help or ideas would be great!



Shawn










share|improve this question
















I have the following format string stored in text (could be any number of columns):



col1_source|col1_target;col2_source|col2_target;col3_source|col3_target;...


I'm trying to come up with an elegant way of extracting and isolating all the xxx_source column names and all the xxx_target column names so I could store them in variables and get the following end result:



@Source_Columns = 'col1_source,col2_source,col3_source'
@Target_Columns = 'col1_target,col2_target,col3_target'



At the end of the day, I'd like to perform SELECTs on my source and target columns to perform data compares.



This is what I've achieved so far, but I find it's just too complex for nothing (with a table valued function):



CREATE FUNCTION [dbo].[UF_miscParseStringToTable]
(
@list nvarchar(MAX)
, @sep varchar(8)
)
RETURNS @ts table
(
[ID] int identity
, [value] nvarchar(MAX)
)
AS
BEGIN

-- Parameters check
if ((@sep is null) or (datalength(@sep) < 1)) return

if ((@list is null) or (@list = '') or (@list = @sep)) return

-- Add path wildcards directly with sep
-- ?worth it?
if (left(@sep, 1) <> '%') set @sep = '%' + @sep
if (right(@sep, 1) <> '%') set @sep = @sep + '%'

-- First first sep
declare @i int
set @i = patindex(@sep, @list)

-- Acc values
while (@i > 0) begin
insert into @ts ([value]) values (rtrim(left(@list, @i - 1)))
set @list = ltrim(right(RTRIM(@list), len(@list) + 3 - (@i + len(@sep) )))
set @i = patindex(@sep, @list)
end

set @list = rtrim(@list)
-- Insert last value, if any
if (@list <> '') insert into @ts (value) values (@list)

return

END


The function above basically takes my mapping string and converts it to a list of column names in a table (see query logic below):



DECLARE @Delim varchar(1) = '|'
DECLARE @Mapping varchar(max) = 'col1_source|col1_target;col2_source|col2_target;col3_source|col3_target'
DECLARE @String varchar(max) = REPLACE(@Mapping,';', @Delim)
SELECT * FROM dbo.UF_miscParseStringToTable(@String, @Delim)


The above resulting query yields the following table:



 ID| value
1 | col1_source
2 | col1_target
3 | col2_source
4 | col2_target
5 | col3_source
6 | col3_target


I could perhaps do a join on the column indexes but, I'm finding it difficult to isolate my source and target fields so that I could perform data comparisons between them. In addition, I'd like to avoid performing an extra join to a table if I don't have to.



Here are the results desired (to be able to perform the following):



SELECT col1_source, col2_source, col3_source FROM mytable;

SELECT col1_target, col2_target, col3_target FROM mytable;


Any help or ideas would be great!



Shawn







sql-server-2017 string string-manipulation parse






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 1 hour ago







shawnyshawny

















asked 3 hours ago









shawnyshawnyshawnyshawny

84




84













  • Your question told us what the source string was and also what your current query is returning. What results do you want returned? Also, please specify your version of SQL Server (2012, 2016, etc.)

    – Scott Hodgin
    3 hours ago











  • My apologies. It's SQL Server 2017.

    – shawnyshawny
    1 hour ago











  • Here are the results desired for the source columns: col1_source,col2_source,col3_source ; here are the results desired for the targets: col1_target,col2_target,col3_target

    – shawnyshawny
    1 hour ago





















  • Your question told us what the source string was and also what your current query is returning. What results do you want returned? Also, please specify your version of SQL Server (2012, 2016, etc.)

    – Scott Hodgin
    3 hours ago











  • My apologies. It's SQL Server 2017.

    – shawnyshawny
    1 hour ago











  • Here are the results desired for the source columns: col1_source,col2_source,col3_source ; here are the results desired for the targets: col1_target,col2_target,col3_target

    – shawnyshawny
    1 hour ago



















Your question told us what the source string was and also what your current query is returning. What results do you want returned? Also, please specify your version of SQL Server (2012, 2016, etc.)

– Scott Hodgin
3 hours ago





Your question told us what the source string was and also what your current query is returning. What results do you want returned? Also, please specify your version of SQL Server (2012, 2016, etc.)

– Scott Hodgin
3 hours ago













My apologies. It's SQL Server 2017.

– shawnyshawny
1 hour ago





My apologies. It's SQL Server 2017.

– shawnyshawny
1 hour ago













Here are the results desired for the source columns: col1_source,col2_source,col3_source ; here are the results desired for the targets: col1_target,col2_target,col3_target

– shawnyshawny
1 hour ago







Here are the results desired for the source columns: col1_source,col2_source,col3_source ; here are the results desired for the targets: col1_target,col2_target,col3_target

– shawnyshawny
1 hour ago












1 Answer
1






active

oldest

votes


















0














You did not specify your RDBMS - my solution uses SQL Server.



Your question states that you have a formatted string stored in text



col1_source|col1_target;col2_source|col2_target;col3_source|col3_target;...


You further state




I'm trying to come up with an elegant way of extracting and isolating
all the xxx_source column names and all the xxx_target column names so
I could store them in variables and get the following end result:



@Source_Columns = 'col1_source,col2_source,col3_source'
@Target_Columns = 'col1_target,col2_target,col3_target'






My solution uses SQL Server 2016 STRING_SPLIT. If you're not up to that version, there are other methods to split the string.



drop table if exists #temp
Declare @String varchar(100) = 'col1_source|col1_target;col2_source|col2_target;col3_source|col3_target;'
----------------------------
Declare @SourceColumns varchar(max)
Declare @TargetColumns varchar(max)

--Split on semicolon and use PARSENAME to extract the source and target column information
SELECT @SourceColumns = isnull(@SourceColumns + ',', '') + SourceColumn
,@TargetColumns = isnull(@TargetColumns + ',', '') + TargetColumn
FROM (
SELECT parsename(replace(value, '|', '.'), 2) AS SourceColumn
,parsename(replace(value, '|', '.'), 1) AS TargetColumn
FROM (
SELECT *
FROM String_split(@string, ';')
) a
WHERE value <> ' '
) a




SELECT @SourceColumns



col1_source,col2_source,col3_source


SELECT @TargetColumns



col1_target,col2_target,col3_target





share|improve this answer
























  • Scott. Thanks very much. That's pretty much what I've been seeking in terms of a solution! I upvoted you, but my reputation does not allow me yet to show public upvotes.

    – shawnyshawny
    1 hour ago











  • @shawnyshawny - if my answer solves your problem, you might consider accepting it

    – Scott Hodgin
    1 hour ago











  • Hi Scott. Thanks for letting me know. It's done!

    – shawnyshawny
    52 mins ago











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "182"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f230382%2fsql-server-2017-extracting-source-and-target-column-names-within-a-string-cont%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You did not specify your RDBMS - my solution uses SQL Server.



Your question states that you have a formatted string stored in text



col1_source|col1_target;col2_source|col2_target;col3_source|col3_target;...


You further state




I'm trying to come up with an elegant way of extracting and isolating
all the xxx_source column names and all the xxx_target column names so
I could store them in variables and get the following end result:



@Source_Columns = 'col1_source,col2_source,col3_source'
@Target_Columns = 'col1_target,col2_target,col3_target'






My solution uses SQL Server 2016 STRING_SPLIT. If you're not up to that version, there are other methods to split the string.



drop table if exists #temp
Declare @String varchar(100) = 'col1_source|col1_target;col2_source|col2_target;col3_source|col3_target;'
----------------------------
Declare @SourceColumns varchar(max)
Declare @TargetColumns varchar(max)

--Split on semicolon and use PARSENAME to extract the source and target column information
SELECT @SourceColumns = isnull(@SourceColumns + ',', '') + SourceColumn
,@TargetColumns = isnull(@TargetColumns + ',', '') + TargetColumn
FROM (
SELECT parsename(replace(value, '|', '.'), 2) AS SourceColumn
,parsename(replace(value, '|', '.'), 1) AS TargetColumn
FROM (
SELECT *
FROM String_split(@string, ';')
) a
WHERE value <> ' '
) a




SELECT @SourceColumns



col1_source,col2_source,col3_source


SELECT @TargetColumns



col1_target,col2_target,col3_target





share|improve this answer
























  • Scott. Thanks very much. That's pretty much what I've been seeking in terms of a solution! I upvoted you, but my reputation does not allow me yet to show public upvotes.

    – shawnyshawny
    1 hour ago











  • @shawnyshawny - if my answer solves your problem, you might consider accepting it

    – Scott Hodgin
    1 hour ago











  • Hi Scott. Thanks for letting me know. It's done!

    – shawnyshawny
    52 mins ago
















0














You did not specify your RDBMS - my solution uses SQL Server.



Your question states that you have a formatted string stored in text



col1_source|col1_target;col2_source|col2_target;col3_source|col3_target;...


You further state




I'm trying to come up with an elegant way of extracting and isolating
all the xxx_source column names and all the xxx_target column names so
I could store them in variables and get the following end result:



@Source_Columns = 'col1_source,col2_source,col3_source'
@Target_Columns = 'col1_target,col2_target,col3_target'






My solution uses SQL Server 2016 STRING_SPLIT. If you're not up to that version, there are other methods to split the string.



drop table if exists #temp
Declare @String varchar(100) = 'col1_source|col1_target;col2_source|col2_target;col3_source|col3_target;'
----------------------------
Declare @SourceColumns varchar(max)
Declare @TargetColumns varchar(max)

--Split on semicolon and use PARSENAME to extract the source and target column information
SELECT @SourceColumns = isnull(@SourceColumns + ',', '') + SourceColumn
,@TargetColumns = isnull(@TargetColumns + ',', '') + TargetColumn
FROM (
SELECT parsename(replace(value, '|', '.'), 2) AS SourceColumn
,parsename(replace(value, '|', '.'), 1) AS TargetColumn
FROM (
SELECT *
FROM String_split(@string, ';')
) a
WHERE value <> ' '
) a




SELECT @SourceColumns



col1_source,col2_source,col3_source


SELECT @TargetColumns



col1_target,col2_target,col3_target





share|improve this answer
























  • Scott. Thanks very much. That's pretty much what I've been seeking in terms of a solution! I upvoted you, but my reputation does not allow me yet to show public upvotes.

    – shawnyshawny
    1 hour ago











  • @shawnyshawny - if my answer solves your problem, you might consider accepting it

    – Scott Hodgin
    1 hour ago











  • Hi Scott. Thanks for letting me know. It's done!

    – shawnyshawny
    52 mins ago














0












0








0







You did not specify your RDBMS - my solution uses SQL Server.



Your question states that you have a formatted string stored in text



col1_source|col1_target;col2_source|col2_target;col3_source|col3_target;...


You further state




I'm trying to come up with an elegant way of extracting and isolating
all the xxx_source column names and all the xxx_target column names so
I could store them in variables and get the following end result:



@Source_Columns = 'col1_source,col2_source,col3_source'
@Target_Columns = 'col1_target,col2_target,col3_target'






My solution uses SQL Server 2016 STRING_SPLIT. If you're not up to that version, there are other methods to split the string.



drop table if exists #temp
Declare @String varchar(100) = 'col1_source|col1_target;col2_source|col2_target;col3_source|col3_target;'
----------------------------
Declare @SourceColumns varchar(max)
Declare @TargetColumns varchar(max)

--Split on semicolon and use PARSENAME to extract the source and target column information
SELECT @SourceColumns = isnull(@SourceColumns + ',', '') + SourceColumn
,@TargetColumns = isnull(@TargetColumns + ',', '') + TargetColumn
FROM (
SELECT parsename(replace(value, '|', '.'), 2) AS SourceColumn
,parsename(replace(value, '|', '.'), 1) AS TargetColumn
FROM (
SELECT *
FROM String_split(@string, ';')
) a
WHERE value <> ' '
) a




SELECT @SourceColumns



col1_source,col2_source,col3_source


SELECT @TargetColumns



col1_target,col2_target,col3_target





share|improve this answer













You did not specify your RDBMS - my solution uses SQL Server.



Your question states that you have a formatted string stored in text



col1_source|col1_target;col2_source|col2_target;col3_source|col3_target;...


You further state




I'm trying to come up with an elegant way of extracting and isolating
all the xxx_source column names and all the xxx_target column names so
I could store them in variables and get the following end result:



@Source_Columns = 'col1_source,col2_source,col3_source'
@Target_Columns = 'col1_target,col2_target,col3_target'






My solution uses SQL Server 2016 STRING_SPLIT. If you're not up to that version, there are other methods to split the string.



drop table if exists #temp
Declare @String varchar(100) = 'col1_source|col1_target;col2_source|col2_target;col3_source|col3_target;'
----------------------------
Declare @SourceColumns varchar(max)
Declare @TargetColumns varchar(max)

--Split on semicolon and use PARSENAME to extract the source and target column information
SELECT @SourceColumns = isnull(@SourceColumns + ',', '') + SourceColumn
,@TargetColumns = isnull(@TargetColumns + ',', '') + TargetColumn
FROM (
SELECT parsename(replace(value, '|', '.'), 2) AS SourceColumn
,parsename(replace(value, '|', '.'), 1) AS TargetColumn
FROM (
SELECT *
FROM String_split(@string, ';')
) a
WHERE value <> ' '
) a




SELECT @SourceColumns



col1_source,col2_source,col3_source


SELECT @TargetColumns



col1_target,col2_target,col3_target






share|improve this answer












share|improve this answer



share|improve this answer










answered 2 hours ago









Scott HodginScott Hodgin

17.4k21534




17.4k21534













  • Scott. Thanks very much. That's pretty much what I've been seeking in terms of a solution! I upvoted you, but my reputation does not allow me yet to show public upvotes.

    – shawnyshawny
    1 hour ago











  • @shawnyshawny - if my answer solves your problem, you might consider accepting it

    – Scott Hodgin
    1 hour ago











  • Hi Scott. Thanks for letting me know. It's done!

    – shawnyshawny
    52 mins ago



















  • Scott. Thanks very much. That's pretty much what I've been seeking in terms of a solution! I upvoted you, but my reputation does not allow me yet to show public upvotes.

    – shawnyshawny
    1 hour ago











  • @shawnyshawny - if my answer solves your problem, you might consider accepting it

    – Scott Hodgin
    1 hour ago











  • Hi Scott. Thanks for letting me know. It's done!

    – shawnyshawny
    52 mins ago

















Scott. Thanks very much. That's pretty much what I've been seeking in terms of a solution! I upvoted you, but my reputation does not allow me yet to show public upvotes.

– shawnyshawny
1 hour ago





Scott. Thanks very much. That's pretty much what I've been seeking in terms of a solution! I upvoted you, but my reputation does not allow me yet to show public upvotes.

– shawnyshawny
1 hour ago













@shawnyshawny - if my answer solves your problem, you might consider accepting it

– Scott Hodgin
1 hour ago





@shawnyshawny - if my answer solves your problem, you might consider accepting it

– Scott Hodgin
1 hour ago













Hi Scott. Thanks for letting me know. It's done!

– shawnyshawny
52 mins ago





Hi Scott. Thanks for letting me know. It's done!

– shawnyshawny
52 mins ago


















draft saved

draft discarded




















































Thanks for contributing an answer to Database Administrators Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f230382%2fsql-server-2017-extracting-source-and-target-column-names-within-a-string-cont%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Szabolcs (Ungheria) Altri progetti | Menu di navigazione48°10′14.56″N 21°29′33.14″E /...

Discografia di Klaus Schulze Indice Album in studio | Album dal vivo | Singoli | Antologie | Colonne...

How to make inet_server_addr() return localhost in spite of ::1/128RETURN NEXT in Postgres FunctionConnect to...