I am looking for a way to concatanate parameters

Posted on 2011-10-31
Last Modified: 2012-05-12
Hello, I am looking for a way to concatanate parameters in the following way:

@cname as varchar(6),
@mname as varchar(5)

DECLARE @cmname as varchar(12)

How would I concatanate @cname + '-' + @mname and send the values to @cmname?

I do not want the value of cmname to be changed by the user of the query or program but I would like that value of @cmname to be stored as a parameter to be used in a query that I am writing.  How would I do this?
Question by:VBBRett
    LVL 1

    Expert Comment

    You should write something like this:
    CREATE PROCEDURE TEST(IN str1 varchar(10), IN str2 varchar(10), IN str3 varchar(10)) 
    SET @query = CONCAT("SELECT fieldname1, fieldname2 from tablename WHERE ", str1, str2, str3); 
    PREPARE statement FROM @query; 
    EXECUTE statement; 

    Open in new window

    Reference :,264282,264292#msg-264292

    Author Comment

    OK, so how would you declare a local variable and hold data but not have to receive input from a user in sql server?
    LVL 1

    Expert Comment

    I think you didn't get my point. Try using this.

    SET @cmname = CONCAT(cname,'-',mname)

    Open in new window


    Author Comment

    set did not work for some reason and concat is not a recognized term.  Are you using sql server?
    LVL 5

    Accepted Solution


    to set value to @cmname, you can use this:
    DECLARE @cmname as varchar(12)
    SET @cmname = @cname + '-' + @mname

    Open in new window


    Author Closing Comment

    Thank you very much!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    Title # Comments Views Activity
    stored proc for auto update 6 39
    SQL Query 2 44
    Oracle PL/SQL syntax 4 29
    SQL Round a percentage 2 13
    'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
    PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
    This video discusses moving either the default database or any database to a new volume.
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now