Solved

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

Posted on 2013-06-18
4
178 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
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 24

Accepted Solution

by:
chaau earned 500 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

All XML, All the Time; More Fun MySQL Tidbits – Dynamically Generate XML via Stored Procedure in MySQL Extensible Markup Language (XML) and database systems, a marriage we are seeing more and more of.  So the topics of parsing and manipulating XM…
I use MySQL for many of my development projects in a Windows environment. To manage my databases (and perform queries) for years I used a tool called MySQL administrator.  This tool has since been replaced by MySQL Workbench. So I decided to m…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

831 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