Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to conditionally format decimal numbers in mysql SQL?

Posted on 2009-06-29
4
Medium Priority
?
426 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 60

Accepted Solution

by:
Kevin Cross earned 1000 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

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…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month13 days, 6 hours left to enroll

972 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