Solved

How to conditionally format decimal numbers in mysql SQL?

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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
java mysql insert application 14 44
SQL - Simple Pivot query 8 27
MySQL Finding Duplicates in a Normalized Database 6 28
T-SQL Query - Group By Year 3 29
Creating and Managing Databases with phpMyAdmin in cPanel.
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

749 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