Need help formatting CONCAT() for MySQL query

codacopia

I have a table where I am attempting to take 3 database table values and reformat them in a single value. Here is the SQL statement that I have at the moment:

SELECT 
  CASE WHEN cb_cardtype = 'Discover Credit Card' 
       THEN 'DS' 
       END + 
       ';' + RIGHT(cardnumbers,4) + ';' + LPAD(MONTH(planexpdate), 2, '0') +
       '/' + LPAD(YEAR(planexpdate), 2, '0') AS account_billing_key 
FROM my_table

So what I wanted to get as an output here would be:

DS;4242;07/14

The problem is that I am using the + to attempt this, which actually adds the values together. Rather, I understand that I need to use CONCAT() to merge the values. I am unclear about how I can pull the individual values and then concatenate them as desired.

Joachim Isaksson

If your query is otherwise correct, all you need to do is to wrap all the strings you want to concatenate - comma separated - inside a call to CONCAT;

SELECT 
  CONCAT(
    CASE WHEN cb_cardtype = 'Discover Credit Card' THEN 'DS' END, 
    ';', 
    RIGHT(cardnumbers,4), 
    ';', 
    LPAD(MONTH(planexpdate), 2, '0'),
    '/', 
    LPAD(YEAR(planexpdate), 2, '0')
  ) AS account_billing_key 
FROM my_table

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related