user2906420
user2906420

Reputation: 1269

TSQL replace my Stored procedure string parameter

My question: once I receive the parameter value, I want to modify the contents.

E.g. a string parameter value is: FullName,Address,Category

I want to change FullName to l.FullName and Category to c.Category and keep the rest same.

ALTER PROCEDURE [dbo].[TableA] @ColNames VARCHAR(1000)
AS
BEGIN
    //I want to modify the contents of @ColNames here

Upvotes: 0

Views: 1883

Answers (4)

Paul Lucaciu
Paul Lucaciu

Reputation: 134

ALTER PROCEDURE [dbo].[TableA] @ColNames VARCHAR(1000)
AS
BEGIN
    SET
        @ColNames = REPLACE(@ColNames, 'FullName', 'l.FullName')
        ;
    SET
        @ColNames = REPLACE(@ColNames, 'Category', 'c.Category')
        ;

Upvotes: 0

Adi
Adi

Reputation: 232

declare 
@ColNames varchar(50)


select @ColNames = replace(@ColNames, 'FullName', 'l.FullName');
select @ColNames = replace(@ColNames, 'Category', 'c.Category');

Upvotes: 0

Donal
Donal

Reputation: 32713

Yes, you can do that - @ColNames is a variable - you can manipulate the contents of it.

ALTER PROCEDURE [dbo].[TableA] @ColNames VARCHAR(1000)
AS
BEGIN
    -- I want to modify the contents of @ColNames here
SET @ColNames = REPLACE(@ColNames, "FullName", "l.FullName")
SET @ColNames = REPLACE(@ColNames, "Category", "c.Category")

Upvotes: 0

Gordon Linoff
Gordon Linoff

Reputation: 1270443

How about this?

select @ColNames = replace(@ColNames, 'FullName', 'l.FullName');
select @ColNames = replace(@ColNames, 'Category', 'c.Category');

Upvotes: 1

Related Questions