Solved

How to conditionally format decimal numbers in mysql SQL?

Posted on 2009-06-29
4
412 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

I have been using r1soft Continuous Data Protection (http://www.r1soft.com/linux-cdp/) for many years now with the mySQL Addon and wanted to share a trick I have used several times. For those of us that don't have the luxury of using all transact…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

746 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

9 Experts available now in Live!

Get 1:1 Help Now