Solved

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

Posted on 2013-06-18
4
176 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
  • 2
  • 2
4 Comments
 
LVL 24

Expert Comment

by:chaau
Comment Utility
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
Comment Utility
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 24

Accepted Solution

by:
chaau earned 500 total points
Comment Utility
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
Comment Utility
Thanks chaau, that is perfect...
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Suggested Solutions

Loading csv or delimited data files to MySQL database is a very common task frequently questioned about and almost every time LOAD DATA INFILE comes to the rescue. Here we will try to understand some of the very common scenarios for loading data …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

771 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

7 Experts available now in Live!

Get 1:1 Help Now