Not escaping a mysql variable within a string.

Posted on 2005-04-12
Last Modified: 2006-11-18
I have the following query:

SELECT @test := 'hello', CONCAT('Message: @test', column_name)

The problem is that this displays "Message: @test", instead I want to display the variable @test that I set right before it, although, I do NOT want to put it seperately like this:

CONCAT('Message:', @test, column_name)

Is there anyway to evaluate a variable enrapped inside quotes without seperating it from the rest of the string so that it shows its value?
Question by:andw928
    LVL 33

    Expert Comment

    No.  Why don't you want to list it separately?
    LVL 10

    Expert Comment

    It is not doing what it should because how it can distinguish whether you want a variable substitution or the string in itself (i.e. @test in this case). By putting it inside the quotes ' ', you are making it a string.
    LVL 7

    Author Comment

    snoyes, because I am passing a coldfusion function which can't be seperated.
    kupra1, you did not read my entire post.

    Does anybody else have a possible solution?
    LVL 33

    Accepted Solution

    AFAIK, MySQL does not support any kind of automatic variable replacement inside string literals.

    You could do this:
    REPLACE(CONCAT('Message: @test', column_name), '@test', @test)
    LVL 7

    Author Comment

    You are a genius my friend, it works well, thank you!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Foreword This is an old article.  Instead of using the MySQL extension that was used in the original code examples, please choose one of the currently supported database extensions instead.  More information is available here: MySQLi / PDO (http://…
    Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    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

    14 Experts available now in Live!

    Get 1:1 Help Now