Rob Bor Rob Bor - 2 months ago 24
MySQL Question

How to display data with suffix/prefix in sql?

I want to write an SQL Statement that displays details (id, date and cost) of Table_Session. Display sessions that are scheduled in June or costs $14 or less. Cost must be prefixed with "$" and the results must be sorted in descending order.

Ive gotten everything apart from "prefixed with $" How do I do that?

SELECT SESSIONID, SESSIONDATE, SESSIONPRICE
FROM SESSIONS
WHERE SESSIONDATE LIKE '%JUN%'
OR SESSIONPRICE < 14
ORDER BY SESSIONPRICE DESC;


is what Ive got. What needs to be added to make prefix or suffixes in sql?

Answer

You need to use the CONCAT function in MySQL to concatenate strings:

SELECT SESSIONID,
       SESSIONDATE,
       CONCAT('$', CAST(SESSIONPRICE AS VARCHAR(15))
FROM SESSIONS
WHERE SESSIONDATE LIKE '%JUN%' OR
      SESSIONPRICE < 14
ORDER BY SESSIONPRICE DESC
Comments