?
Solved

MySQL - remove $ from results and add "-" for a specific group?

Posted on 2013-06-18
4
Medium Priority
?
183 Views
Last Modified: 2013-06-18
Hi,
Is it possible to modify my select statement below to include a negative sign "-" for rec_yearly_total when budget_type is equal to 'E' ?
Also can I remove a '$' sign from the rec_yearly_total column?

Thanks,

Andrew


SELECT 
	  case when budget_type = 'I' then 'INCOME'
           when budget_type = 'E' then 'EXPENSE'
           else budget_type
      end AS 'INCOME or EXPENSE'
	, rec_description AS DESCRIPTION 	  
    , case when budget_group_id = 1 then 'Income'
           when budget_group_id = 2 then 'Commitments'
		   when budget_group_id = 3 then 'Home'
		   when budget_group_id = 4 then 'Utilities'
		   when budget_group_id = 5 then 'Education'
		   when budget_group_id = 6 then 'Health'
		   when budget_group_id = 7 then 'Shopping'
		   when budget_group_id = 8 then 'Transport'
		   when budget_group_id = 9 then 'Entertainment'
		   when budget_group_id = 10 then 'Eating Out'
           else budget_group_id
      end AS 'GROUP HEADING'
    , case when rec_frequency = 'D' then 'Daily'
           when rec_frequency = 'W' then 'Weekly'
		   when rec_frequency = 'F' then 'Fortnightly'
		   when rec_frequency = 'M' then 'Monthly'
		   when rec_frequency = 'Y' then 'Yearly'	  
	       else rec_frequency 
	  end AS FREQUENCY
    , rec_total AS TOTAL
    , rec_yearly_total AS 'YEARLY TOTAL'
FROM `budget_planner` 
WHERE user_id = '5028' ORDER BY budget_type, budget_group_id

Open in new window

0
Comment
Question by:sabecs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 25

Expert Comment

by:chaau
ID: 39258162
This is how to add "-":
SELECT 
	  case when budget_type = 'I' then 'INCOME'
           when budget_type = 'E' then 'EXPENSE'
           else budget_type
      end AS 'INCOME or EXPENSE'
	, rec_description AS DESCRIPTION 	  
    , case when budget_group_id = 1 then 'Income'
           when budget_group_id = 2 then 'Commitments'
		   when budget_group_id = 3 then 'Home'
		   when budget_group_id = 4 then 'Utilities'
		   when budget_group_id = 5 then 'Education'
		   when budget_group_id = 6 then 'Health'
		   when budget_group_id = 7 then 'Shopping'
		   when budget_group_id = 8 then 'Transport'
		   when budget_group_id = 9 then 'Entertainment'
		   when budget_group_id = 10 then 'Eating Out'
           else budget_group_id
      end AS 'GROUP HEADING'
    , case when rec_frequency = 'D' then 'Daily'
           when rec_frequency = 'W' then 'Weekly'
		   when rec_frequency = 'F' then 'Fortnightly'
		   when rec_frequency = 'M' then 'Monthly'
		   when rec_frequency = 'Y' then 'Yearly'	  
	       else rec_frequency 
	  end AS FREQUENCY
    , rec_total AS TOTAL
    , CASE WHEN budget_type = 'E' THEN - rec_yearly_total ELSE rec_yearly_total END AS 'YEARLY TOTAL'
FROM `budget_planner` 
WHERE user_id = '5028' ORDER BY budget_type, budget_group_id

Open in new window


However, I do not see $ sign anywhere in the SQL query. It must be the frontend that puts it. Can you advise what you use for the reporting?
0
 

Author Comment

by:sabecs
ID: 39258169
Thanks chaau, the "$' sign is contained in the rec_yearly_total field, for example $123.45 and I was wondering if I can remove it to leave just the 123.45 ?
Perhaps I could remove the first char somehow via the query?
0
 
LVL 25

Accepted Solution

by:
chaau earned 2000 total points
ID: 39258179
So, it is a text column? Then in this case my query is incorrect. It will be this:

SELECT 
	  case when budget_type = 'I' then 'INCOME'
           when budget_type = 'E' then 'EXPENSE'
           else budget_type
      end AS 'INCOME or EXPENSE'
	, rec_description AS DESCRIPTION 	  
    , case when budget_group_id = 1 then 'Income'
           when budget_group_id = 2 then 'Commitments'
		   when budget_group_id = 3 then 'Home'
		   when budget_group_id = 4 then 'Utilities'
		   when budget_group_id = 5 then 'Education'
		   when budget_group_id = 6 then 'Health'
		   when budget_group_id = 7 then 'Shopping'
		   when budget_group_id = 8 then 'Transport'
		   when budget_group_id = 9 then 'Entertainment'
		   when budget_group_id = 10 then 'Eating Out'
           else budget_group_id
      end AS 'GROUP HEADING'
    , case when rec_frequency = 'D' then 'Daily'
           when rec_frequency = 'W' then 'Weekly'
		   when rec_frequency = 'F' then 'Fortnightly'
		   when rec_frequency = 'M' then 'Monthly'
		   when rec_frequency = 'Y' then 'Yearly'	  
	       else rec_frequency 
	  end AS FREQUENCY
    , rec_total AS TOTAL
    , CASE WHEN budget_type = 'E' THEN Concat('-', REPLACE(rec_yearly_total, '$', '')) ELSE REPLACE(rec_yearly_total, '$', '') END AS 'YEARLY TOTAL'
FROM `budget_planner` 
WHERE user_id = '5028' ORDER BY budget_type, budget_group_id

Open in new window

0
 

Author Closing Comment

by:sabecs
ID: 39258482
Thanks chaau, that is perfect...
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
In this article, we’ll look at how to deploy ProxySQL.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

764 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