Solved

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

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to send multiple emails at the same time in PHP 12 90
Determining creation & modification dates on MySQL tables 4 55
remote mysql 8 53
issue with DB import 1 51
Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…

734 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