Solved

How to conditionally format decimal numbers in mysql SQL?

Posted on 2009-06-29
4
415 Views
Last Modified: 2012-06-27
Hi Experts,

I have several decimal fields containing dollar amounts in table1.  I am inserting decimal data from the first table into a second table using SQL "INSERT INTO table2 varchar_field SELECT ....  FROM table1" sql statement. The target field in second table is varchar(125).

As part of the SQL, I want to perform a format and concatenation operation on the decimal data as follows:

First the Format operation:
000000000.00 --> 0.00     (leave one leading zero when = 0)
000000526.84 --> 526.84  (remove all leading zeros when not 0)

Second the Concatenate operation:
#/0.00|526.84/#.


I know I can do concatenation like this:
  concat('#/', decAmt1, '|', decAmt2, '/#'), but is it possible to do a conditional format as described above?

Thanks much.

0
Comment
Question by:IT79637
4 Comments
 
LVL 4

Expert Comment

by:imgriff
ID: 24741333
Unfortunately MySQL does not support conditional(IF/ELSE) statements unless you use stored procedures. You night have to write your logic in PHP
0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 250 total points
ID: 24741992
Yes, you can use a CASE statement or IF.

SELECT CONCAT('#/', IF(decAmt1=0, 'true', 'false'), '|', CASE decAmt2 WHEN 0 THEN 'true' ELSE 'false' END, '/#')

However, for your case you may be better off just using FORMAT().  If you don't want the number to include the commas, you can just replace those.

http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_format

SELECT CONCAT('#/', FORMAT(decAmt1, 2), '|', FORMAT(decAmt2, 2), '/#')

To replace the ',', just add a replace as mentioned:

REPLACE(FORMAT(decAmt1, 2), ',', '')
0
 
LVL 1

Expert Comment

by:gkhngkdmr
ID: 24743440
this sample may help you to format the number...

select '#'||to_char(00000.00,'FM0.90')||'|'||to_char(000000526.84,'FM99999999999990.00')||'#'  
from dual
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

777 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