Marco Dinatsoli
Marco Dinatsoli

Reputation: 10570

SQL Server : can't concatenate string in the middle of other strings

In my SQL Server stored procedures I want to do this:

"hello xxx how are you"

where xxx is a value of a variable

I tried this

CREATE PROCEDURE insertToPinTable
    (@callerID VARCHAR (200),  
     @vAccount VARCHAR (200))
AS
BEGIN
     DECLARE @textEnglish VARCHAR
     textEnglish = CONCAT ( "hello", @vAccount)

     INSERT INTO pinData([CallerID], [body], [processed]) 
     VALUES (@callerID, @textEnglish, 0)
END

I get an error

Incorrect syntax near 'textEnglish'

it seems that using CONCAT is not correct, could you help me please

(if I can insert the value of vAccount to the hello word, then in the same way I can insert the value of textEnglish, to the string how are you)

Upvotes: 0

Views: 1475

Answers (2)

Gottfried Lesigang
Gottfried Lesigang

Reputation: 67311

If you just want to change a string from

"hello xxx how are you"

to

"hello Mr. Smith how are you"

You might think about place holder replacement:

Define your string as a template:

DECLARE @template VARCHAR(100)='hello {Name} how are you?';

And then use

SELECT REPLACE(@template,'{Name}','Mr. Smith');

Upvotes: 0

Vamsi Prabhala
Vamsi Prabhala

Reputation: 49260

CREATE  PROCEDURE insertToPinTable
(
@callerID VARCHAR (200),  
@vAccount VARCHAR (200)    
)
AS
BEGIN

DECLARE @textEnglish VARCHAR(255) --missing length previously
set @textEnglish = 'hello'+ @vAccount --missing set keyword
INSERT INTO pinData([CallerID], [body], [processed]) VALUES (@callerID, @textEnglish, 0)

END

Upvotes: 4

Related Questions